This article is the 21st day article of Takumi Akashiro Alone Advent Calendar 2020.
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 can't make one of the Docker Images by the end of 2020, so let's find out to overcome it. Don't leave this grudge in 2021 (exaggeration)
& mdash; Takumi Akagi (@takumi_akashiro) December 20, 2020
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.
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
.
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
docker build -t <imagename>: <tag> <DockerFile directory>
This time
docker build -t aktk/slack_bot:latest d:\docker\slackbot
Cho easy.
docker run -d -p 80:80 aktk/slack_bot
Uooo! moved!
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