Make Echolalia LINEbot with Python + heroku

Introduction

This time I'm going to create an Echolalia bot using Python and heroku. This article is for people who have a good environment for python3 and homebrew. If you don't have an environment yet https://ai-inter1.com/python-install/ https://qiita.com/balius_1064/items/ac7dff5ef10eaf69996f Please refer to the article etc.

Development environment

LINE Developers settings

First, log in to LINE Developers with your LINE account. スクリーンショット 2020-05-09 13.10.51.jpg

When you log in, the Developers account registration screen will be displayed. --Developer name

When account registration is completed スクリーンショット 2020-05-09 13.14.17.jpg

A page like this will appear, click Create New Provider. When you enter the provider name スクリーンショット 2020-05-09 13.16.31.jpg

Is displayed, select the Messaging API. スクリーンショット 2020-05-09 13.17.40.jpg When this screen is displayed, --Channel name --Channel description --Large industries --Small industry

installation of line-bot-sdk and flask

pip install flask
pip install line-bot-sdk

Do the above two to line-bot-sdk and flask /) Install.

mkdir line-bot
cd line-bot

Then open a terminal, create a directory for linebot and change to that directory.

Create a configuration file

runtime.txt


python-3.7.3

requirements.txt


Flask==1.1.1
line-bot-sdk==1.8.0

Procfile


web: python main.py

--runtime.txt ・ ・ ・ Describe the Python version --requirements.txt ・ ・ ・ Describe the library to be installed --Procfile ・ ・ ・ Describes how to execute the program

If you can set up so far, it is time to create a python program.

main.py


#Library import
from flask import Flask, request, abort
from linebot import (
    LineBotApi, WebhookHandler
)
from linebot.exceptions import (
    InvalidSignatureError
)
from linebot.models import (
    FollowEvent, MessageEvent, TextMessage, TextSendMessage, ImageMessage, ImageSendMessage, TemplateSendMessage, ButtonsTemplate, PostbackTemplateAction, MessageTemplateAction, URITemplateAction
)
import os
#Lightweight web application framework:Flask
app = Flask(__name__)

#LINE Acces Token
LINE_CHANNEL_ACCESS_TOKEN = "LINE_CHANNEL_ACCESS_TOKEN"
#LINE Channel Secret
LINE_CHANNEL_SECRET = "LINE_CHANNEL_SECRET"

line_bot_api = LineBotApi(LINE_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(LINE_CHANNEL_SECRET)

@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'

@handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
	line_bot_api.reply_message(
        event.reply_token,
        TextSendMessage(text=event.message.text )
     )

if __name__ == "__main__":
    port = os.getenv("PORT")
    app.run(host="0.0.0.0", port=port)

Create main.py like this.

#LINE Acces Token
LINE_CHANNEL_ACCESS_TOKEN = "LINE_CHANNEL_ACCESS_TOKEN"
#LINE Channel Secret
LINE_CHANNEL_SECRET = "LINE_CHANNEL_SECRET"

For the LINE Acces Token and LINE Channel Secret part, go to the LINE bot channel setting page that you created first, check your own, and copy and paste. スクリーンショット 2020-05-09 13.56.16.jpg スクリーンショット 2020-05-09 13.56.42.jpg

Heroku settings

First, register on Heroku. (It was hard for me who is not good at English) After registration is complete, click create new app to create the app. スクリーンショット 2020-05-09 14.10.55.jpg The app name you enter at this time should not be shared with other people's, so please think carefully. スクリーンショット 2020-05-09 14.11.15.jpg After creating the app, enter the url written in Domains of Setting with / callback in the webhook setting of LINE Messaging API. スクリーンショット 2020-05-09 14.15.34.jpg スクリーンショット 2020-05-09 14.17.25.jpg

Then at the terminal

brew install heroku

Please run the. Next, configure the settings to push to heroku.

heroku login

And press the enter key to launch the browser. Therefore, please log in by entering the email address and password you registered earlier.

Logged in as [Registered email address]

Is displayed, it is successful.

git init

heroku git:remote -a (app name)

git add .

git commit -m "first commit"

git push heroku master

Deploying is completed by executing the above commands in order. At heroku's Activity スクリーンショット 2020-05-09 14.50.17.jpg If Build succeeded is displayed, it is successful.

Setting environment variables

heroku config:set LINE_CHANNEL_ACCESS_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXX

heroku config:set LINE_CHANNEL_SECRET=XXXXXXXXXXXXXXXXXXXXX

Next, execute the above two in the terminal to set the environment variables. This is the same as the LINE Acces Token and LINE Channel Secret you entered in main.py earlier, so copy and execute it.

Complete

Let's send appropriate characters to the bot you added earlier with your LINE app! If you get the same content as the characters you sent, you are successful. hoge.jpg

If you can read it but it doesn't work, go to the terminal

heroku logs --tail

If you enter, you can see the log in real time, so please investigate the cause there. (Often the LINE messaging API has disabled webhooks)

Summary

This time, I created a LINEbot that will give you back, but you can improve this source code and create your own LINEbot! Also, Rich Menu and LIFF App It is also possible to create a more luxurious LINE bot by adding liff / developing-liff-apps /). Please try that too!

reference

https://developers.line.biz/ja/docs/messaging-api/ https://github.com/line/line-bot-sdk-python

Recommended Posts

Make Echolalia LINEbot with Python + heroku
Launch environment with LineBot + Heroku + Docker + Python
Make a Twitter trend bot with heroku + Python
Make Puyo Puyo AI with Python
Make a fortune with Python
Make apache log csv with python
Let's make a GUI with python.
Make a recommender system with python
Let's make a graph with python! !!
Make the Python console covered with UNKO
Let's make a shiritori game with Python
I tried linebot with flask (anaconda) + heroku
Fractal to make and play with Python
Let Heroku do background processing with Python
Let's make a voice slowly with Python
Make pypy submission easier with atcoder-cli (python)
[Python] Let's make matplotlib compatible with Japanese
Easy web app with Python + Flask + Heroku
Let's make a web framework with Python! (1)
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Let's make a web framework with Python! (2)
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
LINE heroku python
Bingo with python
Zundokokiyoshi with python
LINE-Bot [0] in Python
Excel with Python
Microcomputer with Python
Cast with python
How to upload with Heroku, Flask, Python, Git (4)
Make Python scripts into Windows-executable .exes with Pyinstaller
[Python] Make a game with Pyxel-Use an editor-
I want to make a game with Python
Procedure for creating a LineBot made with Python
Try to make a "cryptanalysis" cipher with Python
Make OpenCV3 available from python3 installed with pyenv
Make your own module quickly with setuptools (python)
[Python] Make a simple maze game with Pyxel
Let's replace UWSC with Python (5) Let's make a Robot
Try to make a dihedral group with Python
Until you use PhantomJS with Python on Heroku
[Python] Expression (1,2) does not make tuples with parentheses
Make JSON into CSV with Python from Splunk
Make Python built with jhbuild work on OSX
Make your Python environment "easy" with VS Code
[# 1] Make Minecraft with Python. ~ Preliminary research and design ~
Serial communication with Python
Make one repeating string with a Python regular expression.
Zip, unzip with python