I participated in the summer internship of Sciseed inc. and developed LINE Chatbot. Since the official version of Messaging API was announced from LINE on September 29, 2016, this time I created a bot that automatically responds with the official version.
[LINE] Announced new developments for the development and popularization of chatbots, released a new Messaging API, and officially started providing it to developers https://linecorp.com/ja/pr/news/ja/2016/1516
The source code of the bot has been released on github. https://github.com/Sciseed/yukko-line-bot
The development environment this time is as follows. -Python 3.4.0 ・ Heroku · Django 1.8.14
This time I used Heroku and set up a server using Django. (I will explain about server construction later)
Register a business account and create a Messaging API account. https://business.line.me/ja/services/bot
A Channel Access Token will be issued when you create an API account. Please make a note of it as it will be used. The webhook URL is an endpoint that notifies you when a message is sent from LINE. Specify the URL created on Heroku. (Since SSL communication was specified at the time of Trial, I wonder if there is this time as well) (It seems that port number 443 is not necessary)
Create a method by referring to LINE API Reference. The following code will return a message sent by the user to Echolalia. (The API used in the photo below is chatting using docomo's chat API)
view.py
REPLY_ENDPOINT = 'https://api.line.me/v2/bot/message/reply'
def post_text(reply_token, text):
header = {
"Content-Type": "application/json",
"Authorization": "Bearer {ENTER_ACCESS_TOKEN}"
}
payload = {
"replyToken":reply_token,
"messages":[
{
"type":"text",
"text": text
}
]
}
requests.post(REPLY_ENDPOINT, headers=header, data=json.dumps(payload))
I implemented Carousel using the newly introduced Template Message function. There were more restrictions than I expected and it was a little difficult. Pay attention to the limit on the number of elements and the limit on the number of characters in the description. Also, according to the documentation, thumbnails need to be sent over HTTPS (not sure if they can be sent over HTTP).
On the LINE chat screen, "What is the recommended restaurant?" "Tell me a good lunch spot." Such, "Restaurant" "Lunch" and "Recommended" "Tell me" If you enter a sentence that includes, etc., Carousel will be displayed.
views.py
def post_carousel(reply_token):
header = {
"Content-Type": "application/json",
"Authorization": "Bearer {ENTER_ACCESS_TOKEN}"
}
payload = {
"replyToken":reply_token,
"messages":[
{
"type": "template",
"altText": "Recommended restaurant",
"template": {
"type": "carousel",
"columns": [
{
"thumbnailImageUrl": "https://s3-us-west-2.amazonaws.com/lineapitest/hamburger_240.jpeg ",
"title": "Junk burger",
"text": "No matter who says it, the king of junk food is still a hamburger.",
"actions": [
{
"type": "uri",
"label": "view the details",
"uri": "http://example.com/page/222"
}
]
},
{
"thumbnailImageUrl": "https://s3-us-west-2.amazonaws.com/lineapitest/pizza_240.jpeg ",
"title": "pizza cap",
"text": "Authentic Naples taste fast and cheap. It is a pizza specialty store with 17 stores in Tokyo.",
"actions": [
{
"type": "uri",
"label": "view the details",
"uri": "http://example.com/page/222"
}
]
},
{
"thumbnailImageUrl": "https://s3-us-west-2.amazonaws.com/lineapitest/bread_240.jpeg ",
"title": "Authentic bread studio Takeyoshi",
"text": "What do you think is the most important thing for bread? Takeyoshi believes that baking the surface is the life.",
"actions": [
{
"type": "uri",
"label": "view the details",
"uri": "http://example.com/page/222"
}
]
},
{
"thumbnailImageUrl": "https://s3-us-west-2.amazonaws.com/lineapitest/harumaki_240.jpeg ",
"title": "Vietnam Tokyo",
"text": "A long-established Vietnamese restaurant in Higashiikebukuro. We offer Vietnamese food that has been loved by people for over 40 years.",
"actions": [
{
"type": "uri",
"label": "view the details",
"uri": "http://example.com/page/222"
}
]
},
]
}
}
]
}
req = requests.post(REPLY_ENDPOINT, headers=header, data=json.dumps(payload))
I was able to do it relatively quickly. Compared to the previous trial version, the documentation was easier to understand, so it was much easier to develop than expected.
Add friends to LINE and give it a try! ^ ^
Tips ・ If "... I cannot reply individually with this account ..." is returned when sending a message, the automatic response function is working, so LINE @ MANAGER → Account settings → Bot settings → Automatic response If you turn off the message temporarily, it will reply. -In the Trial version, it was necessary to specify port number 443, but it seems that it is no longer necessary. -Carousel did not work on iPhone as of 9/30/2016.
-This time, we only return the parrot, but if you return the text passed to post_text with docomo's chat API, you can easily make a chat robot. (Implemented in the above account)
Some more features have been added from the Trial version. The development of language processing is also progressing, and it seems that more and more high-quality, multifunctional chatbots will be born. Saiseed, who participated as an intern this time, uses natural language processing and machine learning to provide high-quality advice to job hunting students "Job Hunting My Concier". We are developing a LINE service. It seems that we are also looking for internship students for engineers at any time.
I made a Chatbot using LINE Messaging API and Python (2)
・ I tried to make my own high school girl BOT with LINE BOT (Python & Heroku) ・ Run LINE BOT on Heroku for free ・ LINE API Reference -Summary when deploying Django apps on Heroku ・ I tried to make LINE BOT with Python using LINE BOT API
Recommended Posts