Create a line chatbot that returns parrots. There are many other articles that can be helpful, but this time I will write including the ones I stumbled upon.
window7 64bit python 3.6.4 editor Atom (anything is fine) Library: heroku Flask line-bot-sdk
To put it simply, Heroku seems to be able to easily publish your own web application. It is necessary to make a line bot, so let's install it. Create an account and install Heroku. https://jp.heroku.com/
After installation and account creation, at the command prompt
heroku login
When you enter A screen like this will be displayed in your browser, so press "Log in". Then You can confirm that you have successfully logged in.
All you need runtime.txt requirements.txt Procfile main.py I will make four. Let's look at each one ・ Contents of runtime.txt
python-3.6.4
・ Contents of requirements.txt
Flask==1.1.2
line-bot-sdk==1.17.0
This is a copy and paste by typing pip freeze at the command prompt ・ Contents of Procfile (without extension)
web: python main.py
・main.py
from flask import Flask, request, abort
import os
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
#Get environment variables
YOUR_CHANNEL_ACCESS_TOKEN = os.environ["YOUR_CHANNEL_ACCESS_TOKEN"]
YOUR_CHANNEL_SECRET = os.environ["YOUR_CHANNEL_SECRET"]
line_bot_api = LineBotApi(YOUR_CHANNEL_ACCESS_TOKEN)
handler = WebhookHandler(YOUR_CHANNEL_SECRET)
@app.route("/")
def hello_world():
return "hello world!"
@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:
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__":
# app.run()
port = int(os.getenv("PORT"))
app.run(host="0.0.0.0", port=port)
You can use main.py as it is without editing (quote) Combine these files into one folder.
At the command prompt
heroku create *******
heroku config:set YOUR_CHANNEL_ACCESS_TOKEN="**********" --app **********
heroku config:set YOUR_CHANNEL_SECRET="**********" --app **********
Enter the part of ******* according to your environment. Enter the app name in the first "heroku create ***". YOUR_CHANNEL_ACCESS_TOKEN and YOUR_CHANNEL_SECRET are described in "Channel basic settings" and "Messaging API settings" of LINE developers. After that, enter the app name in "--app ****".
At the command prompt
git init
git add .
git commit -m "test commit"
git push heroku master
In order. Finally
heroku open
When you enter ...
If it can be displayed on the browser, it is successful.
After that, you can check the operation of the parrot return bot in the line app.
There were a lot of typos in the first place. .. It took a long time to resolve the error because the "Procfile" file was "Procfile.text". .. I don't notice a surprisingly simple mistake. If you don't know where the error is,
heroku log
You may find the cause of the error by typing.
cheer up!!
Recommended Posts