I made LINE-bot with Python + Flask + ngrok + LINE Messaging API

Introduction

As the title says, I created a LINE-bot with a simple timer function using Python + Flask + ngrok + LINE Messaging API. It can be created in a short time, so if you are interested, please feel free to challenge.

environment

procedure

① LINE Developers --Create channels

-Log in with your LINE account, etc. at LINE Developers --Select Create Provider and enter any name --Select Messaging API in channel settings --Enter each item (hereinafter, required items) --Channel type: Messaging API --Provider: The provider name you entered earlier --Channel name: LINE in-app display name --Channel description: Brief function description, creator name, etc. --Large industry: e.g. Individual --Small industry: e.g. Student --Created by agreeing to the terms of use --When you have completed the channel registration, make a note of the following two points. --Messaging API settings-> Channel access token (issued if blank) --Channel basic settings-> Channel secret

② Construction of virtual environment

This time, we will use venv to build a virtual environment with the name lineVenv in the directory Workspace.

% mkdir Workspace
% cd Workspace
% python3 -m venv lineVenv

After executing the following command, if there is a notation (lineVenv) at the beginning of the terminal line, it is successful.

% . lineVenv/bin/active

Then use pip to install Flask and LINE Messaging API SDK for Python. The required packages can be installed with the following command.

% pip3 install flask
% pip3 install line-bot-sdk

This completes the construction of the virtual environment.

③ Implementation of bot program

By the way, a bot program with a timer function while referring to README.rst in GitHub of the LINE Messaging API for SDK installed earlier. Will be implemented. Create ʻapp.py` in the previous directory and write as follows.

app.py


from flask import Flask, request, abort

from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    MessageEvent, TextMessage, TextSendMessage,
)

from time import time
from datetime import timedelta

app = Flask(__name__)

line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')  #Channel access token
handler = WebhookHandler('YOUR_CHANNEL_SECRET')  #Channel secret

@app.route("/callback", methods=['POST'])
def callback():
    # get X-Line-Signature header value
    signature = request.headers['X-Line-Signature']

    # get request body as text
    body = request.get_data(as_text=True)
    app.logger.info("Request body: " + body)

    # handle webhook body
    try:
        handler.handle(body, signature)
    except InvalidSignatureError:
        print("Invalid signature. Please check your channel access token/channel secret.")
        abort(400)

    return 'OK'

user = {}
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
    id = event.source.user_id   #Obtaining a LINE user ID
    if event.message.text == "start":

        if not id in user:  #For new users
            user[id] = {}
            user[id]["total"] = 0

        user[id]["start"] = time()  #get the time when start is executed

        totalTimeStr = timedelta(seconds = user[id]["total"])  #s -> h:m:s

        reply_message = f"Start Timer\n\nTotal: {totalTimeStr}"

    elif event.message.text == "stop":
        end = time()  #end Get run time
        dif = int(end - user[id]["start"])  #Get elapsed time
        user[id]["total"] += dif  #Update total time

        timeStr = timedelta(seconds = dif)
        totalTimeStr = timedelta(seconds = user[id]["total"])

        reply_message = f"Stop Timer\n\nTime: {timeStr}s\nTotal: {totalTimeStr}"

    elif event.message.text == "reset":
        user[id]["total"] = 0 #Reset total time to 0

        totalTimeStr = timedelta(seconds = user[id]["total"])

        reply_message = f"Reset Timer\n\nTotal: {totalTimeStr}"

    else:
        reply_message = "Please send \"start\" or \"stop\"or \"reset\""  #Response to 3 unspecified words

    line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=reply_message))


if __name__ == "__main__":
    app.run()

It looks like it's done, but there are two more changes to the following:

line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')  #Channel access token
handler = WebhookHandler('YOUR_CHANNEL_SECRET')  #Channel secret

What you need here is the channel access token and channel secret that you wrote down in ①. Please copy and paste to YOUR_CHANNEL_ACCESS_TOKEN and YOUR_CHANNEL_SECRET respectively.

⑤ Execution

Now, let's move on to execution. If you run flask run in the virtual environment, you will see the following.

% flask run
* Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)

There is an address of http://127.0.0.1:5000, but since it is localhost, it cannot be accessed from outside the LAN. (That is, it cannot be accessed from the LINE server and the webhook cannot be used) There is a way to deploy it, but when I was looking for another method, a tool called ngrok seemed to be useful.

How to use ngrok https://qiita.com/hirokisoccer/items/7033c1bb9c85bf6789bd

So open a terminal different from the one running flask run and install it with the following command.

% brew install ngrok

You can also download the executable file from ngrok official website.

After the installation is complete, run ngrok http 5000 and you will see the following:

% ngrok http 5000

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

You can now access http://127.0.0.1:5000 via the public URL https://xxxxxxxxxxxx.ngrok.io.

⑥ LINE Developers --Webhook settings ・ Response settings

Now that we have the URL for the webhook, we'll return to LINE Developers. Change the following two points in Messaging API Settings-> Webhook Settings.

The following two points of the LINE official account function will be changed.

--Allow participation in group / multi-person chat: Invalid --Response message: Invalid

This is the end of all the steps.

test

Add LINE friends from the following and send the request specified in the program. Messaging API Settings-> QR Code / Bot Basic ID

Here is a bot that reproduces the contents of this article. (Deployed) You can refer this if you like.

bonus

Heroku (free version) was not suitable for deploying the LINE-bot created this time. Since the public port number is not fixed on Heroku, the process of making multiple requests and grouping them is not stable. PythonAnywhere worked well, provided it was free and stable.

Reference site

LINE Developers - Documents - Messaging API LINE Messaging API SDK for Python How to use ngrok

Recommended Posts

I made LINE-bot with Python + Flask + ngrok + LINE Messaging API
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 Mattermost bot with Python (+ Flask)
I made blackjack with python!
I made blackjack with Python.
I made wordcloud with Python.
I made a LINE BOT with Python and Heroku
[LINE Messaging API] Create parrot return BOT with Python
SNS Python basics made with Flask
I made a Line-bot using Python!
I made a simple book application with python + Flask ~ Introduction ~
I made a fortune with Python.
I made a daemon with Python
Made "Unofficial Apple Refurbished Product Introduction" BOT with LINE Messaging API (v2) + API Gateway + lambda (python)
I made a Nyanko tweet form with Python, Flask and Heroku
[LINE Messaging API] Create a BOT that connects with someone with Python
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
I tried linebot with flask (anaconda) + heroku
Simple Slack API client made with Python
I made a character counter with Python
[Python] Quickly create an API with Flask
I made a Hex map with Python
Serverless face recognition API made with Python
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
How to make an artificial intelligence LINE bot with Flask + LINE Messaging API
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I made my dog "Monaka Bot" with LineBot
I made a Twitter fujoshi blocker with Python ①
Procedure for creating a LineBot made with Python
[Python] I made a Youtube Downloader with Tkinter.
LINE BOT with Python + AWS Lambda + API Gateway
I made a LINE Bot with Serverless Framework!
I made a bin picking game with Python
I made a Python Qiita API wrapper "qiipy"
Decision support system for elderly people made with LINE Messaging API + Serverless Framework
Programming with Python Flask
[AWS] I made a reminder BOT with LINE WORKS
I made a household account book bot with LINE Bot
I made a Christmas tree lighting game with Python
[LINE Messaging API] Create a rich menu in Python
I made a net news notification app with Python
I made a Python3 environment on Ubuntu with direnv.
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client
Various memorandums when using sdk of LINE Messaging API with Python (2.7.9) + Google App Engine
I tried fp-growth with python
Use Trello API with python
When I made CaboCha usable with python3, I got stuck (Windows 10)
I made a LINE BOT that returns parrots with Go
Use Twitter API with Python
I made a package to filter time series with python
API with Flask + uWSGI + Nginx
SNS Flask (Ajax) made with Flask
I tried follow management with Twitter API and Python (easy)
Web API with Python + Falcon