forked from slackapi/python-dialog-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
124 lines (105 loc) · 3.86 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
112
113
114
115
116
117
118
119
120
121
122
123
124
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 web server for incoming traffic from Slack
app = Flask(__name__)
# Dictionary to store coffee orders. In the real world, you'd want an actual key-value store
COFFEE_ORDERS = {}
# Send a message to the user asking if they would like coffee
user_id = "U0CAV5XME"
order_dm = slack_client.api_call(
"chat.postMessage",
as_user=True,
channel=user_id,
text="I am Coffeebot ::robot_face::, and I\'m here to help bring you fresh coffee :coffee:",
attachments=[{
"text": "",
"callback_id": user_id + "coffee_order_form",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [{
"name": "coffee_order",
"text": ":coffee: Order Coffee",
"type": "button",
"value": "coffee_order"
}]
}]
)
# Create a new order for this user in the COFFEE_ORDERS dictionary
COFFEE_ORDERS[user_id] = {
"order_channel": order_dm["channel"],
"message_ts": "",
"order": {}
}
@app.route("/slack/message_actions", methods=["POST"])
def message_actions():
# Parse the request payload
message_action = json.loads(request.form["payload"])
user_id = message_action["user"]["id"]
if message_action["type"] == "interactive_message":
# Add the message_ts to the user's order info
COFFEE_ORDERS[user_id]["message_ts"] = message_action["message_ts"]
# Show the ordering dialog to the user
open_dialog = slack_client.api_call(
"dialog.open",
trigger_id=message_action["trigger_id"],
dialog={
"title": "Request a coffee",
"submit_label": "Submit",
"callback_id": user_id + "coffee_order_form",
"elements": [
{
"label": "Coffee Type",
"type": "select",
"name": "meal_preferences",
"placeholder": "Select a drink",
"options": [
{
"label": "Cappuccino",
"value": "cappuccino"
},
{
"label": "Latte",
"value": "latte"
},
{
"label": "Pour Over",
"value": "pour_over"
},
{
"label": "Cold Brew",
"value": "cold_brew"
}
]
}
]
}
)
print(open_dialog)
# Update the message to show that we're in the process of taking their order
slack_client.api_call(
"chat.update",
channel=COFFEE_ORDERS[user_id]["order_channel"],
ts=message_action["message_ts"],
text=":pencil: Taking your order...",
attachments=[]
)
elif message_action["type"] == "dialog_submission":
coffee_order = COFFEE_ORDERS[user_id]
# Update the message to show that we're in the process of taking their order
slack_client.api_call(
"chat.update",
channel=COFFEE_ORDERS[user_id]["order_channel"],
ts=coffee_order["message_ts"],
text=":white_check_mark: Order received!",
attachments=[]
)
return make_response("", 200)
if __name__ == "__main__":
app.run()