I made a Chatbot using LINE Messaging API and Python

Introduction

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

Postscript (2016/10/18)

The source code of the bot has been released on github. https://github.com/Sciseed/yukko-line-bot

System configuration

The development environment this time is as follows. -Python 3.4.0 ・ Heroku · Django 1.8.14

How to make

0. Build a server

This time I used Heroku and set up a server using Django. (I will explain about server construction later)

1. Create a LINE business account

Register a business account and create a Messaging API account. https://business.line.me/ja/services/bot

2. Set account information and webhook URL

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) webhookurl.jpg

3. Implemented BOT API processing

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))

4. Tried new features added by Messaging API

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))

5. Completion

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)

Summary

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.

Continued article

I made a Chatbot using LINE Messaging API and Python (2)

reference

I tried to make my own high school girl BOT with LINE BOT (Python & Heroku)Run LINE BOT on Heroku for freeLINE API Reference -Summary when deploying Django apps on HerokuI tried to make LINE BOT with Python using LINE BOT API

Recommended Posts

I made a Chatbot using LINE Messaging API and Python
I made a Chatbot using LINE Messaging API and Python (2) ~ Server ~
I made a LINE BOT with Python and Heroku
I made a Line-bot using Python!
I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
I tried using Twitter api and Line api
I made a Python Qiita API wrapper "qiipy"
I made a python text
I made a login / logout process using Python Bottle.
[LINE Messaging API] Create a rich menu in Python
I made a quick feed reader using feedparser in Python
I made a fortune with Python.
[Python] I made a Line bot that randomly asks English words.
[Python3] I made a decorator that declares undefined functions and methods.
I created a Python library to call the LINE WORKS API
[LINE Messaging API] Create a BOT that connects with someone with Python
I made a poker game server chat-holdem using websocket with python
I made a Python wrapper library for docomo image recognition API.
I made a chatbot with Tensor2Tensor and this time it worked
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
I made a payroll program in Python!
I made a character counter with Python
Beginner: I made a launcher using dictionary
I tried using UnityCloudBuild API from Python
I made a Hex map with Python
After studying Python3, I made a Slackbot
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I tried to create a sample to access Salesforce using Python and Bottle
I want to make a web application using React and Python flask
I made a stamp substitute bot with line
I made a python dictionary file for Neocomplete
I made a weather forecast bot-like with Python.
I tried web scraping using python and selenium
I made a GUI application with Python + PyQt5
I tried object detection using Python and OpenCV
I made a Twitter fujoshi blocker with Python ①
I wrote a class in Python3 and Java
[Python] I made a Youtube Downloader with Tkinter.
I made a LINE Bot with Serverless Framework!
I tried reading a CSV file using Python
Create a Mac app using py2app and Python3! !!
Try using ChatWork API and Qiita API in Python
I made a Caesar cryptographic program in Python.
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I made a Line Bot that uses Python to retrieve unread Gmail emails!
[For beginners] I made a motion sensor with Raspberry Pi and notified LINE!
I made an in-house peer bonus tool (aggregation only) using Python and BigQuery
I made a script to record the active window using win32gui of Python
〇✕ I made a game
[AWS] I made a reminder BOT with LINE WORKS
I made a Twitter BOT with GAE (python) (with a reference)
I made a prime number generation program in Python
I made a household account book bot with LINE Bot
Try creating a compressed file using Python and zlib
I made a Christmas tree lighting game with Python
I made a net news notification app with Python