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.
First, log in to LINE Developers with your LINE account.
When you log in, the Developers account registration screen will be displayed. --Developer name
When account registration is completed
A page like this will appear, click Create New Provider. When you enter the provider name
Is displayed, select the Messaging API. When this screen is displayed, --Channel name --Channel description --Large industries --Small industry
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.
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. The app name you enter at this time should not be shared with other people's, so please think carefully. After creating the app, enter the url written in Domains of Setting with / callback in the webhook setting of LINE Messaging API.
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 If Build succeeded is displayed, it is successful.
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.
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.
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)
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!
https://developers.line.biz/ja/docs/messaging-api/ https://github.com/line/line-bot-sdk-python
Recommended Posts