Many articles have written about how to create a linebot, but as a beginner I had a stumbling block, so I made up for it and made it easy for me to understand. Since this is an article I made for myself, there are many points that are difficult to understand, but please do your best while referring to other articles.
$mkdir lineBotTest
$cd lineBotTest
Enter 3. Launch TextEdit in your application 4, select the format in the top bar, select standard text, copy and paste the following No need to change such as'LINE_CHANNEL_SECRET' Copy as it is
import os
import sys
from argparse import ArgumentParser
from flask import Flask, request, abort
from linebot import (
LineBotApi, WebhookHandler
)
from linebot.exceptions import (
InvalidSignatureError
)
from linebot.models import (
MessageEvent, TextMessage, TextSendMessage,
)
app = Flask(__name__)
"""
heroku config:set LINE_CHANNEL_SECRET="Channel secret" --app app name
Is typed into the terminal, so the channel secret is automatically entered in the following code every time.
The access token is the same, so there is no need to change it.
"""
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
"""
Null channel secret or access token(nothing)Processing in case of
"""
if channel_secret is None:
print('Specify LINE_CHANNEL_SECRET as environment variable.')
sys.exit(1)
if channel_access_token is None:
print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.')
sys.exit(1)
"""
Get tokens etc. from Heroku variables
"""
line_bot_api = LineBotApi(channel_access_token)
handler = WebhookHandler(channel_secret)
"""
Webhook from LINE
Basic functions that do not change
"""
@app.route("/callback", methods=['POST'])
def callback():
#Get the value for signature verification from the request header
signature = request.headers['X-Line-Signature']
#Get request body
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
#Validate the signature and call the function defined in handle if there is no problem
try:
handler.handle(body, signature)
except InvalidSignatureError:#Returns 400 when an error occurs
abort(400)
return 'OK'
"""
Massage Event on LINE(When a text message is sent)Called when
line_bot_api.reply_event as the first argument of message.reply_token is used to respond to events
The second argument is linebot.Pass the TextSendMessage object for reply defined in models
"""
@handler.add(MessageEvent, message=TextMessage)
def message_text(event):
#What you entered(event.message.text)Reply according to
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text)
)
if __name__ == "__main__":#Identify if it is running with the correct file extension
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)
Name it main.py. Also, save it in lineBotTest at this point It should be like this.
Confirm that it is lineBotTest user $ in the terminal, and enter the following command
$brew tap heroku/brew && brew install heroku
$heroku login
//Login starts when you press a button other than q
$heroku create app name
//Use an app name created by another person and a unique name that does not cover
//heroku create linetest0000 Use linetest0000 this time
$heroku addons:create fixie:tricycle --app linetest0000
//Addition of "Fixie" add-on
$pipenv install flask
$pipenv install line-bot-sdk
//"Pipfile" and "Pipfile" in the folder."lock" is generated.
/*
Enter when doing web scraping
$pipenv install bs4
$pipenv install requests
*/
$heroku config:set LINE_CHANNEL_SECRET="Channel secret" --app linetest0000
//Enter the first acquired channel secret
$heroku config:set LINE_CHANNEL_ACCESS_TOKEN="Access token (long term)" --app linetest0000
//Enter the access token you got first
$echo web: python main.py > Procfile
//Profile creation
$heroku git:remote --app linetest0000
$git init
$git add -A
$git commit -m "Enter a comment"
$git push heroku master
git add -A The following command is a command to type to reflect when the code is changed
If you use import in your code, use Procfile
$pipenv install library name
Enter Enter Procfile with a command instead of opening it
When it doesn't move
$heroku logs -t
You can see the log at. This usually works.
This completes
Recommended Posts