I tried to create an interactive slackbot with buttons in python using SW. If it is written by other people, it is usually quite complicated with node.js and I am not good at javascript, so I want to avoid it as much as possible and do it with python! So I tried to make it as simple as possible using flask + heroku + slack client. Slack bots with buttons have a reputation for being bothersome and popular, so I'm wondering if this article can be used as a reference to make it relatively quick.
The code is available here [https://github.com/sh-tatsuno/slackbot_python_server).
--Installing pyenv, pyenv-virtualenv --Register on heroku --A suitable slack account
Please prepare these in advance.
This is quite a hassle, but let's do our best.
--Creating a slack app Log in to here and create a new slack app. The App Name can be anything, but for Workspace, specify the team you want to use the bot with.
Then go to basic information. At this time, please save the following Verification Token.
Next, we will make settings for the bot. Set the following Interactive Messages and Permissions.
--Interactive Messages settings Specify the URL to redirect to when you press the button. This time we will redirect to'slack / json_html' on heroku.
--Permissions setting Let's set the required permissions from the Scope near the center of the page. Is it Send messages system for the time being? Next, let's issue an OAuth Token from Tokens for Your Workspace at the top of the page. Please keep this as well.
Now, let's make the server side from here.
--Creating a python environment
Create a project and install a virtualenv environment. The necessary libraries will also be installed at this time.
$ mkdir slackbot_python_server
$ cd slackbot_python_server
$ pyenv virtualenv 3.6.2 heroku_python_3.6.2
$ pyenv local heroku_python_3.6.2
$ pip install Flask gunicorn slackclient
$ echo .python-version >> .gitignore
$ pip freeze > requirements.txt
--Create a Procfile
$ echo web: gunicorn main:app --log-file=- > Procfile
--Create main.py
First, define the slack client and the flask server. SLACK_BOT_TOKEN and SLACK_VERIFICATION_TOKEN will be set later in heroku as environment variables.
main.py
from flask import Flask, request, make_response, Response
import os
import json
from slackclient import SlackClient
# Your app's Slack bot user token
SLACK_BOT_TOKEN = os.environ.get("SLACK_BOT_TOKEN")
SLACK_VERIFICATION_TOKEN = os.environ.get("SLACK_VERIFICATION_TOKEN")
# Slack client for Web API requests
slack_client = SlackClient(SLACK_BOT_TOKEN)
# Flask webserver for incoming traffic from Slack
app = Flask(__name__)
Next, define the attachments for the button post. At this time, set each element of the button in the action part. It seems that you can enter any id you like for call_back_id.
main.py
# your attachment
attachments_json = [
{
"fallback": "Upgrade your Slack client to use messages like these.",
"color": "#258ab5",
"attachment_type": "default",
"callback_id": "the_greatest_war",
"actions": [
{
"name": "choco1",
"text": "mushroom",
"value": "kinoko",
"type": "button"
},
{
"name": "choco2",
"text": "bambooshoot",
"value": "takenoko",
"type": "button"
}
]
}
]
Set the root part. When a request comes in with GET, slack_client.api_call is called and a post with a button is posted on the general channel. The element set earlier is entered in attachments_json.
main.py
#route
#when you access such as curl command, slackbot post interactive message
@app.route("/", methods=["GET"])
def index():
slack_client.api_call(
"chat.postMessage",
channel="#general",
text="Which school are you?",
attachments=attachments_json
)
return make_response("", 200)
Finally, create the redirect part when the button is pressed. Since form_json ["actions"] [0] ["value"] contains the value selected by the previous button, it branches based on this value and returns the statement.
main.py
#redirect from button
@app.route("/slack/json_html", methods=["POST"])
def json_html():
# Parse the request payload
form_json = json.loads(request.form["payload"])
val = form_json["actions"][0]["value"]
if val == "kinoko":
response_text = "All right, then it's a war"
else:
response_text = "All right, then ally"
response = slack_client.api_call(
"chat.postMessage",
channel="#general",
text=response_text,
attachments=[]
)
return make_response("", 200)
This completes main.py.
--git heroku settings
Set up git.
$ git init
$ git add .
$ git commit -m "first commit"
Create and configure heroku. In addition to creating a heroku project, set the environment variables of the Token obtained earlier in Slack.
$ heroku login
$ heroku create <app-name>
$ heroku config:set SLACK_BOT_TOKEN = "<The OAuth Token you just got>" SLACK_VERIFICATION_TOKEN = "<Verification Token acquired earlier>" --app "<app-name>"
Push the project to heroku with the following command.
$ git push heroku master
Now, send a request to the app given to heroku with the following command and check the operation.
$ curl https://<heroku apps>.herokuapp.com/
At this time, if the button is posted to general and it works, it is successful.
At first, I did it with python's slackbot and got stuck in a jar. I had a hard time with slackclient because there weren't many articles in Japanese, but when I looked it up, it seems that it can be selected by a list and various options can be made, so it seems to be quite convenient (probably the same as js). ). Please give it a try.
-Deploy Flask app on heroku -Create a Slack bot with Python's slackbot library -I tried using buttons and interactive messages in Slack's Botkit
Recommended Posts