[Super easy] Let's make a LINE BOT with Python.

1.First of all

This article is written with the following two prerequisites.

--Easy to understand even for beginners ――Be lean and concise

2. Overview

Personally, the official SDK of LINE Messaging API is difficult to use. https://github.com/line/line-bot-sdk-python

Therefore, I created a wrapper called pylinebot so that even beginners of programming can easily handle LINE BOT. https://github.com/nanato12/pylinebot

3. Advance preparation (necessary items)

3.1 Creating and setting up a LINE account for BOT

I wrote it in this article, so please refer to it.

[For beginners] LINE official account creation and setting for creating LINE BOT
https://qiita.com/nanato12/items/25e2db9461bb6ac2b8c5

3.2 Package installation

This article uses Flask. You can use Django.

$ pip install flask
$ pip install pylinebot

3.3 ngrok setup

Download from the link below and set it so that you can use ngrok. https://ngrok.com/

$ ngrok version
ngrok version 2.3.35

4. Let's make an Echolalia bot

Basic form of bot making! For the time being, I feel like making Echolalia.

The directory structure is as follows.

linebot  ┠ app.py  ┗ op.py

Only this! Sounds easy, doesn't it?

4.1 Write the code

With channel_access_token Enter the channel_secret of your bot.

app.py


from flask import Flask, request
from pylinebot import LINE, Tracer

from op import receive_message

DEBUG = True

app = Flask(__name__)

bot = LINE(
    channel_access_token='XXXXXXXXXXXXXXXXXXX',
    channel_secret='XXXXXXXXX'
)
tracer = Tracer(bot, debug=DEBUG)
tracer.add_event('message', receive_message)

#For webhooks
@app.route("/", methods=['POST'])
def hello():
    signature = request.headers['X-Line-Signature']
    body = request.get_data(as_text=True)
    tracer.trace(body, signature)
    return 'OK'

#For connection test
@app.route("/test", methods=['GET'])
def test():
    return 'OK'

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=3000, debug=DEBUG)

op.py


def receive_message(bot, event):
    message = event.message
    message_type = message.type

    if message_type == 'text':
        message_text = message.text
        bot.reply_text_message(message_text)

### 4.2 Run & Publish & Test

Let's run app.py.

$ python3 app.py
# * Serving Flask app "app" (lazy loading)
# * Environment: production
#   WARNING: This is a development server. Do not use it in a production deployment.
#   Use a production WSGI server instead.
# * Debug mode: on
# * Running on http://0.0.0.0:3000/ (Press CTRL+C to quit)
# * Restarting with stat
# * Debugger is active!
# * Debugger PIN: 984-300-804

Did it start like this?
Let's publish using ngrok in another tab.

$ ngrok http 3000
#ngrok by @inconshreveable                           (Ctrl+C to quit)
                                                                    
# Session Status                online                                
# Session Expires               7 hours, 58 minutes                   
# Version                       2.3.35                                
# Region                        United States (us)                    
# Web Interface                 http://127.0.0.1:4040                 
# Forwarding                    http://70fc9cf8b47c.ngrok.io -> http:/
# Forwarding                    https://70fc9cf8b47c.ngrok.io -> http:
                                                                    
# Connections                   ttl     opn     rt1     rt5     p50   
#                               0       0       0.00    0.00    0.00 

Use the https URL.
Since / test is used for connection test, in my browser Let's connect to https://70fc9cf8b47c.ngrok.io/test.

The word ** OK ** is displayed in the browser, It is OK if it is displayed like this on each console.

python3_app.py


127.0.0.1 - - [25/Jun/2020 08:19:04] "GET /test HTTP/1.1" 200 -

ngrok_http_3000


HTTP Requests                                                       
-------------                                                       
                                                                    
GET /test                      200 OK                      

4.3 Webhook settings

Go here and select a channel for the LINE Messaging API. https://developers.line.biz/console/

From ** Messaging API **> ** Webhook Setting ** Type in the Webhook URL.

webhooksetting

Click ** Verify ** and if ** Success ** appears, it's OK.

success

4.4 Operation check

I will actually send it.

Screenshot 2020-06-25 17.21.15.png

Perfect ✨

5. Conclusion

In addition to text reply, you can easily send videos, send images, quick replies, etc., so I will explain them next time.

There is also a sample source on pylinebot's github, so you may want to take a look. https://github.com/nanato12/pylinebot/tree/master/sample

Nanato when Twitter: @nanato12_dev Email: [email protected] Blog: https://blog.nanato12.info GitHub: https://github.com/nanato12

Recommended Posts

[Super easy] Let's make a LINE BOT with Python.
Let's make a GUI with python.
[For play] Let's make Yubaba a LINE Bot (Python)
Let's make a graph with python! !!
Let's make a shiritori game with Python
Let's make a web framework with Python! (1)
Let's make a web framework with Python! (2)
Make a Twitter trend bot with heroku + Python
Create a LINE BOT with Minette for Python
Let's replace UWSC with Python (5) Let's make a Robot
Make a LINE WORKS bot with Amazon Lex
Let's make a Discord Bot.
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
Make a LINE BOT (chat)
Make a fortune with Python
Make a morphological analysis bot loosely with LINE + Flask
[Let's play with Python] Make a household account book
Let's make a simple game with Python 3 and iPhone
I made a LINE BOT with Python and Heroku
If you want to make a discord bot with python, let's use a framework
[Python] Make your own LINE bot
Make a recommender system with python
Let's make a supercomputer with xCAT
Let's make a websocket client with Python. (Access token authentication)
Make a LINE bot with GoogleAppEngine / py. Simple naked version
Let's make a LINE bot using various services [ngrok edition]
[Python] Super easy test with assert statement
Make GUI apps super easy with tkinter
Let's create a free group with Python
[Python] [LINE Bot] Create a parrot return LINE Bot
Let's make a simple language with PLY 1
[Python] Let's make matplotlib compatible with Japanese
[LINE Messaging API] Create a BOT that connects with someone with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Let's make a tic-tac-toe AI with Pylearn 2
Let's make a combination calculation in Python
Let's make a web chat using WebSocket with AWS serverless (Python)!
Make a desktop app with Python with Electron
The story of making a university 100 yen breakfast LINE bot with Python
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
[Ev3dev] Let's make a remote control program by Python with RPyC protocol
I made a stamp substitute bot with line
Send a message to LINE with Python (LINE Notify)
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Try to make a "cryptanalysis" cipher with Python
LINE BOT with Python + AWS Lambda + API Gateway
[Python] Make a simple maze game with Pyxel
I made a LINE Bot with Serverless Framework!
Steps to create a Twitter bot with python
Try to make a dihedral group with Python
Read line by line from a file with Python
Make your Python environment "easy" with VS Code
I made a Mattermost bot with Python (+ Flask)
Make LINE BOT (Echolalia)
Make one repeating string with a Python regular expression.
I made a Twitter BOT with GAE (python) (with a reference)
I made a household account book bot with LINE Bot
[Python] How to draw a line graph with Matplotlib
Try to make a command standby tool with python
Let's make a Makefile and build it (super beginner)