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

Commit 99c92af

Browse files
committed
added docstrings, cleaned up code a bit
1 parent 0e0bd7b commit 99c92af

File tree

3 files changed

+56
-31
lines changed

3 files changed

+56
-31
lines changed

src/app.py

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import logging
22
import time
3-
from pprint import pprint
43
from slackclient import SlackClient
54
from utils.log_manager import setup_logging
65
from decouple import config
@@ -16,21 +15,30 @@
1615
# constants
1716
PROXY = config('PROXY')
1817

19-
TOKEN = config('PERSONAL_APP_TOKEN')
20-
COMMUNITY_CHANNEL = config('PERSONAL_PRIVATE_CHANNEL')
18+
# TOKEN = config('PERSONAL_APP_TOKEN')
19+
# COMMUNITY_CHANNEL = config('PERSONAL_PRIVATE_CHANNEL')
2120

22-
# TOKEN = config('OPCODE_APP_TOKEN')
21+
TOKEN = config('OPCODE_APP_TOKEN')
22+
# TOKEN = config('TOKEN')
2323
# COMMUNITY_CHANNEL = config('OPCODE_COMMUNITY_ID')
24+
COMMUNITY_CHANNEL = config('OPCODE_REWRITE_CHANNEL')
25+
PROJECTS_CHANNEL = config('OPCODE_OC_PROJECTS_CHANNEL')
2426

2527
PROXY = PROXY if PROXY else None
2628
slack_client = SlackClient(TOKEN, proxies=PROXY)
2729

2830

31+
# TODO: Do something with all of the return values here
32+
2933
def build_message(message_template: str, **kwargs: dict) -> str:
3034
return message_template.format(**kwargs)
3135

3236

3337
def event_handler(event_dict: dict) -> None:
38+
"""
39+
Handles routing all of the received subscribed events to the correct method
40+
:param event_dict:
41+
"""
3442
# all_event_logger.info(event_dict)
3543
# if event_dict['type'] == 'team_join':
3644
# new_event_logger.info('New member event recieved')
@@ -43,6 +51,11 @@ def event_handler(event_dict: dict) -> None:
4351

4452

4553
def help_menu_interaction(data: dict) -> None:
54+
"""
55+
Receives help menu selection from the user and dynamically updates
56+
displayed message
57+
:param data:
58+
"""
4659
params = {'text': ' \n\n\n' + HELP_MENU_RESPONSES[data['actions'][0]['value']],
4760
'channel': data['channel']['id'],
4861
'ts': data['message_ts'],
@@ -54,7 +67,10 @@ def help_menu_interaction(data: dict) -> None:
5467
def greeted_interaction(data: dict) -> dict:
5568
"""
5669
Handles the interactive message sent to the #community channel
57-
when a new member joins
70+
when a new member joins.
71+
72+
Displays the user that claimed the greeting along with the option
73+
to un-claim
5874
"""
5975
if data['actions'][0]['value'] == 'greeted':
6076
clicker = data['user']['id']
@@ -65,7 +81,6 @@ def greeted_interaction(data: dict) -> dict:
6581
'as_user': True
6682
}
6783
res = slack_client.api_call("chat.update", **params)
68-
# TODO Do something with this return value
6984
return res
7085
elif data['actions'][0]['value'] == 'reset_greet':
7186
params = {'text': data['original_message']['text'],
@@ -77,12 +92,10 @@ def greeted_interaction(data: dict) -> dict:
7792
res = slack_client.api_call("chat.update", **params)
7893

7994

80-
# TODO return something to flask app
8195
def new_member(event_dict: dict) -> None:
8296
new_event_logger.info('Recieved json event: {}'.format(event_dict))
8397

8498
user_id = event_dict['user']['id']
85-
# user_id = event_dict['user']
8699
logging.info('team_join message')
87100

88101
real_name = user_name_from_id(user_id)
@@ -92,21 +105,23 @@ def new_member(event_dict: dict) -> None:
92105
new_event_logger.info('Built message: {}'.format(custom_message))
93106
response = slack_client.api_call('chat.postMessage',
94107
channel=user_id,
95-
as_user=True,
108+
# channel=COMMUNITY_CHANNEL, # testing option
109+
# as_user=True,
96110
text=custom_message)
97111

98112
r2 = slack_client.api_call('chat.postMessage',
99113
channel=user_id,
100-
as_user=True,
114+
# channel=COMMUNITY_CHANNEL, # testing option
115+
# as_user=True,
101116
**HELP_MENU)
102117

103118
# Notify #community
104-
text = f":tada: <@{user_id}> has joined the Slack team :tada:"
105-
slack_client.api_call('chat.postMessage', channel=COMMUNITY_CHANNEL,
106-
text=text, attachments=needs_greet_button())
119+
# text = f":tada: <@{user_id}> has joined the Slack team :tada:"
120+
# slack_client.api_call('chat.postMessage', channel=COMMUNITY_CHANNEL,
121+
# text=text, attachments=needs_greet_button())
107122

108123
if response['ok']:
109-
new_event_logger.info('New Member Slack response: {}'.format(response))
124+
new_event_logger.info('New Member Slack response: Response 1: {} \nResponse2: {}'.format(response, r2))
110125
else:
111126
new_event_logger.error('FAILED -- Message to new member returned error: {}'.format(response))
112127

@@ -138,12 +153,21 @@ def user_name_from_id(user_id: str) -> str:
138153

139154

140155
def join_channels():
156+
"""
157+
Utility function for joining channels. Move to utils?
158+
"""
141159
response = slack_client.api_call('channels.join', name='general')
142160
print(response)
143161

144162

145163
# set the defalt to a 1 second delay
146-
def run_bot(delay: int = 1):
164+
def run_bot(delay: int=1):
165+
"""
166+
Runs the bot using the Slack Real Time Messaging API.
167+
**Doesn't provide events or interactive functionality
168+
:param delay:
169+
:return:
170+
"""
147171
setup_logging()
148172
if slack_client.rtm_connect():
149173
print(f"StarterBot connected and running with a {delay} second delay")

src/flask_endpoint.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
from flask import Flask, request, make_response
22
from decouple import config
33
import json
4-
from pprint import pprint
54

65
from src import app as bot
76
from utils.log_manager import setup_logging
87

98
app = Flask(__name__)
109

11-
# VERIFICATION_TOKEN = config('OPCODE_VERIFICATION_TOKEN')
12-
VERIFICATION_TOKEN = config('APP_VERIFICATION_TOKEN')
10+
VERIFICATION_TOKEN = config('OPCODE_VERIFICATION_TOKEN')
11+
12+
13+
# VERIFICATION_TOKEN = config('APP_VERIFICATION_TOKEN')
1314

1415

1516
@app.route("/user_interaction", methods=['POST'])
@@ -20,6 +21,7 @@ def interaction():
2021

2122
data = json.loads(request.form['payload'])
2223
if data['token'] != VERIFICATION_TOKEN:
24+
# TODO Logger here
2325
print("Bad request")
2426
return make_response("", 403)
2527
callback = data['callback_id']
@@ -45,7 +47,6 @@ def challenge():
4547
"""
4648
Endpoint for all subscribed events
4749
"""
48-
# pprint(request.get_json())
4950
payload = {}
5051
data = request.get_json()
5152
if data['token'] != VERIFICATION_TOKEN:

utils/general_utils.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
from slackclient import SlackClient
22
from decouple import config
33

4-
TOKEN = config('PERSONAL_APP_TOKEN')
4+
TOKEN = config('OPCODE_APP_TOKEN')
55

66
slack_client = SlackClient(TOKEN)
77

88

99
def list_channels():
10-
channels_call = slack_client.api_call("groups.list")
10+
channels_call = slack_client.api_call("channels.list")
1111
if channels_call.get('ok'):
12-
return channels_call['groups']
12+
return channels_call['channels']
1313
return None
1414

1515

1616
def channel_info(channel_id):
17-
channel_info = slack_client.api_call("channels.info", channel=channel_id)
18-
if channel_info:
19-
return channel_info['channel']
17+
info = slack_client.api_call("channels.info", channel=channel_id)
18+
if info:
19+
return info['channel']
2020
return None
2121

2222

@@ -36,12 +36,12 @@ def send_message(channel_id, message):
3636
print("Channels: ")
3737
for channel in channels:
3838
print(channel['name'] + " (" + channel['id'] + ")")
39-
detailed_info = channel_info(channel['id'])
40-
if detailed_info:
41-
if detailed_info['members']:
42-
print([x for x in detailed_info['members']])
43-
# print('Latest text from ' + channel['name'] + ":")
44-
# print(detailed_info['latest']['text'])
39+
# detailed_info = channel_info(channel['id'])
40+
# if detailed_info:
41+
# if detailed_info['members']:
42+
# print([x for x in detailed_info['members']])
43+
# print('Latest text from ' + channel['name'] + ":")
44+
# print(detailed_info['latest']['text'])
4545
print('-----')
4646
else:
4747
print("Unable to authenticate.")

0 commit comments

Comments
 (0)