1
+ import json
1
2
import logging
2
3
import time
3
4
from slackclient import SlackClient
4
5
from utils .log_manager import setup_logging
5
6
from decouple import config
6
7
import traceback
7
8
from pprint import pprint
8
-
9
+ import requests
9
10
10
11
from src .help_menu import HELP_MENU_RESPONSES
11
- from src .messages import HELP_MENU , MESSAGE , needs_greet_button , greeted_response_attachments , SUGGESTION_MODAL
12
+ from src .messages import *
13
+
14
+ # from src.airtable_handling import airtable
12
15
13
16
logger = logging .getLogger (__name__ )
14
17
new_event_logger = logging .getLogger (f'{ __name__ } .new_member' )
15
18
all_event_logger = logging .getLogger (f'{ __name__ } .all_events' )
16
19
17
20
# constants
18
- PROXY = config ('PROXY' )
21
+ PROXY = config ('PROXY' , default = None )
19
22
20
- # TOKEN = config('PERSONAL_APP_TOKEN')
21
- # COMMUNITY_CHANNEL = config('PERSONAL_PRIVATE_CHANNEL')
23
+ TOKEN = config ('PERSONAL_APP_TOKEN' )
24
+ COMMUNITY_CHANNEL = config ('PERSONAL_PRIVATE_CHANNEL' )
22
25
23
- TOKEN = config ('OPCODE_APP_TOKEN' )
26
+ # TOKEN = config('OPCODE_APP_TOKEN')
24
27
# COMMUNITY_CHANNEL = config('OPCODE_REWRITE_CHANNEL')
25
28
# PROJECTS_CHANNEL = config('OPCODE_OC_PROJECTS_CHANNEL')
26
29
# COMMUNITY_CHANNEL = config('OPCODE_COMMUNITY_ID')
27
- COMMUNITY_CHANNEL = config ('OPCODE_BOT_TESTING_CHANNEL' )
30
+ # COMMUNITY_CHANNEL = config('OPCODE_BOT_TESTING_CHANNEL')
31
+
32
+ """Airtable configs"""
33
+ AIRTABLE_BASE_KEY = config ('PERSONAL_AIRTABLE_BASE_KEY' )
34
+ AIRTABLE_API_KEY = config ('PERSONAL_AIRTABLE_TOKEN' )
35
+ AIRTABLE_TABLE_NAME = 'Mentor Request'
28
36
29
- PROXY = PROXY if PROXY else None
30
37
slack_client = SlackClient (TOKEN , proxies = PROXY )
31
38
32
39
@@ -41,10 +48,10 @@ def event_handler(event_dict: dict) -> None:
41
48
Handles routing all of the received subscribed events to the correct method
42
49
:param event_dict:
43
50
"""
44
- # all_event_logger.info(event_dict)
45
- # if event_dict['type'] == 'team_join':
46
- # new_event_logger.info('New member event recieved')
47
- # new_member(event_dict)
51
+ all_event_logger .info (event_dict )
52
+ if event_dict ['type' ] == 'team_join' :
53
+ new_event_logger .info ('New member event recieved' )
54
+ new_member (event_dict )
48
55
49
56
""" Trigger for testing team_join event """
50
57
if event_dict ['type' ] == 'message' and 'user' in event_dict .keys () and event_dict ['text' ] == 'testgreet' :
@@ -64,10 +71,15 @@ def help_menu_interaction(data: dict) -> None:
64
71
if response == 'suggestion' :
65
72
trigger_id = data ['trigger_id' ]
66
73
res = slack_client .api_call ('dialog.open' , trigger_id = trigger_id , dialog = SUGGESTION_MODAL )
67
- pprint (res )
74
+
75
+ # Disabled while airtable integration is still in development
76
+ # elif response == 'mentor':
77
+ # trigger_id = data['trigger_id']
78
+ # res = slack_client.api_call('dialog.open', trigger_id=trigger_id, dialog=MENTOR_REQUEST_MODAL)
79
+ # pprint(res)
68
80
69
81
else :
70
- params = {'text' : ' \n \n \n ' + HELP_MENU_RESPONSES [data ['actions' ][0 ]['value' ]],
82
+ params = {'text' : HELP_MENU_RESPONSES [data ['actions' ][0 ]['value' ]],
71
83
'channel' : data ['channel' ]['id' ],
72
84
'ts' : data ['message_ts' ],
73
85
'as_user' : True
@@ -103,14 +115,60 @@ def greeted_interaction(data: dict) -> dict:
103
115
res = slack_client .api_call ("chat.update" , ** params )
104
116
105
117
106
- def suggestion_submission (data ):
118
+ def suggestion_submission (data : dict ) -> None :
119
+ """
120
+ Receives the event when a user submits a suggestion for a new help topic and
121
+ posts it to the #community channel
122
+ :param data:
123
+ """
107
124
suggestion = data ['submission' ]['suggestion' ]
108
125
user_id = data ['user' ]['id' ]
109
- message = f"<@{ user_id } > just submitted a suggestion for a help topic:\n { suggestion } "
126
+ message = f":exclamation: <@{ user_id } > just submitted a suggestion for a help topic:exclamation: \n -- { suggestion } "
110
127
res = slack_client .api_call ('chat.postMessage' , channel = COMMUNITY_CHANNEL , text = message )
111
128
112
129
130
+ def mentor_submission (data ):
131
+ """
132
+ Parses the mentor request dialog form and pushes the data to Airtable.
133
+ :param data:
134
+ :return:
135
+ """
136
+
137
+ # Temporary hack. Change this to getting the record ID's from the table itself
138
+ services_records = {
139
+ 'General Guidance - Slack Chat' : 'recBxmDasLXwmVB78' ,
140
+ 'General Guidance - Voice Chat' : 'recDyu4PMbPl7Ti58' ,
141
+ 'Pair Programming' : 'recHCFAO9uNSy1WDs' ,
142
+ 'Code Review' : 'recUK55xJXOfAaYNb' ,
143
+ 'Resume Review' : 'recXZzUduWfaxWvSF' ,
144
+ 'Mock Interview' : 'recdY4XLeN1CPz1l8'
145
+ }
146
+
147
+ form = data ['submission' ]
148
+ params = {
149
+ 'fields' : {
150
+ 'Slack User' : form ['Slack User' ],
151
+ 'Email' : form ['Email' ],
152
+ 'Service' : [services_records [form ['service' ]]],
153
+ 'Skillsets' : [form ['skillset' ]],
154
+ 'Additional Details' : form ['Additional Details' ]
155
+ }
156
+ }
157
+
158
+ headers = {
159
+ 'authorization' : "Bearer " + AIRTABLE_API_KEY
160
+ }
161
+ res = requests .post (f"https://api.airtable.com/v0/{ AIRTABLE_BASE_KEY } /{ AIRTABLE_TABLE_NAME } " , json = params ,
162
+ headers = headers )
163
+
164
+
113
165
def new_member (event_dict : dict ) -> None :
166
+ """
167
+ Invoked when a new user joins and a team_join event is received.
168
+ DMs the new user with the welcome message and help menu as well as pings
169
+ the #community channel with a new member notification
170
+ :param event_dict:
171
+ """
114
172
new_event_logger .info ('Recieved json event: {}' .format (event_dict ))
115
173
116
174
user_id = event_dict ['user' ]['id' ]
@@ -121,27 +179,27 @@ def new_member(event_dict: dict) -> None:
121
179
custom_message = MESSAGE .format (real_name = real_name )
122
180
123
181
new_event_logger .info ('Built message: {}' .format (custom_message ))
124
- # response = slack_client.api_call('chat.postMessage',
125
- # # channel=user_id,
126
- # channel=COMMUNITY_CHANNEL, # testing option
127
- # # as_user=True, # Currently not working. DM comes from my account
128
- # text=custom_message)
182
+ response = slack_client .api_call ('chat.postMessage' ,
183
+ channel = user_id ,
184
+ # channel=COMMUNITY_CHANNEL, # testing option
185
+ as_user = True , # Currently not working. DM comes from my account
186
+ text = custom_message )
129
187
130
188
r2 = slack_client .api_call ('chat.postMessage' ,
131
- # channel=user_id,
132
- channel = COMMUNITY_CHANNEL , # testing option
133
- # as_user=True,
189
+ channel = user_id ,
190
+ # channel=COMMUNITY_CHANNEL, # testing option
191
+ as_user = True ,
134
192
** HELP_MENU )
135
193
136
194
# Notify #community
137
195
text = f":tada: <@{ user_id } > has joined the Slack team :tada:"
138
196
slack_client .api_call ('chat.postMessage' , channel = COMMUNITY_CHANNEL ,
139
197
text = text , attachments = needs_greet_button ())
140
- #
141
- # if response['ok']:
142
- # new_event_logger.info('New Member Slack response: Response 1: {} \nResponse2: {}'.format(response, r2))
143
- # else:
144
- # new_event_logger.error('FAILED -- Message to new member returned error: {}'.format(response))
198
+
199
+ if response [ 'ok' ] and r2 ['ok' ]:
200
+ new_event_logger .info ('New Member Slack response: Response 1: {} \n Response2: {}' .format (response , r2 ))
201
+ else :
202
+ new_event_logger .error ('FAILED -- Message to new member returned error: {}\n {} ' .format (response , r2 ))
145
203
146
204
147
205
def parse_slack_output (slack_rtm_output : list ) -> None :
@@ -176,7 +234,6 @@ def join_channels():
176
234
"""
177
235
response = slack_client .api_call ('channels.join' , name = 'general' )
178
236
print (response )
179
- # set the defalt to a 1 second delay
180
237
181
238
182
239
def run_bot (delay : int = 1 ) -> None :
@@ -202,4 +259,4 @@ def run_bot(delay: int = 1) -> None:
202
259
203
260
204
261
if __name__ == '__main__' :
205
- run_bot ()
262
+ run_bot ()
0 commit comments