-
Notifications
You must be signed in to change notification settings - Fork 0
/
bolt.py
297 lines (238 loc) · 9.54 KB
/
bolt.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
===========================================
Module: Slack bot Bolt app
===========================================
"""
import asyncio
import pprint
import copy
from slack_bolt import App
from slack_sdk.errors import SlackApiError
from slack_bolt.adapter.socket_mode import SocketModeHandler
import utils.slack_utils as slack_utils
from utils.general import env_var
# from bots.choose_team import run_bot_async as bot_choose_team
from bots.code_qa import run_bot_async as bot_code_qa
from bots.docs_qa import run_bot_async as bot_docs_qa
# from bots.github_qa import run_bot_async as bot_github_qa
from bots.structured_log import bot_log, update_reactions, BotLogEntry
# Install the Slack app and get xoxb- token in advance
app = App(
token=env_var("SLACK_BOT_TOKEN"),
signing_secret=env_var("SLACK_BOT_SIGNING_SECRET"),
)
bot_query_word_code = "[code] "
bot_query_word_docs = "[docs] "
bot_query_word_team = "[team] "
bot_query_word_gh_issues = "[gh-issues] "
pp = pprint.PrettyPrinter(indent=2)
hitl_config = {"enabled": False, "qa_channel": env_var("REVIEW_CHANNEL_ID")}
@app.command("/hello-socket-mode")
def hello_command(ack, body):
user_id = body["user_id"]
ack(f"Hi, <@{user_id}>!")
@app.event("app_mention")
def event_test(say):
say("Hi there!")
@app.event("message")
def handle_message_events(ack, say, body, logger):
print(f"-- incoming slack message event payload --")
# pp.pprint(body)
src_evt_context = slack_utils.get_event_context(body)
evt = body["event"]
if evt.get("type") == "message":
if evt.get("subtype") == "message_deleted":
logger.info(f"Ignoring message_deleted event.")
clean_text = text = evt.get("text", "")
print(f"user input: {text}")
if not evt.get("thread_ts"):
# message is not in a thread
bot_name = None
if text.startswith(bot_query_word_docs):
bot_name = "docs"
clean_text = text.replace(bot_query_word_docs, "")
elif text.startswith(bot_query_word_code):
bot_name = "code"
clean_text = text.replace(bot_query_word_code, "")
elif text.startswith(bot_query_word_team):
bot_name = "team"
clean_text = text.replace(bot_query_word_team, "")
elif text.startswith(bot_query_word_gh_issues):
bot_name = "gh-issues"
clean_text = text.replace(bot_query_word_gh_issues, "")
elif not text.startswith("["):
bot_name = "docs"
first_thread_ts = say(text="Thinking...", thread_ts=evt.get('ts'))
entry = BotLogEntry(
slack_context= src_evt_context,
elapsed_ms= 0,
step_name= 'select_bot',
payload= {"user_input": clean_text, "bot_name": bot_name}
)
bot_log(entry)
if bot_name == "docs":
asyncio.run(bot_docs_qa(app, hitl_config, say, body, clean_text, first_thread_ts))
elif bot_name == "code":
asyncio.run(bot_code_qa(app, hitl_config, say, body, clean_text))
# elif bot_name == "team":
# asyncio.run(bot_choose_team(app, hitl_config, say, body, clean_text))
# elif bot_name == "gh-issues":
# asyncio.run(bot_github_qa(app, hitl_config, say, body, clean_text))
elif not text.startswith("["):
asyncio.run(bot_docs_qa(app, hitl_config, say, body, clean_text))
else:
print(f"Unknown query type: '{text}'")
@app.action("approve_button")
def update_message(ack):
ack()
print(f"approve button pressed")
def handle_reaction_events(event):
# TODO: check if in channel before getting current message reactions
try:
message_info = app.client.reactions_get(
channel=event["item"]["channel"],
timestamp=event["item"]["ts"]
)
reactions = message_info["message"].get("reactions", [])
# print(f"Current reactions: {reactions}")
if reactions != None:
item_context = slack_utils.get_reaction_item_context(event)
update_reactions(item_context, reactions)
except SlackApiError as e:
print(f"Error fetching reactions: {e}")
@app.event("reaction_added")
def handle_reaction_added(event, say):
handle_reaction_events(event)
@app.event("reaction_removed")
def handle_reaction_removed(event, say):
handle_reaction_events(event)
@app.action("[team][choose][confirm]")
def handle_team_choose_confirm(ack, body, logger):
print(f"ack - action_id:'[team][choose][confirm]'")
# Acknowledge the interaction
ack()
# Log the body of the interaction payload
pp.pprint(body)
# logger.info(body)
user_id = body["user"]["id"]
# Check if the current user is a Slack workspace admin
try:
user_info = app.client.users_info(user=user_id)
is_admin = user_info["user"]["is_admin"]
except SlackApiError as e:
logger.error(f"Error fetching user info: {e}")
is_admin = False
if is_admin:
print(f"User {user_id} is an admin.")
else:
print(f"User {user_id} is not an admin.")
# Send a reply to the thread confirming that the message action was successful
try:
app.client.chat_postMessage(
channel=body["channel"]["id"],
thread_ts=body["message"]["ts"],
user_id=user_id,
text=f"<@{user_id}> gave the thumbs-up.\nI'll send this reply to <source workspace>-<thread_ts>",
)
except SlackApiError as e:
logger.error(f"Error sending confirmation: {e}")
@app.action("docs|qa|approve_reply")
def handle_action_docs_qa_approve_reply(ack, body, logger):
action = next(
(
action
for action in body.get("actions", [])
if action.get("action_id") == "docs|qa|approve_reply"
),
None,
)
if action is None:
logger.error("No action with action_id 'docs|qa|approve_reply' found.")
return
# Split action.value on "|" and get team, channel and message_ts
team, channel, message_ts = action.get("value").split(
"|"
) # team|channel|message_ts
print(
f"ack - action_id:'docs|qa|approve_reply', values - team: {team}, channel: {channel}, message_ts: {message_ts}"
)
# Acknowledge the interaction
ack()
# Log the body of the interaction payload
pp.pprint(body)
user_id = body["user"]["id"]
bot_id = body["message"]["bot_id"]
blocks_without_sendbutton = copy.deepcopy(body["message"]["blocks"])
if "accessory" in blocks_without_sendbutton[-2]:
del blocks_without_sendbutton[-2]["accessory"]
print("blocks_without_sendbutton:")
pp.pprint(blocks_without_sendbutton)
try:
app.client.reactions_add(
channel=body["channel"]["id"],
timestamp=body["message"]["ts"],
name="white_check_mark",
as_user=True,
)
except SlackApiError as e:
logger.error(f"Error adding reaction: {e}")
# Send a reply to the thread confirming that the message action was successful
try:
app.client.chat_postMessage(
channel=body["channel"]["id"],
thread_ts=body["message"]["ts"],
user_id=user_id,
text=f"<@{user_id}> gave the thumbs-up, forwarding message...",
)
app.client.chat_postMessage(
team=team,
channel=channel,
thread_ts=message_ts,
text=body["message"]["text"],
blocks=blocks_without_sendbutton,
user_id=bot_id,
)
except SlackApiError as e:
logger.error(f"Error sending confirmation: {e}")
# The open_modal shortcut opens a plain old modal
# Shortcuts require the command scope
@app.shortcut("open_modal")
def open_modal(ack, shortcut, client, logger):
# Acknowledge shortcut request
ack()
try:
# Call the views.open method using the WebClient passed to listeners
result = client.views_open(
trigger_id=shortcut["trigger_id"],
view={
"type": "modal",
"title": {"type": "plain_text", "text": "My App"},
"close": {"type": "plain_text", "text": "Close"},
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "About the simplest modal you could conceive of :smile:\n\nMaybe <https://api.slack.com/reference/block-kit/block-elements|*make the modal interactive*> or <https://api.slack.com/surfaces/modals/using#modifying|*learn more advanced modal use cases*>.",
},
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "Psssst this modal was designed using <https://api.slack.com/tools/block-kit-builder|*Block Kit Builder*>",
}
],
},
],
},
)
logger.info(result)
except SlackApiError as e:
logger.error("Error creating conversation: {}".format(e))
def main():
app.start(port=int(env_var("PORT", 3000)))
SocketModeHandler(app, env_var("SLACK_APP_TOKEN")).start()
if __name__ == "__main__":
main()