Launch environment with LineBot + Heroku + Docker + Python

Overview

Mainly referenced pages

https://devcenter.heroku.com/articles/container-registry-and-runtime

https://github.com/line/line-bot-sdk-python

Build a server environment for linebot on Docker

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.

Folder structure

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.

Deployment method

Create an application on Heroku

https://www.heroku.com/home Remember the name of the application you created (application generation using cli is also OK)

Build a Docker image

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.

Push to Heroku and deploy

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

Setting environment variables

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

Launch environment with LineBot + Heroku + Docker + Python
Prepare python3 environment with Docker
Make Echolalia LINEbot with Python + heroku
Build Mysql + Python environment with docker
Build Jupyter Lab (Python) environment with Docker
Create Python + uWSGI + Nginx environment with Docker
Launch Flask application with Docker on Heroku
Use python with docker
Python environment with docker-compose
Virtual environment with Python 3.6
Prepare the execution environment of Python3 with Docker
[Python] Build a Django development environment with Docker
Create Nginx + uWSGI + Python (Django) environment with docker
Build PyPy and Python execution environment with Docker
Install Python environment with Anaconda
Japanese can be used with Python in Docker environment
Manage python environment with virtualenv
[Python] OpenCV environment construction with Docker (cv2.imshow () also works)
Build python3 environment with ubuntu 16.04
Launch a Python web application with Nginx + Gunicorn with Docker
Build python environment with direnv
From environment construction to deployment for flask + Heroku with Docker
Create a simple Python development environment with VSCode & Docker Desktop
Build python virtual environment with virtualenv
Setup modern Python environment with Homebrew
Create a virtual environment with Python!
Building a virtual environment with Python 3
Build PyPy execution environment with Docker
code-server online environment (5) Launch code-server on Docker
Python3 environment construction with pyenv-virtualenv (CentOS 7.3)
pytorch @ python3.8 environment construction with pipenv
Prepare Python development environment with Atom
Docker environment update: add Python package
Rebuild Django's development environment with Docker! !! !! !!
Data science environment construction with Docker
[docker] python3.5 + numpy + matplotlib environment construction
Application development with Docker + Python + Flask
Create a C ++ and Python execution environment with WSL2 + Docker + VSCode
Create a simple Python development environment with VS Code and Docker
python launch
[Pyenv] Building a python environment with ubuntu 16.04
[Ubuntu 18.04] Python environment construction with pyenv + pipenv
[Python] Building an environment with Anaconda [Mac]
Competitive programming with python Local environment settings
Change Python 64bit environment to 32bit environment with Anaconda
Building a Python3 environment with Amazon Linux2
[Development environment] Python with Xcode [With screen transition]
Use smbus with python3 under pyenv environment
I tried linebot with flask (anaconda) + heroku
Using venv in Windows + Docker environment [Python]
Behind the flyer: Using Docker with Python
[Python] Create a virtual environment with Anaconda
Let Heroku do background processing with Python
Building a Python 3.6 environment with Windows + PowerShell
Use cryptography library cryptography with Docker Python image
Created Ubuntu, Python, OpenCV environment on Docker
Easy web app with Python + Flask + Heroku
[Linux] Build a jenkins environment with Docker
Run a Python web application with Docker
Build a python virtual environment with pyenv
Clean python environment with pythonz and virtualenv