[PYTHON] Try running Slack's (Classic) Bot with docker

This article is the 21st day article of Takumi Akashiro Alone Advent Calendar 2020.

at first

This year was a year when I thought I had to make a Slack bot, but When the machine breaks down in the future, it is troublesome to build the environment from scratch again. It is also dull to prepare an environment construction document for handing over, so I thought that I had to build an environment with Docker in a nice way and think about how to easily run it in anyone's environment.

That was about 23 hours ago.

I slept and woke up, started work, and started working to write this article at 20:00! Did you make it in time? It's an article because it was in time.

Preparing to make a Slack bot

This time I want to run it with RTM (a guy who can take events in real time), so I will make a Classic Bot. https://api.slack.com/apps?new_classic_app=1

Install Bot properly and create Bot token.

Preparation of various files

Prepare the following files.

This time, make it appropriately with the slackclient module. It's safe because it is prepared by Slack official.

src/requirements.txt


slackclient

I thought I'd write something, but I don't have time to spare at all Use the RTM API sample code provided by Slack officials. Real Time Messaging (RTM) — slackclient (Legacy Python Slack SDK)

src/app.py


import os
from slack import RTMClient

@RTMClient.run_on(event="message")
def say_hello(**payload):
  data = payload['data']
  web_client = payload['web_client']

  if 'Hello' in data['text']:
    channel_id = data['channel']
    thread_ts = data['ts']
    user = data['user'] # This is not username but user ID (the format is either U*** or W***)

    web_client.chat_postMessage(
      channel=channel_id,
      text=f"Hi <@{user}>!",
      thread_ts=thread_ts
    )

slack_token = os.environ["SLACK_BOT_TOKEN"]
rtm_client = RTMClient(token=slack_token)
rtm_client.start()

Dockerfile


FROM python:3.8

ENV SLACK_BOT_TOKEN <Bot token>
RUN mkdir src
copy src src/

RUN pip install -r ./src/requirements.txt

WORKDIR ./src/
ENTRYPOINT [ "python", "app.py" ]

It's like this image.png

Create a Docker image

docker build -t <imagename>: <tag> <DockerFile directory>

This time docker build -t aktk/slack_bot:latest d:\docker\slackbot

Cho easy.

Run Docker image

docker run -d -p 80:80 aktk/slack_bot

Hello in front of the Slack bot

21_01.gif Uooo! moved!

Tightening

It's easier than I expected and I'm surprised! !! I regret that I should have touched it sooner if it could be done so easily.

In the future, I would like to use it for environment construction + build of tools that require EXE conversion made by PySide2, but The container seems to be running on Linux, so I think I have to investigate more ...

Recommended Posts

Try running Slack's (Classic) Bot with docker
Try running cloudera manager with docker
Try WildFly with Docker
Try running MPLS-VPN with FR Routing on Docker
Try running MySql and Blazor with docker-compose
Try using Kong + Konga with Docker Compose.
Try building Express + PostgreSQL + Sequelize with Docker [Part 2]
Keep docker container running with no resident process running
Try building Express + PostgreSQL + Sequelize with Docker [Part 1]
Try using another Servlet container Jetty with Docker
Deploy Line bot with rails5 + Docker + Heroku Note
Try running an app made with Quarkus on Heroku
Feel free to try Elasticsearch cluster with WSL2 + Docker
Try connecting to AzureCosmosDB Emulator for Docker with Java
Run Mosquitto with Docker and try WebSocket communication with MQTT
Try running the Embulk command with your Lambda function
Try DI with Micronaut
Launch MariaDB with Docker
Rails deploy with Docker
Run Pico with docker
Explode Docker with WSL2
Use Puphpeteer with Docker
Operate Emby with Docker
Use ngrok with Docker
Run Payara with Docker
[Docker] Connection with MySQL
Php settings with Docker
Getting Started with Docker
Disposable PHP with Docker
Install Composer with Docker
docker-compose.yml when you want to keep mysql running with docker
I tried running WordPress with docker preview on M1 Mac.