forked from slackapi/bolt-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteps_from_apps.py
More file actions
177 lines (153 loc) · 5.2 KB
/
steps_from_apps.py
File metadata and controls
177 lines (153 loc) · 5.2 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# ------------------------------------------------
# instead of slack_bolt in requirements.txt
import sys
sys.path.insert(1, "..")
# ------------------------------------------------
import logging
from slack_sdk import WebClient
from slack_sdk.web import SlackResponse
from slack_bolt import App, Ack
from slack_bolt.workflows.step import Configure, Update, Complete, Fail
logging.basicConfig(level=logging.DEBUG)
# export SLACK_SIGNING_SECRET=***
# export SLACK_BOT_TOKEN=xoxb-***
app = App()
@app.middleware # or app.use(log_request)
def log_request(logger, body, next):
logger.debug(body)
return next()
# https://api.slack.com/tutorials/workflow-builder-steps
def edit(ack: Ack, step, configure: Configure):
ack()
configure(
blocks=[
{
"type": "section",
"block_id": "intro-section",
"text": {
"type": "plain_text",
"text": "Create a task in one of the listed projects. The link to the task and other details will be available as variable data in later steps.",
},
},
{
"type": "input",
"block_id": "task_name_input",
"element": {
"type": "plain_text_input",
"action_id": "task_name",
"placeholder": {
"type": "plain_text",
"text": "Write a task name",
},
},
"label": {"type": "plain_text", "text": "Task name"},
},
{
"type": "input",
"block_id": "task_description_input",
"element": {
"type": "plain_text_input",
"action_id": "task_description",
"placeholder": {
"type": "plain_text",
"text": "Write a description for your task",
},
},
"label": {"type": "plain_text", "text": "Task description"},
},
{
"type": "input",
"block_id": "task_author_input",
"element": {
"type": "plain_text_input",
"action_id": "task_author",
"placeholder": {
"type": "plain_text",
"text": "Write a task name",
},
},
"label": {"type": "plain_text", "text": "Task author"},
},
]
)
def save(ack: Ack, view: dict, update: Update):
state_values = view["state"]["values"]
update(
inputs={
"taskName": {
"value": state_values["task_name_input"]["task_name"]["value"],
},
"taskDescription": {
"value": state_values["task_description_input"]["task_description"][
"value"
],
},
"taskAuthorEmail": {
"value": state_values["task_author_input"]["task_author"]["value"],
},
},
outputs=[
{"name": "taskName", "type": "text", "label": "Task Name", },
{
"name": "taskDescription",
"type": "text",
"label": "Task Description",
},
{
"name": "taskAuthorEmail",
"type": "text",
"label": "Task Author Email",
},
]
)
ack()
pseudo_database = {}
def execute(step: dict, client: WebClient, complete: Complete, fail: Fail):
try:
complete(
outputs={
"taskName": step["inputs"]["taskName"]["value"],
"taskDescription": step["inputs"]["taskDescription"]["value"],
"taskAuthorEmail": step["inputs"]["taskAuthorEmail"]["value"],
}
)
user: SlackResponse = client.users_lookupByEmail(
email=step["inputs"]["taskAuthorEmail"]["value"]
)
user_id = user["user"]["id"]
new_task = {
"task_name": step["inputs"]["taskName"]["value"],
"task_description": step["inputs"]["taskDescription"]["value"],
}
tasks = pseudo_database.get(user_id, [])
tasks.append(new_task)
pseudo_database[user_id] = tasks
blocks = []
for task in tasks:
blocks.append(
{
"type": "section",
"text": {"type": "plain_text", "text": task["task_name"]},
}
)
blocks.append({"type": "divider"})
home_tab_update: SlackResponse = client.views_publish(
user_id=user_id,
view={
"type": "home",
"title": {"type": "plain_text", "text": "Your tasks!"},
"blocks": blocks,
},
)
except Exception as err:
fail(error={
"message": "Something wrong!"
})
app.step(
callback_id="copy_review",
edit=edit,
save=save,
execute=execute,
)
if __name__ == "__main__":
app.start(3000) # POST http://localhost:3000/slack/events