Mainly referenced pages
https://devcenter.heroku.com/articles/container-registry-and-runtime
https://github.com/line/line-bot-sdk-python
I think that if you deploy to normal Heroku, you push requirement.txt and procfile, but if you can create an environment with Docker, it will be easier to use it, so build an environment using Docker. I do.
linebot │ .dockerignore │ Dockerfile.heroku │ └─app │ bot.py
Dockerfile.heroku
FROM kyobad/miniconda3-alpine:latest
MAINTAINER K.Kato
RUN pip install --upgrade pip \
&& conda install -y flask \
&& pip install flask gunicorn line-bot-sdk \
&& adduser -D botter \
&& mkdir /home/botter/app
USER botter
COPY ./app /home/botter/app
WORKDIR /home/botter/app
CMD gunicorn -b 0.0.0.0:$PORT bot:app --log-file=-
bot.py
###This is a copy of the official SDK###
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__)
line_bot_api = LineBotApi('YOUR_CHANNEL_ACCESS_TOKEN')
handler = WebhookHandler('YOUR_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 handle_message(event):
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=event.message.text))
if __name__ == "__main__":
app.run()
In Dockerfile, we are building the environment for ourselves. You can change it freely, but be aware that Heroku will not recognize the port unless you set it to $ PORT
.
https://www.heroku.com/home Remember the name of the application you created (application generation using cli is also OK)
From here, I will work with cli
First, log in to the registry with heroku container: login
Next, create an image with docker -f Dockerfile.heroku build
. The image name will be changed later, so feel free to name it.
After creating the image, rewrite the image name to push to Heroku
docker tag <image> registry.heroku.com/<app>/<process-type>
Here, <app>
is the application name on heroku, and <process-type>
is OK.
And docker push registry.heroku.com/ <app> / <process-type>
This will launch the container with the specified URL! Convenient! But it still doesn't work anywhere
Since the access key and secret key are not solid in the code, set using heroku's cli
heroku config:set ACCESS_TOKEN=hogehoge --app appname
And so on, I'll set environment variables from the console.
When this is done, the parrot return should be completed
Recommended Posts