1
1
import logging
2
2
import time
3
- from pprint import pprint
4
3
from slackclient import SlackClient
5
4
from utils .log_manager import setup_logging
6
5
from decouple import config
16
15
# constants
17
16
PROXY = config ('PROXY' )
18
17
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')
21
20
22
- # TOKEN = config('OPCODE_APP_TOKEN')
21
+ TOKEN = config ('OPCODE_APP_TOKEN' )
22
+ # TOKEN = config('TOKEN')
23
23
# COMMUNITY_CHANNEL = config('OPCODE_COMMUNITY_ID')
24
+ COMMUNITY_CHANNEL = config ('OPCODE_REWRITE_CHANNEL' )
25
+ PROJECTS_CHANNEL = config ('OPCODE_OC_PROJECTS_CHANNEL' )
24
26
25
27
PROXY = PROXY if PROXY else None
26
28
slack_client = SlackClient (TOKEN , proxies = PROXY )
27
29
28
30
31
+ # TODO: Do something with all of the return values here
32
+
29
33
def build_message (message_template : str , ** kwargs : dict ) -> str :
30
34
return message_template .format (** kwargs )
31
35
32
36
33
37
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
+ """
34
42
# all_event_logger.info(event_dict)
35
43
# if event_dict['type'] == 'team_join':
36
44
# new_event_logger.info('New member event recieved')
@@ -43,6 +51,11 @@ def event_handler(event_dict: dict) -> None:
43
51
44
52
45
53
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
+ """
46
59
params = {'text' : ' \n \n \n ' + HELP_MENU_RESPONSES [data ['actions' ][0 ]['value' ]],
47
60
'channel' : data ['channel' ]['id' ],
48
61
'ts' : data ['message_ts' ],
@@ -54,7 +67,10 @@ def help_menu_interaction(data: dict) -> None:
54
67
def greeted_interaction (data : dict ) -> dict :
55
68
"""
56
69
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
58
74
"""
59
75
if data ['actions' ][0 ]['value' ] == 'greeted' :
60
76
clicker = data ['user' ]['id' ]
@@ -65,7 +81,6 @@ def greeted_interaction(data: dict) -> dict:
65
81
'as_user' : True
66
82
}
67
83
res = slack_client .api_call ("chat.update" , ** params )
68
- # TODO Do something with this return value
69
84
return res
70
85
elif data ['actions' ][0 ]['value' ] == 'reset_greet' :
71
86
params = {'text' : data ['original_message' ]['text' ],
@@ -77,12 +92,10 @@ def greeted_interaction(data: dict) -> dict:
77
92
res = slack_client .api_call ("chat.update" , ** params )
78
93
79
94
80
- # TODO return something to flask app
81
95
def new_member (event_dict : dict ) -> None :
82
96
new_event_logger .info ('Recieved json event: {}' .format (event_dict ))
83
97
84
98
user_id = event_dict ['user' ]['id' ]
85
- # user_id = event_dict['user']
86
99
logging .info ('team_join message' )
87
100
88
101
real_name = user_name_from_id (user_id )
@@ -92,21 +105,23 @@ def new_member(event_dict: dict) -> None:
92
105
new_event_logger .info ('Built message: {}' .format (custom_message ))
93
106
response = slack_client .api_call ('chat.postMessage' ,
94
107
channel = user_id ,
95
- as_user = True ,
108
+ # channel=COMMUNITY_CHANNEL, # testing option
109
+ # as_user=True,
96
110
text = custom_message )
97
111
98
112
r2 = slack_client .api_call ('chat.postMessage' ,
99
113
channel = user_id ,
100
- as_user = True ,
114
+ # channel=COMMUNITY_CHANNEL, # testing option
115
+ # as_user=True,
101
116
** HELP_MENU )
102
117
103
118
# 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())
107
122
108
123
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: {} \n Response2: {} ' .format (response , r2 ))
110
125
else :
111
126
new_event_logger .error ('FAILED -- Message to new member returned error: {}' .format (response ))
112
127
@@ -138,12 +153,21 @@ def user_name_from_id(user_id: str) -> str:
138
153
139
154
140
155
def join_channels ():
156
+ """
157
+ Utility function for joining channels. Move to utils?
158
+ """
141
159
response = slack_client .api_call ('channels.join' , name = 'general' )
142
160
print (response )
143
161
144
162
145
163
# 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
+ """
147
171
setup_logging ()
148
172
if slack_client .rtm_connect ():
149
173
print (f"StarterBot connected and running with a { delay } second delay" )
0 commit comments