2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Deploy a LineBot with Flask and Heroku

Last updated at Posted at 2022-07-08

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

image.png

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

image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?