-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
441 lines (402 loc) · 17.5 KB
/
__init__.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# Copyright 2018, domcross
# Github https://github.com/domcross
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mycroft import MycroftSkill, intent_file_handler
from mycroft.util.format import nice_date, nice_time
from mycroft.util.log import LOG
from rapidfuzz import fuzz
from mattermostdriver import Driver
import mattermostdriver.exceptions as mme
from datetime import datetime
import time
class MycroftChat(MycroftSkill):
def __init__(self):
MycroftSkill.__init__(self)
def initialize(self):
self.register_entity_file('channel.entity')
self.register_entity_file('service.entity')
self.state = "idle"
self.mm = None
# login data
self.username = self.settings.get("username", "")
self.token = self.settings.get("token", "")
self.login_id = self.settings.get("login_id", "")
self.password = self.settings.get("password", "")
# monitoring
self.ttl = self.settings.get("ttl", 10) * 60
self.notify_on_updates = self.settings.get("notify_on_updates", False)
LOG.debug("username: {}".format(self.username))
mm_driver_config = {
'url': self.settings.get("url", "chat.mycroft.ai"),
'scheme': self.settings.get("scheme", "https"),
'port': self.settings.get("port", 443),
'verify': self.settings.get("verify", True)
}
if self.settings.get("url", "chat.mycroft.ai") == "chat.mycroft.ai":
self.service_name = "Mycroft Chat"
else:
self.service_name = "Mattermost"
if self.token:
mm_driver_config['token'] = self.token
elif self.login_id and self.password:
mm_driver_config['login_id'] = self.login_id
mm_driver_config['password'] = self.password
if self.username:
self.mm = Driver(mm_driver_config)
try:
self.mm.login()
self.userid = \
self.mm.users.get_user_by_username(self.username)['id']
# TODO check if user is member of several teams
self.teamid = self.mm.teams.get_team_members_for_user(
self.userid)[0]['team_id']
LOG.debug("userid: {} teamid: {}".format(self.userid,
self.teamid))
except (mme.ResourceNotFound, mme.HTTPError,
mme.NoAccessTokenProvided, mme.NotEnoughPermissions,
mme.InvalidOrMissingParameters) as e:
LOG.debug("Exception: {}".format(e))
self.mm = None
self.speak_dialog("mattermost.error", {'exception': e})
if self.mm:
# info on all subscribed public channels as returned by MM
self.channel_subscriptions = None
self.channel_subs_ts = 0
# basic info of channels
# channel_id, display_name, (unread) msg_count, mentions
self.channel_info = None
self.channel_info_ts = 0
self.usercache = {}
self.prev_unread = 0
self.prev_mentions = 0
if self.settings.get('monitoring', False):
self.monitoring = True
self.schedule_repeating_event(
self._mattermost_monitoring_handler,
None, self.ttl, 'Mattermost')
else:
self.monitoring = False
# Check and then monitor for credential changes
self.settings_change_callback = self.on_websettings_changed
def on_websettings_changed(self):
LOG.debug("websettings changed!")
if self.mm:
self.mm.logout()
if self.monitoring:
self.cancel_scheduled_event('Mattermost')
self.initialize()
@intent_file_handler('read.unread.channel.intent')
def read_channel_messages(self, message):
if not self.mm:
self.speak_dialog("skill.not.initialized")
return
elif self.state != "idle":
return
else:
self.state = "speaking"
channel_name = message.data.get('channel')
LOG.debug("data {}".format(message.data))
if not channel_name:
self.speak_dialog('channel.unknown', data={'channel': ''})
self.state = "idle"
return
# do some fuzzy matching on channel
best_chan = {}
best_score = 66 # minimum score required
for chan in self._get_channel_info():
score = fuzz.ratio(channel_name.lower(),
chan['display_name'].lower(),
score_cutoff=best_score)
# LOG.debug("{}->{}".format(unr['display_name'], score))
if score > best_score:
best_chan = chan
best_score = score
LOG.debug("{} -> {}".format(best_chan, best_score))
if not best_chan:
self.speak_dialog('channel.unknown',
data={'channel': channel_name})
elif best_chan['msg_count'] == 0:
self.speak_dialog('no.unread.channel.messages',
data={'channel': channel_name})
else:
self._read_unread_channel(best_chan)
self.state = "idle"
@intent_file_handler('start.monitoring.intent')
def start_monitoring_mattermost(self, message):
if not self.mm:
self.speak_dialog("skill.not.initialized")
return
LOG.debug("start monitoring with ttl {} secs".format(self.ttl))
self.schedule_repeating_event(self._mattermost_monitoring_handler,
None, self.ttl, 'Mattermost')
self.monitoring = True
self.settings['monitoring'] = True
self.speak_dialog('monitoring.active', {'service': self.service_name})
@intent_file_handler('end.monitoring.intent')
def end_monitoring_mattermost(self, message):
LOG.debug("end monitoring")
self.cancel_scheduled_event('Mattermost')
self.monitoring = False
self.settings['monitoring'] = False
self.speak_dialog('monitoring.inactive', data={'service':
self.service_name})
@intent_file_handler('read.unread.messages.intent')
def read_unread_messages(self, message):
if not self.mm:
self.speak_dialog("skill.not.initialized")
return
elif self.state != "idle":
return
else:
self.state = "speaking"
for chan in self._get_channel_info():
if self.state == "stopped":
break
self._read_unread_channel(chan)
self.state = "idle"
@intent_file_handler('list.unread.channels.intent')
def list_unread_channels(self, message):
if not self.mm:
self.speak_dialog("skill.not.initialized")
return
elif self.state != "idle":
return
else:
self.state = "speaking"
count = 0
for ch in self._get_channel_info():
responses = []
if(ch['msg_count'] and ch['mention_count']):
responses.append(self.dialog_renderer.render(
"channel.unread.and.mentioned", {
'msg_count': ch['msg_count'], # TODO use nice_number
'display_name': ch['display_name'],
'mention_count': ch['mention_count']
}))
elif ch['msg_count']:
responses.append(self.dialog_renderer.render(
"channel.unread", {
'msg_count': ch['msg_count'], # TODO use nice_number
'display_name': ch['display_name']
}))
elif ch['mention_count']:
responses.append(self.dialog_renderer.render(
"channel.mentioned", {
'mention_count': ch['mention_count'],
'display_name': ch['display_name']
}))
if responses:
count += 1
for res in responses:
if self.state == "stopped":
break
self.speak(res, wait=True)
if count == 0:
# no unread/mentions
self.speak_dialog('no.unread.messages', data={'service':
self.service_name})
self.state = "idle"
@intent_file_handler('unread.messages.intent')
def check_unread_messages_and_mentions(self, message):
if not self.mm:
self.speak_dialog("skill.not.initialized")
return
elif self.state != "idle":
return
else:
self.state = "speaking"
unreadmsg = self._get_unread_msg_count()
mentions = self._get_mention_count()
response = self.__render_unread_dialog(unreadmsg, mentions,
self.service_name)
self.enclosure.deactivate_mouth_events()
self.enclosure.mouth_text('unread: {} mentions: {}'.format(unreadmsg,
mentions))
self.speak(response, wait=True)
self.enclosure.activate_mouth_events()
self.enclosure.mouth_reset()
self.state = "idle"
def stop(self):
# this requires mycroft-stop skill installed
if self.state == "speaking":
LOG.debug("stopping")
self.state = "stopped"
return True
return False
def _read_unread_channel(self, chan):
if self.state == "stopped":
return
msg_count = chan['msg_count']
if msg_count:
channel_message = self.dialog_renderer.render(
"messages.for.channel", {
'display_name': chan['display_name']
})
LOG.debug(channel_message)
self.speak(channel_message)
pfc = self.mm.posts.get_posts_for_channel(chan['channel_id'])
order = pfc['order']
# in case returned posts are less than number of unread
# avoid 'index out of bounds'
msg_count = msg_count if msg_count < len(order) else len(order)
prev_date = ""
for i in range(0, msg_count):
if self.state == "stopped":
break
# order starts with newest to oldest,
# start to read the oldest of the unread
post = pfc['posts'][order[msg_count - i - 1]]
create_at = ""
# nice_date does only support en-us yet - bummer!
# MM timestamps are in millisecs, python in secs
msg_date = nice_date(datetime.fromtimestamp(
post['create_at'] / 1000), self.lang,
now=datetime.now())
if prev_date != msg_date:
create_at = msg_date + " "
prev_date = msg_date
msg_time = nice_time(datetime.fromtimestamp(
post['create_at'] / 1000), self.lang)
create_at += msg_time
msg = self.dialog_renderer.render(
"message", {
'user_name': self._get_user_name(post['user_id']),
'create_at': create_at,
'message': post['message']
})
LOG.debug(msg)
self.speak(msg, wait=True)
time.sleep(.3)
# mark channel as read
self.mm.channels.view_channel(self.userid, {
'channel_id': chan['channel_id']})
# TODO clarify when to reset prev_unread/prev_mentions
self.prev_unread = 0
self.prev_mentions = 0
def _get_unread_msg_count(self):
unreadmsg = 0
for chan in self._get_channel_info():
if(chan['msg_count']):
unreadmsg += chan['msg_count']
return unreadmsg
def _get_mention_count(self):
mentions = 0
for chan in self._get_channel_info():
if(chan['mention_count']):
mentions += chan['mention_count']
return mentions
def __render_unread_dialog(self, unreadmsg, mentions,
service=None):
if not service:
service = self.service_name
LOG.debug("unread {} mentions {}".format(unreadmsg, mentions))
response = ""
if unreadmsg:
response += self.dialog_renderer.render('unread.messages', {
'unreadmsg': unreadmsg, 'service': service})
response += " "
if mentions:
response += self.dialog_renderer.render('mentioned', {
'mentions': mentions, 'service': service})
if not response:
response = self.dialog_renderer.render('no.unread.messages',
{'service': service})
return response
def _mattermost_monitoring_handler(self):
LOG.debug("mm monitoring handler")
# do not update when last run was less than 30secs before
if (time.time() - self.channel_subs_ts) > 30:
self._get_channel_subscriptions()
if (time.time() - self.channel_info_ts) > 30:
self._get_channel_info()
LOG.debug("check for notifications")
unreadmsg = self._get_unread_msg_count()
mentions = self._get_mention_count()
# TODO clarify when to reset prev_unread/prev_mentions
if unreadmsg != self.prev_unread:
self.prev_unread = unreadmsg
else:
unreadmsg = 0
if mentions != self.prev_mentions:
self.prev_mentions = mentions
else:
mentions = 0
LOG.debug("unread: {} mentions: {}".format(unreadmsg, mentions))
if unreadmsg or mentions:
# display unread and mentions on Mark-1/2 display
display_text = self.dialog_renderer.render(
'display.message.count', {'unread': unreadmsg,
'mentions': mentions})
if self.config_core.get("enclosure").get("platform", "") == \
'mycroft_mark_1':
self.enclosure.deactivate_mouth_events()
self.enclosure.mouth_text(display_text)
# clear display after 30 seconds
self.schedule_event(self._mattermost_display_handler, 30, None,
'mmdisplay')
elif self.config_core.get("enclosure").get("platform", "") == \
'mycroft_mark_2':
self.gui.show_text(display_text, self.service_name)
if self.notify_on_updates:
self.speak(self.__render_unread_dialog(unreadmsg, mentions))
def _mattermost_display_handler(self):
# clear display and reset display handler
self.enclosure.activate_mouth_events()
self.enclosure.mouth_reset()
self.cancel_scheduled_event('mmdisplay')
def _get_channel_subscriptions(self):
# update channel subscriptions only every second ttl interval
if (time.time() - self.channel_subs_ts) > (self.ttl * 2):
LOG.debug("get channel subscriptions...")
self.channel_subscriptions = \
self.mm.channels.get_channels_for_user(self.userid,
self.teamid)
self.channel_subs_ts = time.time()
LOG.debug("...done")
# LOG.debug(self.channel_subscriptions)
return self.channel_subscriptions
def _get_channel_info(self):
if (time.time() - self.channel_info_ts) > self.ttl:
LOG.debug("get channel info...")
info = []
for chan in self._get_channel_subscriptions():
if chan['team_id'] != self.teamid:
continue
unr = self.mm.channels.get_unread_messages(
self.userid, chan['id'])
info.append({
'display_name': chan['display_name'],
'msg_count': unr['msg_count'],
'mention_count': unr['mention_count'],
'channel_id': chan['id']
})
self.channel_info = info
self.channel_info_ts = time.time()
LOG.debug("...done")
# LOG.debug(self.channel_info)
return self.channel_info
def _get_user_name(self, userid):
if not (userid in self.usercache):
user = self.mm.users.get_user(userid)
self.usercache[userid] = user['username']
# LOG.debug("usercache {}->{}".format(userid, user['username']))
return self.usercache[userid]
def create_skill():
return MycroftChat()
def shutdown(self):
if self.mm:
self.mm.logout()
if self.monitoring:
self.cancel_scheduled_event('Mattermost')
super(MycroftChat, self).shutdown()