Skip to content

Commit 917ef52

Browse files
committed
add message report action button
1 parent e72c4f1 commit 917ef52

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

pybot/endpoints/slack/actions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,37 @@ def create_endpoints(plugin):
1616
plugin.on_action("reset_claim_mentee", claim_mentee, wait=False)
1717
plugin.on_action("claimed", claimed, name='claimed', wait=False)
1818
plugin.on_action("claimed", reset_claim, name='reset_claim', wait=False)
19+
plugin.on_action("report_message", open_report_dialog, wait=False)
20+
plugin.on_action("report_dialog", send_report, wait=False)
21+
22+
23+
async def send_report(action: Action, app: SirBot):
24+
"""
25+
Called when a user submits the report dialog. Pulls the original message
26+
info from the state and posts the details to the moderators channel
27+
"""
28+
slack_id = action['user']['id']
29+
details = action['submission']['details']
30+
message_details = json.loads(action.action['state'])
31+
32+
response = build_report_message(slack_id, details, message_details)
33+
34+
await app["plugins"]["slack"].api.query(methods.CHAT_POST_MESSAGE, response)
35+
36+
37+
async def open_report_dialog(action: Action, app: SirBot):
38+
"""
39+
Opens the message reporting dialog for the user to provide details.
40+
41+
Adds the message that they're reporting to the dialog's hidden state
42+
to be pulled out when submitted.
43+
"""
44+
trigger_id = action['trigger_id']
45+
response = {
46+
'trigger_id': trigger_id,
47+
'dialog': report_dialog(action),
48+
}
49+
await app.plugins["slack"].api.query(methods.DIALOG_OPEN, response)
1950

2051

2152
async def resource_buttons(action: Action, app: SirBot):

pybot/endpoints/slack/utils/action_messages.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1+
import json
12
from time import time
23
from typing import List
34

45
from slack.events import Message
56

7+
from pybot.endpoints.slack.utils import REPORT_CHANNEL
8+
69

710
def now():
811
"""
@@ -111,6 +114,62 @@ def suggestion_dialog(trigger_id):
111114
}
112115

113116

117+
def report_dialog(action):
118+
trigger_id = action['trigger_id']
119+
120+
user = action['message'].get('user') or action['message'].get('username') # for bots
121+
message_data = {
122+
'text': action['message']['text'],
123+
'user': user,
124+
'channel': action['channel']
125+
}
126+
return {
127+
"callback_id": "report_dialog",
128+
"state": json.dumps(message_data),
129+
"title": "Report details",
130+
"submit_label": "Submit",
131+
"trigger_id": trigger_id,
132+
"elements": [{
133+
"type": "textarea",
134+
"label": "Details",
135+
"name": "details",
136+
"placeholder": "",
137+
"required": False
138+
}]
139+
}
140+
141+
142+
def build_report_message(slack_id, details, message_details):
143+
message = f'<@{slack_id}> sent a report with details: {details}'
144+
145+
attachment = [{
146+
"fields": [
147+
{
148+
"title": "User",
149+
"value": f"<@{message_details['user']}>",
150+
"short": True
151+
},
152+
{
153+
"title": "Channel",
154+
"value": f"<#{message_details['channel']['id']}|{message_details['channel']['name']}>",
155+
"short": True
156+
},
157+
{
158+
"title": "Message",
159+
"value": message_details['text'],
160+
"short": False
161+
}
162+
163+
]
164+
}]
165+
166+
return {
167+
"text": message,
168+
"channel": REPORT_CHANNEL,
169+
"attachments": attachment
170+
}
171+
172+
114173
def mentee_claimed_attachment(user_id: str, record: str) -> List[dict]:
115174
return [{
116175
"text": f":100: Request claimed by <@{user_id}>:100:\n"

0 commit comments

Comments
 (0)