Skip to content
This repository was archived by the owner on Jun 19, 2019. It is now read-only.

Commit 8ee71f2

Browse files
committed
prepping for merge and deployment
1 parent 05bd15f commit 8ee71f2

File tree

7 files changed

+298
-45
lines changed

7 files changed

+298
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
credentials.json
44
creds.py
55

6+
67
# remove pycharm ide files
78
.idea/
89

src/airtable_handling.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import json
2+
from pprint import pprint
3+
4+
import requests
5+
from airtable import Airtable
6+
from decouple import config
7+
8+
BASE_KEY = config('PERSONAL_AIRTABLE_BASE_KEY')
9+
API_KEY = config('PERSONAL_AIRTABLE_TOKEN')
10+
TABLE_NAME = 'Mentor Request'
11+
12+
13+
14+
def get_table(table):
15+
airtable = Airtable(BASE_KEY, table, api_key=API_KEY)
16+
res = airtable.get_all()
17+
pprint(res)
18+
19+
20+
21+
22+
def test():
23+
params = {
24+
'fields': {
25+
'Slack User': 'Allen2',
26+
'Email': '[email protected]',
27+
'Service': ['recBxmDasLXwmVB78'],
28+
'Skillsets': ["Web (Frontend Development)"],
29+
'Additional Details': 'Details'}
30+
}
31+
32+
headers = {
33+
'authorization': "Bearer " + API_KEY
34+
}
35+
res = requests.post("https://api.airtable.com/v0/app2p4KgQKx35WoCm/Mentor%20Request", json=params,
36+
headers=headers)
37+
print(res.content)
38+
39+
40+
if __name__ == '__main__':
41+
# test()
42+
get_table('Services')
43+
44+
45+
services_records = {
46+
'General Guidance - Slack Chat': 'recBxmDasLXwmVB78',
47+
'General Guidance - Voice Chat': 'recDyu4PMbPl7Ti58',
48+
'Pair Programming': 'recHCFAO9uNSy1WDs',
49+
'Code Review': 'recUK55xJXOfAaYNb',
50+
'Resume Review': 'recXZzUduWfaxWvSF',
51+
'Mock Interview': 'recdY4XLeN1CPz1l8'
52+
}

src/app.py

Lines changed: 88 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
1+
import json
12
import logging
23
import time
34
from slackclient import SlackClient
45
from utils.log_manager import setup_logging
56
from decouple import config
67
import traceback
78
from pprint import pprint
8-
9+
import requests
910

1011
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
1215

1316
logger = logging.getLogger(__name__)
1417
new_event_logger = logging.getLogger(f'{__name__}.new_member')
1518
all_event_logger = logging.getLogger(f'{__name__}.all_events')
1619

1720
# constants
18-
PROXY = config('PROXY')
21+
PROXY = config('PROXY', default=None)
1922

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')
2225

23-
TOKEN = config('OPCODE_APP_TOKEN')
26+
# TOKEN = config('OPCODE_APP_TOKEN')
2427
# COMMUNITY_CHANNEL = config('OPCODE_REWRITE_CHANNEL')
2528
# PROJECTS_CHANNEL = config('OPCODE_OC_PROJECTS_CHANNEL')
2629
# 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'
2836

29-
PROXY = PROXY if PROXY else None
3037
slack_client = SlackClient(TOKEN, proxies=PROXY)
3138

3239

@@ -41,10 +48,10 @@ def event_handler(event_dict: dict) -> None:
4148
Handles routing all of the received subscribed events to the correct method
4249
:param event_dict:
4350
"""
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)
4855

4956
""" Trigger for testing team_join event """
5057
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:
6471
if response == 'suggestion':
6572
trigger_id = data['trigger_id']
6673
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)
6880

6981
else:
70-
params = {'text': ' \n\n\n' + HELP_MENU_RESPONSES[data['actions'][0]['value']],
82+
params = {'text': HELP_MENU_RESPONSES[data['actions'][0]['value']],
7183
'channel': data['channel']['id'],
7284
'ts': data['message_ts'],
7385
'as_user': True
@@ -103,14 +115,60 @@ def greeted_interaction(data: dict) -> dict:
103115
res = slack_client.api_call("chat.update", **params)
104116

105117

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+
"""
107124
suggestion = data['submission']['suggestion']
108125
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}"
110127
res = slack_client.api_call('chat.postMessage', channel=COMMUNITY_CHANNEL, text=message)
111128

112129

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+
113165
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+
"""
114172
new_event_logger.info('Recieved json event: {}'.format(event_dict))
115173

116174
user_id = event_dict['user']['id']
@@ -121,27 +179,27 @@ def new_member(event_dict: dict) -> None:
121179
custom_message = MESSAGE.format(real_name=real_name)
122180

123181
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)
129187

130188
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,
134192
**HELP_MENU)
135193

136194
# Notify #community
137195
text = f":tada: <@{user_id}> has joined the Slack team :tada:"
138196
slack_client.api_call('chat.postMessage', channel=COMMUNITY_CHANNEL,
139197
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: {} \nResponse2: {}'.format(response, r2))
201+
else:
202+
new_event_logger.error('FAILED -- Message to new member returned error: {}\n{}'.format(response, r2))
145203

146204

147205
def parse_slack_output(slack_rtm_output: list) -> None:
@@ -176,7 +234,6 @@ def join_channels():
176234
"""
177235
response = slack_client.api_call('channels.join', name='general')
178236
print(response)
179-
# set the defalt to a 1 second delay
180237

181238

182239
def run_bot(delay: int = 1) -> None:
@@ -202,4 +259,4 @@ def run_bot(delay: int = 1) -> None:
202259

203260

204261
if __name__ == '__main__':
205-
run_bot()
262+
run_bot()

src/flask_endpoint.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
app = Flask(__name__)
1111

12-
VERIFICATION_TOKEN = config('OPCODE_VERIFICATION_TOKEN')
13-
# VERIFICATION_TOKEN = config('APP_VERIFICATION_TOKEN')
12+
# VERIFICATION_TOKEN = config('OPCODE_VERIFICATION_TOKEN')
13+
VERIFICATION_TOKEN = config('APP_VERIFICATION_TOKEN')
1414

1515

1616
@app.route("/user_interaction", methods=['POST'])
@@ -24,6 +24,7 @@ def interaction():
2424
# TODO Logger here
2525
print("Bad request")
2626
return make_response("", 403)
27+
2728
callback = data['callback_id']
2829
pprint(data['user'])
2930

@@ -34,6 +35,9 @@ def interaction():
3435
elif callback == 'suggestion_modal':
3536
pprint(data)
3637
bot.suggestion_submission(data)
38+
elif callback == 'mentor_request':
39+
# pprint(data)
40+
bot.mentor_submission(data)
3741
return make_response('', 200)
3842

3943

@@ -53,6 +57,9 @@ def challenge():
5357
"""
5458
payload = {}
5559
data = request.get_json()
60+
61+
pprint(data)
62+
5663
if data['token'] != VERIFICATION_TOKEN:
5764
print("Bad request")
5865
return make_response("", 403)

0 commit comments

Comments
 (0)