This article is written with the following two prerequisites.
--Easy to understand even for beginners ――Be lean and concise
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
I wrote it in this article, so please refer to it.
This article uses Flask.
You can use Django.
$ pip install flask
$ pip install pylinebot
Download from the link below and set it so that you can use ngrok.
https://ngrok.com/
$ ngrok version
ngrok version 2.3.35
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?
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)
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                      
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.
|  | 
|---|
Click ** Verify ** and if ** Success ** appears, it's OK.
|  | 
|---|
I will actually send it.
|  | 
|---|
Perfect ✨
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