-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtmhub_bot.py
359 lines (276 loc) · 14.1 KB
/
gtmhub_bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
from dialog_bot_sdk.interactive_media import *
from dialog_bot_sdk.bot import DialogBot
from dialog_bot_sdk.internal.peers import private_peer
from src.gtmhub_metric_handler import GtmHubMetricHandler
from src.gtmhub_scheduler import GtmHubScheduler
from src.gtmhub_checkin import GtmHubCheckin
from src.input_exception import InputException
from src.translator import _
import re
import grpc
import json
# BOT UTILS
def parse_config():
with open('./config/config.json') as json_file:
return json.load(json_file)
def parse_command_params(command, text):
username_mark = '\[\[(.+?)\]\]'
listname_mark = '\(\((.+?)\)\)'
regexp = '^\/'+command+'( '+username_mark+'( '+listname_mark+')?| '+listname_mark+'( '+username_mark+')?)?$'
if not re.match(regexp, text):
raise InputException(_('Invalid command'))
username_match = re.search(username_mark, text)
listname_match = re.search(listname_mark, text)
params = {}
if username_match:
params['username'] = username_match.group(1)
if listname_match:
params['list_name'] = listname_match.group(1)
return params
def is_activate_text_command(text):
return re.match('^/'+_('activate')+'$', text)
def is_desactivate_text_command(text):
return re.match('^/'+_('desactivate')+'$', text)
def is_okr_text_command(text):
return re.match('^/'+_('okr'), text)
def is_cancel_text_command(text):
return re.match('^/'+_('stop')+'$', text)
def is_overview_text_command(text):
return re.match('^/'+_('overview'), text)
def print_okr_list(peer, okr_list):
if not okr_list:
bot.messaging.send_message(peer, _("There are no available OKR's with the specified parameters"))
else:
for id, okr in okr_list.items():
button = InteractiveMediaButton(okr['id'], _('Refresh'))
action = InteractiveMedia('create', button)
group = InteractiveMediaGroup([action])
objective_text = '['+okr['project_name']+']('+okr['project_url']+') - '+str(int(round(okr['project_attainment']*100)))+'%'
metric_text = '['+okr['name']+']('+okr['project_url']+'metric/'+okr['id']+'/) - '+str(okr['actual'])+' - * '+str(int(round(okr['attainment']*100)))+'% *'
bot.messaging.send_message(peer, objective_text+'\n'+metric_text, [group])
def print_okr_info(peer, okr_info):
message = okr_info['title']+'\n'
message += _('Now - * {} *').format(okr_info['current_value'])+'\n'
message += _('Must be - * {} *').format(okr_info['target_value'])+'\n'
message += _('Specify a new value.')
bot.messaging.send_message(peer, message)
def print_need_changes_info(peer):
bot.messaging.send_message(peer, _('What has changed? Why?'))
def print_need_scheduler_hour(peer):
bot.messaging.send_message(peer, _('Ok, now please reply with time in 16:40 format by Moscow timezone for me to know when to start'))
def print_need_scheduler_username(peer):
bot.messaging.send_message(peer, _('Tell me what user you will be overviewing'))
def print_need_confidence_level(peer, confidence_levels):
options = {}
for value in confidence_levels:
options[str(value)] = str(value)
select = InteractiveMediaSelect(options, _('Choose level'))
action = InteractiveMedia('set', select)
group = InteractiveMediaGroup([action])
bot.messaging.send_message(peer, _('Specify the level of confidence in the result'), [group])
def print_need_reminder_day(peer, days_list):
select = InteractiveMediaSelect(days_list, _('Choose day'))
action = InteractiveMedia('cron', select)
group = InteractiveMediaGroup([action])
bot.messaging.send_message(peer, _('Please select day to update OKR with you'), [group])
def print_update_preview(peer, preview_info):
send_button = InteractiveMediaButton(preview_info['id'], _('Submit'))
send_action = InteractiveMedia('send', send_button)
cancel_button = InteractiveMediaButton(preview_info['id'], _('Cancel'))
cancel_action = InteractiveMedia('cancel', cancel_button)
group = InteractiveMediaGroup([send_action, cancel_action])
bot.messaging.send_message(peer, '['+preview_info['title']+']('+preview_info['link']+') - * '+str(preview_info['new_percentage'])+'% *', [group])
def print_overview_navigation(peer):
send_button = InteractiveMediaButton('next', _('Next key'))
send_action = InteractiveMedia('overview', send_button)
cancel_button = InteractiveMediaButton('list', _('List un-updated keys'))
cancel_action = InteractiveMedia('overview', cancel_button)
group = InteractiveMediaGroup([send_action, cancel_action])
bot.messaging.send_message(peer, _('What is next'), [group])
def print_update_completed(peer):
bot.messaging.send_message(peer, _('The result is updated'))
def print_overview_completed(peer):
bot.messaging.send_message(peer, _('The overview is completed'))
def print_update_canceled(peer):
bot.messaging.send_message(peer, _('The update has been cancelled'))
def print_overview_canceled(peer):
bot.messaging.send_message(peer, _('The overview has been cancelled'))
def print_scheduler_canceled(peer):
bot.messaging.send_message(peer, _('The reminder has been cancelled'))
def print_message(peer, message):
bot.messaging.send_message(peer, message)
def print_error(peer, error):
bot.messaging.send_message(peer, error)
# BOT CALLBACKS
def scheduled_overview(peer_id, username):
print('Running scheduled overview '+str(peer_id))
peer = private_peer(peer_id)
if handler.has_overview(peer_id) or handler.has_checkin(peer_id) or scheduler.need_params(peer_id):
print_message(peer, _("It's time to update your OKRs. but seems you have some pendant works, after finish it you can run /overview command"))
else:
try:
handler.create_overview(peer.id, None, username)
print_message(peer, _("It's time to update your OKRs. Ok, let's go"))
print_okr_list(peer, handler.overviews[peer.id])
except InputException as ex:
print_message(peer, _("It's time to update your OKRs. But seems you have no OKR's to overview"))
except Exception as ex:
print("Exception on scheduled overview")
print(ex)
def on_message(*params):
peer = params[0].peer
message = params[0].message
sender_uid = params[0].sender_uid
if peer.id == sender_uid:
try:
if not hasattr(message, 'textMessage'):
raise InputException(_('Invalid input value, only text messages are allowed'))
text = message.textMessage.text
if is_cancel_text_command(text):
if handler.has_checkin(peer.id):
handler.cancel_checkin(peer.id)
print_update_canceled(peer)
elif handler.has_overview(peer.id):
handler.cancel_overview(peer.id)
print_overview_canceled(peer)
elif scheduler.need_params(peer.id):
scheduler.cancel_cron(peer.id)
print_scheduler_canceled(peer)
else:
print_error(peer, _('Nothing to cancel'))
elif is_overview_text_command(text):
if scheduler.need_params(peer.id):
raise InputException(_('Please, finish your reminder configuration, or cancel it, before continue'))
command_params = parse_command_params(_('overview'), text)
username = command_params.get('username', None)
list_name = command_params.get('list_name', None)
handler.create_overview(peer.id, list_name, username)
print_okr_list(peer, handler.overviews[peer.id])
elif is_okr_text_command(text):
if scheduler.need_params(peer.id):
raise InputException(_('Please, finish your reminder configuration, or cancel it, before continue'))
if handler.has_checkin(peer.id):
raise InputException(_('You have a checkin currently'))
if handler.has_overview(peer.id):
raise InputException(_("Please, finish you overview, or cancel it, before continue"))
command_params = parse_command_params(_('okr'), text)
username = command_params.get('username', None)
list_name = command_params.get('list_name', None)
okr_list = handler.get_okr_list(list_name, username)
print_okr_list(peer, okr_list)
elif is_activate_text_command(text):
if handler.has_checkin(peer.id) or handler.has_overview(peer.id):
raise InputException(_('Please, finish your updates before begin configuring the reminder'))
if scheduler.has_cron(peer.id):
raise InputException(_('You have a reminder currently, remove it to setup a new one'))
scheduler.create_cron(peer.id)
print_need_reminder_day(peer, GtmHubScheduler.days_list)
elif is_desactivate_text_command(text):
if not scheduler.has_cron(peer.id):
raise InputException(_('You have not a scheduler currently'))
scheduler.cancel_cron(peer.id)
print_scheduler_canceled(peer)
else:
if handler.has_checkin(peer.id):
handler.update_checkin(peer.id, text)
checkin_current_state = handler.get_checkin_state(peer.id)
if checkin_current_state == GtmHubCheckin.STATES.INITIALIZED:
print_need_changes_info(peer)
elif checkin_current_state == GtmHubCheckin.STATES.COMMENTED:
confidence_levels = handler.get_confidence_levels(peer.id)
print_need_confidence_level(peer, confidence_levels)
else:
raise Exception(_('Unexpected checkin state'))
elif scheduler.need_params(peer.id):
scheduler.set_value(peer.id, text)
if not scheduler.need_params(peer.id):
scheduler.enable_cron(peer.id)
day = scheduler.days_list[scheduler.crons[peer.id].day]
hour = scheduler.crons[peer.id].hour
print_message(peer, _("Ok, then each {} at {} I will start updating okr's with you.").format(day, hour))
else:
if scheduler.crons[peer.id].hour is None:
print_need_scheduler_hour(peer)
else:
print_need_scheduler_username(peer)
else:
raise InputException(_('Invalid command'))
except InputException as exception:
print(exception)
print_error(peer, str(exception))
except Exception as exception:
print(exception)
print_error(peer, _('Some internal error happens'))
def on_interact(*params):
action_id = params[0].id
value = params[0].value
uid = params[0].uid
peer = private_peer(uid)
try:
if action_id == 'create':
handler.create_checkin(peer.id, value)
okr_info = handler.get_okr_info(peer.id)
print_okr_info(peer, okr_info)
elif action_id == 'cron':
scheduler.set_value(peer.id, value)
print_need_scheduler_hour(peer)
elif action_id == 'set':
handler.update_checkin(peer.id, value)
preview_info = handler.get_preview_info(peer.id)
print_update_preview(peer, preview_info)
elif action_id == 'cancel':
handler.cancel_checkin(peer.id)
print_update_canceled(peer)
elif action_id == 'send':
handler.send_checkin(peer.id)
print_update_completed(peer)
if handler.has_overview(peer.id):
if handler.get_overview_lenght(peer.id) > 0:
print_overview_navigation(peer)
else:
handler.cancel_overview(peer.id)
print_overview_completed(peer)
elif action_id == 'overview':
if not handler.has_overview(peer.id):
raise InputException(_('Invalid action'))
if handler.has_checkin(peer.id):
raise InputException(_('You have a checkin currently, please finish it before'))
if value == 'next':
if handler.get_overview_lenght(peer.id) == 0:
raise InputException(_('Empty overview'))
next_okr_id = handler.get_next_overview_checkin_id(peer.id)
handler.create_checkin(peer.id, next_okr_id)
okr_info = handler.get_okr_info(peer.id)
print_okr_info(peer, okr_info)
elif value == 'list':
print_okr_list(peer, handler.overviews[peer.id])
else:
raise InputException(_('Invalid action'))
else:
raise InputException(_('Invalid action'))
except InputException as exception:
print(exception)
print_error(peer, str(exception))
except Exception as exception:
print(exception)
print_error(peer, _('Some internal error happens'))
if __name__ == '__main__':
try:
config = parse_config()
gtmhub_config = config['gtmhubConfig']
dialog_config = config['dialogConfig']
handler = GtmHubMetricHandler(gtmhub_config['url'], gtmhub_config['token'], gtmhub_config['account'])
scheduler = GtmHubScheduler(handler.get_usernames_list(), scheduled_overview)
bot = DialogBot.get_secure_bot(
dialog_config['host']+':'+dialog_config['port'],
grpc.ssl_channel_credentials(),
dialog_config['token']
)
bot.messaging.on_message(on_message, on_interact)
except KeyboardInterrupt as ex:
print(_('Stopping scheduler thread ...'))
if scheduler.loop.is_running() is True:
scheduler.loop.stop()
except Exception as ex:
print(_('Error initializing the bot - {}').format(str(ex)))
raise ex