forked from slackapi/python-message-menu-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
111 lines (89 loc) · 3.33 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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["SLACK_BOT_TOKEN"]
SLACK_VERIFICATION_TOKEN = os.environ["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__)
# Helper for verifying that requests came from Slack
def verify_slack_token(request_token):
if SLACK_VERIFICATION_TOKEN != request_token:
print("Error: invalid verification token!")
print("Received {} but was expecting {}".format(request_token, SLACK_VERIFICATION_TOKEN))
return make_response("Request contains invalid Slack verification token", 403)
# The endpoint Slack will load your menu options from
@app.route("/slack/message_options", methods=["POST"])
def message_options():
# Parse the request payload
form_json = json.loads(request.form["payload"])
# Verify that the request came from Slack
verify_slack_token(form_json["token"])
# Dictionary of menu options which will be sent as JSON
menu_options = {
"options": [
{
"text": "Cappuccino",
"value": "cappuccino"
},
{
"text": "Latte",
"value": "latte"
}
]
}
# Load options dict as JSON and respond to Slack
return Response(json.dumps(menu_options), mimetype='application/json')
# The endpoint Slack will send the user's menu selection to
@app.route("/slack/message_actions", methods=["POST"])
def message_actions():
# Parse the request payload
form_json = json.loads(request.form["payload"])
# Verify that the request came from Slack
verify_slack_token(form_json["token"])
# Check to see what the user's selection was and update the message accordingly
selection = form_json["actions"][0]["selected_options"][0]["value"]
if selection == "cappuccino":
message_text = "cappuccino"
else:
message_text = "latte"
response = slack_client.api_call(
"chat.update",
channel=form_json["channel"]["id"],
ts=form_json["message_ts"],
text="One {}, right coming up! :coffee:".format(message_text),
attachments=[] # empty `attachments` to clear the existing massage attachments
)
# Send an HTTP 200 response with empty body so Slack knows we're done here
return make_response("", 200)
# Send a Slack message on load. This needs to be _before_ the Flask server is started
# A Dictionary of message attachment options
attachments_json = [
{
"fallback": "Upgrade your Slack client to use messages like these.",
"color": "#3AA3E3",
"attachment_type": "default",
"callback_id": "menu_options_2319",
"actions": [
{
"name": "bev_list",
"text": "Pick a beverage...",
"type": "select",
"data_source": "external"
}
]
}
]
# Send a message with the above attachment, asking the user if they want coffee
slack_client.api_call(
"chat.postMessage",
channel="#python",
text="Would you like some coffee? :coffee:",
attachments=attachments_json
)
# Start the Flask server
if __name__ == "__main__":
app.run()