|
1 | | -import logging |
2 | | -from pprint import pprint |
3 | | - |
4 | | -from sirbot import SirBot |
5 | | -from slack import methods |
6 | | -from slack.actions import Action |
7 | | - |
8 | | -from pybot.endpoints.slack.utils.action_messages import * |
9 | | -from pybot.endpoints.slack.utils import COMMUNITY_CHANNEL, TICKET_CHANNEL |
10 | | - |
11 | | -logger = logging.getLogger(__name__) |
12 | | - |
13 | | - |
14 | | -def create_endpoints(plugin): |
15 | | - plugin.on_action("resource_buttons", resource_buttons, wait=False) |
16 | | - plugin.on_action("greeted", member_greeted, name='greeted', wait=False) |
17 | | - plugin.on_action("greeted", reset_greet, name='reset_greet', wait=False) |
18 | | - plugin.on_action("suggestion", open_suggestion, wait=False) |
19 | | - plugin.on_action("suggestion_modal", post_suggestion, wait=False) |
20 | | - plugin.on_action("claim_mentee", claim_mentee, wait=False) |
21 | | - plugin.on_action("reset_claim_mentee", claim_mentee, wait=False) |
22 | | - plugin.on_action("claimed", claimed, name='claimed', wait=False) |
23 | | - plugin.on_action("claimed", reset_claim, name='reset_claim', wait=False) |
24 | | - plugin.on_action("report_message", open_report_dialog, wait=False) |
25 | | - plugin.on_action("report_dialog", send_report, wait=False) |
26 | | - plugin.on_action("open_ticket", open_ticket, wait=False) |
27 | | - plugin.on_action("ticket_status", ticket_status, wait=False) |
28 | | - |
29 | | - |
30 | | -async def ticket_status(action: Action, app: SirBot): |
31 | | - """ |
32 | | - Updates the ticket status dropdown. (I don't know why we need to manually |
33 | | - update the message for this..) |
34 | | - """ |
35 | | - slack = app.plugins["slack"].api |
36 | | - |
37 | | - response, selected_option = updated_ticket_status(action) |
38 | | - update_message = update_ticket_message(action, selected_option['text']) |
39 | | - |
40 | | - await slack.query(methods.CHAT_UPDATE, response) |
41 | | - await slack.query(methods.CHAT_POST_MESSAGE, update_message) |
42 | | - |
43 | | - |
44 | | -async def open_ticket(action: Action, app: SirBot): |
45 | | - """ |
46 | | - Called when a user submits the ticket dialog. Parses the submission and posts |
47 | | - the new ticket details to the required channel |
48 | | - """ |
49 | | - attachments = ticket_attachments(action) |
50 | | - response = { |
51 | | - 'channel': TICKET_CHANNEL, |
52 | | - 'attachments': attachments, |
53 | | - 'text': 'New Ticket Submission', |
54 | | - } |
55 | | - |
56 | | - await app["plugins"]["slack"].api.query(methods.CHAT_POST_MESSAGE, response) |
57 | | - |
58 | | - |
59 | | -async def send_report(action: Action, app: SirBot): |
60 | | - """ |
61 | | - Called when a user submits the report dialog. Pulls the original message |
62 | | - info from the state and posts the details to the moderators channel |
63 | | - """ |
64 | | - slack_id = action['user']['id'] |
65 | | - details = action['submission']['details'] |
66 | | - message_details = json.loads(action.action['state']) |
67 | | - |
68 | | - response = build_report_message(slack_id, details, message_details) |
69 | | - |
70 | | - await app["plugins"]["slack"].api.query(methods.CHAT_POST_MESSAGE, response) |
71 | | - |
72 | | - |
73 | | -async def open_report_dialog(action: Action, app: SirBot): |
74 | | - """ |
75 | | - Opens the message reporting dialog for the user to provide details. |
76 | | -
|
77 | | - Adds the message that they're reporting to the dialog's hidden state |
78 | | - to be pulled out when submitted. |
79 | | - """ |
80 | | - trigger_id = action['trigger_id'] |
81 | | - response = { |
82 | | - 'trigger_id': trigger_id, |
83 | | - 'dialog': report_dialog(action), |
84 | | - } |
85 | | - await app.plugins["slack"].api.query(methods.DIALOG_OPEN, response) |
86 | | - |
87 | | - |
88 | | -async def resource_buttons(action: Action, app: SirBot): |
89 | | - """ |
90 | | - Edits the resource message with the clicked on resource |
91 | | - """ |
92 | | - name = action['actions'][0]['name'] |
93 | | - |
94 | | - response = base_response(action) |
95 | | - response['text'] = HELP_MENU_RESPONSES[name] |
96 | | - |
97 | | - await app.plugins["slack"].api.query(methods.CHAT_UPDATE, response) |
98 | | - |
99 | | - |
100 | | -async def open_suggestion(action: Action, app: SirBot): |
101 | | - """ |
102 | | - Opens the suggestion modal when the user clicks on the "Are we missing something?" button |
103 | | - """ |
104 | | - trigger_id = action['trigger_id'] |
105 | | - response = { |
106 | | - 'trigger_id': trigger_id, |
107 | | - 'dialog': suggestion_dialog(trigger_id) |
108 | | - } |
109 | | - |
110 | | - await app.plugins["slack"].api.query(methods.DIALOG_OPEN, response) |
111 | | - |
112 | | - |
113 | | -async def post_suggestion(action: Action, app: SirBot): |
114 | | - """ |
115 | | - Posts a suggestion supplied by the suggestion modal to the community channel |
116 | | - """ |
117 | | - suggesting_user = action['user']['id'] |
118 | | - suggestion = action['submission']['suggestion'] |
119 | | - |
120 | | - response = { |
121 | | - 'text': new_suggestion_text(suggesting_user, suggestion), |
122 | | - 'channel': COMMUNITY_CHANNEL |
123 | | - } |
124 | | - |
125 | | - await app.plugins["slack"].api.query(methods.CHAT_POST_MESSAGE, response) |
126 | | - |
127 | | - |
128 | | -async def member_greeted(action: Action, app: SirBot): |
129 | | - """ |
130 | | - Called when a community member clicks the button saying they greeted the new member |
131 | | - """ |
132 | | - response = base_response(action) |
133 | | - user_id = action['user']['id'] |
134 | | - response['attachments'] = greeted_attachment(user_id) |
135 | | - |
136 | | - await app.plugins["slack"].api.query(methods.CHAT_UPDATE, response) |
137 | | - |
138 | | - |
139 | | -async def reset_greet(action: Action, app: SirBot): |
140 | | - """ |
141 | | - Resets the claim greet button back to its initial state and appends the user that hit reset and the time |
142 | | - """ |
143 | | - response = base_response(action) |
144 | | - response['attachments'] = not_greeted_attachment() |
145 | | - response['attachments'][0]['text'] = reset_greet_message(action['user']['id']) |
146 | | - |
147 | | - await app.plugins["slack"].api.query(methods.CHAT_UPDATE, response) |
148 | | - |
149 | | - |
150 | | -async def claimed(action: Action, app: SirBot): |
151 | | - """ |
152 | | - Provides basic "claim" functionality for use-cases that don't have any other effects. |
153 | | -
|
154 | | - Simply updates the button to allow resets and displays the user and time it was clicked. |
155 | | - """ |
156 | | - response = base_response(action) |
157 | | - user_id = action['user']['id'] |
158 | | - |
159 | | - attachments = action['original_message']['attachments'] |
160 | | - |
161 | | - for index, attachment in enumerate(attachments): |
162 | | - if 'callback_id' in attachment and attachment['callback_id'] == 'claimed': |
163 | | - attachments[index] = claimed_attachment(user_id) |
164 | | - response['attachments'] = attachments |
165 | | - |
166 | | - await app.plugins['slack'].api.query(methods.CHAT_UPDATE, response) |
167 | | - |
168 | | - |
169 | | -async def reset_claim(action: Action, app: SirBot): |
170 | | - """ |
171 | | - Provides basic "unclaim" functionality for use-cases that don't have any other effects. |
172 | | -
|
173 | | - Updates the button back to its initial state |
174 | | - """ |
175 | | - response = base_response(action) |
176 | | - |
177 | | - attachments = action['original_message']['attachments'] |
178 | | - for index, attachment in enumerate(attachments): |
179 | | - if 'callback_id' in attachment and attachment['callback_id'] == 'claimed': |
180 | | - attachments[index] = not_claimed_attachment() |
181 | | - |
182 | | - response['attachments'] = attachments |
183 | | - await app.plugins['slack'].api.query(methods.CHAT_UPDATE, response) |
184 | | - |
185 | | - |
186 | | -async def claim_mentee(action: Action, app: SirBot): |
187 | | - """ |
188 | | - Called when a mentor clicks on the button to claim a mentor request. |
189 | | -
|
190 | | - Attempts to update airtable with the new request status and updates the claim |
191 | | - button allowing it to be reset if needed. |
192 | | - """ |
193 | | - try: |
194 | | - update_airtable = True |
195 | | - clicker_id = action['user']['id'] |
196 | | - request_record = action['actions'][0]['name'] |
197 | | - click_type = action['actions'][0]['value'] |
198 | | - |
199 | | - response = base_response(action) |
200 | | - |
201 | | - user_info = await app.plugins['slack'].api.query(methods.USERS_INFO, dict(user=clicker_id)) |
202 | | - clicker_email = user_info['user']['profile']['email'] |
203 | | - |
204 | | - if click_type == 'mentee_claimed': |
205 | | - mentor_id = await app.plugins['airtable'].api.mentor_id_from_slack_email(clicker_email) |
206 | | - if mentor_id: |
207 | | - attachment = mentee_claimed_attachment(clicker_id, request_record) |
208 | | - else: |
209 | | - update_airtable = False |
210 | | - attachment = action['original_message']['attachments'] |
211 | | - attachment[0]['text'] = f":warning: <@{clicker_id}>'s slack Email not found in Mentor table. :warning:" |
212 | | - else: |
213 | | - mentor_id = '' |
214 | | - attachment = mentee_unclaimed_attachment(clicker_id, request_record) |
215 | | - |
216 | | - response['attachments'] = attachment |
217 | | - |
218 | | - await app.plugins['slack'].api.query(methods.CHAT_UPDATE, response) |
219 | | - if update_airtable: |
220 | | - await app.plugins['airtable'].api.update_request(request_record, mentor_id) |
221 | | - except Exception as ex: |
222 | | - print(ex) |
0 commit comments