It's a long time ago, but I suddenly decided to create a LINE Bot, I just made a LINE Bot that returns parrots.
Basically, you should be able to register without any problems according to the official page below. [To use Messaging API | LINE Developers](https://developers.line.biz/ja/docs/messaging-api/getting-started/#%E3%83%81%E3%83%A3%E3 % 83% 8D% E3% 83% AB% E3% 81% AE% E4% BD% 9C% E6% 88% 90)
![Screenshot 2020-05-12 20.48.49.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/130414/83b70611-c51f-d7f0-ca71 -6389eac33f13.png)
If the channel can be registered safely, it will be in a state like ↓
Check the following two as you will need it later ・ Channel secret ← Located in the Basic Setting tab ・ Channel access token (long-lived) ← Located in the Messaging API tab
2.Heroku
Basically, you should be able to register without any problems according to the official page below. Heroku Dev Center
![Screenshot 2020-05-12 21.14.40.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/130414/add1067e-c321-8ba2-a348 -3dcf648916be.png)
Download and install the Heroku CLI from the following page The Heroku CLI | Heroku Dev Center
![Screenshot 2020-05-12 21.21.03.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/130414/a3ee0f63-8bb9-a06e-418e -27c04e72a1b5.png)
After a successful installation, you should be able to use the heroku command in your terminal.
Create a file with the following configuration
main.py The main part of the program
main.py
# -*- coding: utf-8 -*-
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
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__)
# get channel_secret and channel_access_token from your environment variable
channel_secret = os.getenv('LINE_CHANNEL_SECRET', None)
channel_access_token = os.getenv('LINE_CHANNEL_ACCESS_TOKEN', None)
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)
line_bot_api = LineBotApi(channel_access_token)
handler = WebhookHandler(channel_secret)
@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 message_text(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text)
)
if __name__ == "__main__":
port = int(os.getenv("PORT", 5000))
app.run(host="0.0.0.0", port=port)
Procfile How to run the program
web: python main.py
requirements.txt Module to use
Flask==0.12.2
line-bot-sdk==1.8.0
runtime.txt Listed python version
python-3.6.6
Execute the following command from a terminal etc.
$ cd line-bot
$ git init
$ git config user.name "name"
$ git config user.email email address
$ git add .
$ git commit -m "comment"
$ cd line-bot: Move to root directory $ git init: Initialize git repository $ git config user.name "name": config config $ git config user.email email address: config settings $ git add.: Add $ git commit -m "comment": commit
Execute the following command to log in to Heroku
$ heroku login
When you execute it, it will be in the following state, so if you press any key, the login screen will be displayed on the browser, so Click the Log In button to log in
Execute the following command to create and deploy an application on Heroku
$heroku create application name
$ heroku config:set LINE_CHANNEL_SECRET="Channel Secret" --app application name
$ heroku config:set LINE_CHANNEL_ACCESS_TOKEN="Access token" --app application name
$ git push heroku master
The application name is arbitrary Set the "Channel Secret" and "Access Token" that you confirmed when you created the channel with LINE Developers.
Deployment may fail if the build pack is not set. In that case, execute the following command and set the build pack
$ heroku buildpacks:set heroku/python
Set the webhook settings for the channel created from the Line Developers console Use Webhook and specify the following URL as the webhook URL
https://Application name.herokuapp.com/callback
When the work up to this point is completed, the bot that returns the parrot is completed! I was a beginner who had never used Heroku, Git, or Messaging API, but it was surprisingly easy to create. I wish I could make something based on this in the future
Recommended Posts