Long time no see guys, here's Xu from Odakyu Electric Railway co. ltd working on the Wooms project.
Before we start the Line bot developing, please make sure you've registered a Line account and create a project on
https://developers.line.biz/ja/services/messaging-api/ and issued the access-token and your channel secret.
Env
・Python3.10.3
・Ubuntu 22.04
・Heroku
・Messaging API
Installation
pip install line-bot-sdk
pip install flask
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
Directory
lineBot_app
├─ Procfile
├─ app.py
├─ runtime.txt
└─ requirements.txt
Heroku
About Heroku
A free app building, deploying, managing platform.
550h for free Dyons.
The time period during the app is in sleep mod or maintenance mod will be counted as well.
Docs:Heroku Architecture | Heroku Dev Center
Script app.py
import
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
import os
Connect Line bot with messaging API
line_bot_api = LineBotApi([YOUR_CHANNEL_ACCESS_TOKEN])
handler = WebhookHandler([YOUR_CHANNEL_SECRET])
Define app
app = Flask(__name__)
For checking app status
@app.route("/")
def hello_world():
return "hello world!"
Webhook
@app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'OK'
Write functions here
@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
#Repeating function for example
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text))
Runserver
if __name__ == "__main__":
app.run()
Procfile
web: python app.py
runtime.txt
python-3.10.3 #heroku supports 3.10.3, 3.10.5
requirements.txt
pip freeze > requirements.txt
Build and deploy
Login Heroku
heroku login -i
Create a new app
Push to Heroku
heroku git:clone -a [your-app-name]
cd [your-app-dir]
git add .
git commit -am "make it better"
git push heroku master
Add Webhook to line bot
https://[your-app-name].herokuapp.com/callback