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.
-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
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.
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.
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
.
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.
https://xxxxxxxxxxxx.ngrok.io
--Using Webhook: ValidThe 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.
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.
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.
LINE Developers - Documents - Messaging API LINE Messaging API SDK for Python How to use ngrok
Recommended Posts