forked from marksull/err-backend-cisco-webex-teams
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCiscoWebexTeams.py
693 lines (544 loc) · 21.9 KB
/
CiscoWebexTeams.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
import asyncio
import copyreg
import json
import sys
import logging
import uuid
import websockets
import string
import random
from markdown import markdown
from errbot.core import ErrBot
from errbot.backends.base import Message, Person, Room, RoomOccupant, OFFLINE, RoomDoesNotExistError
from errbot import rendering
import webexteamssdk
__version__="1.0.0"
log = logging.getLogger('errbot.backends.CiscoWebexTeams')
CISCO_WEBEX_TEAMS_MESSAGE_SIZE_LIMIT = 7439
DEVICES_URL = 'https://wdm-a.wbx2.com/wdm/api/v1/devices'
DEVICE_DATA = {
"deviceName" : "pywebsocket-client",
"deviceType" : "DESKTOP",
"localizedModel": "python",
"model" : "python",
"name" : f"python-webex-teams-client-{''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))}",
"systemName" : "python-webex-teams-client",
"systemVersion" : "0.1"
}
class FailedToCreateWebexDevice(Exception):
pass
class FailedToFindWebexTeamsPerson(Exception):
pass
class FailedToFindWebexTeamsRoom(Exception):
pass
class CiscoWebexTeamsMessage(Message):
"""
A Cisco Webex Teams Message
"""
@property
def is_direct(self) -> bool:
return self.extras['roomType'] == 'direct'
@property
def is_group(self) -> bool:
return not self.is_direct
class CiscoWebexTeamsPerson(Person):
"""
A Cisco Webex Teams Person
"""
def __init__(self, backend, attributes=None):
self._backend = backend
attributes = attributes or {}
if isinstance(attributes, webexteamssdk.Person):
self.teams_person = attributes
else:
self.teams_person = webexteamssdk.Person(attributes)
@property
def id(self):
return self.teams_person.id
@id.setter
def id(self, val):
self.teams_person._json_data['id'] = val
@property
def emails(self):
return self.teams_person.emails
@emails.setter
def emails(self, val):
self.teams_person._json_data['emails'] = val
@property
def email(self):
if type(self.emails) is list:
if len(self.emails):
# Note sure why a person can have multiple email addresses
return self.emails[0]
return None
@email.setter
def email(self, val):
self.emails = [val]
@property
def aclattr(self):
return self.teams_person.email
@property
def displayName(self):
return self.teams_person.displayName
@property
def created(self):
return self.teams_person.created
@property
def avatar(self):
return self.teams_person.avatar
def find_using_email(self):
"""
Return the FIRST Cisco Webex Teams person found when searching using an email address
"""
try:
for person in self._backend.webex_teams_api.people.list(email=self.email):
self.teams_person = person
return
except:
raise FailedToFindWebexTeamsPerson(f'Could not find a user using the email address {self.email}')
def find_using_name(self):
"""
Return the FIRST Cisco Webex Teams person found when searching using the display name
"""
try:
for person in self._backend.webex_teams_api.people.list(displayName=self.displayName):
self.teams_person = person
return
except:
raise FailedToFindWebexTeamsPerson(f'Could not find the user using the displayName {self.displayName}')
def get_using_id(self):
"""
Return a Cisco Webex Teams person when searching using an ID
"""
try:
self._backend.webex_teams_api.people.get(self.id)
except:
raise FailedToFindWebexTeamsPerson(f'Could not find the user using the id {self.id}')
# Required by the Err API
@property
def person(self):
return self.email
@property
def client(self):
return ''
@property
def nick(self):
return ''
@property
def fullname(self):
return self.displayName
def json(self):
return self.teams_person.json()
def __eq__(self, other):
return str(self) == str(other)
def __unicode__(self):
return self.email
__str__ = __unicode__
class CiscoWebexTeamsRoomOccupant(CiscoWebexTeamsPerson, RoomOccupant):
"""
A Cisco Webex Teams Person that Occupies a Cisco Webex Teams Room
"""
def __init__(self, backend, room=None, person=None):
room = room or {}
person = person or {}
if isinstance(room, CiscoWebexTeamsRoom):
self._room = room
else:
self._room = CiscoWebexTeamsRoom(backend=backend, room_id=room)
if isinstance(person, CiscoWebexTeamsPerson):
self.teams_person = person
else:
self.teams_person = CiscoWebexTeamsPerson(backend=backend, attributes=person)
@property
def room(self):
return self._room
class CiscoWebexTeamsRoom(Room):
"""
A Cisco Webex Teams Room
"""
def __init__(self, backend, room_id=None, room_title=None):
self._backend = backend
self._room_id = room_id
self._room_title = room_title
self._room = None
if room_id is not None and room_title is not None:
raise ValueError("room_id and room_title are mutually exclusive")
if not room_id and not room_title:
raise ValueError("room_id or room_title is needed")
if room_title is not None:
self.load_room_from_title()
else:
self.load_room_from_id()
def load_room_from_title(self):
"""
Load a room object from a title. If no room is found, return a new Room object.
"""
rooms = self._backend.webex_teams_api.rooms.list()
room = [room for room in rooms if room.title == self._room_title]
if not len(room) > 0:
self._room = webexteamssdk.models.immutable.Room({})
self._room_id = None
else:
# TODO: not sure room title will duplicate
self._room = room[0]
self._room_id = self._room.id
def load_room_from_id(self):
"""
Load a room object from a webex room id. If no room is found, return a new Room object.
"""
try:
self._room = self._backend.webex_teams_api.rooms.get(self._room_id)
self._room_title = self._room.title
except webexteamssdk.exceptions.ApiError:
self._room = webexteamssdk.models.immutable.Room({})
@property
def id(self):
"""Return the ID of this room"""
return self._room_id
@property
def room(self):
"""Return the webexteamssdk.models.immutable.Room instance"""
return self._room
@property
def created(self):
return self._room.created
@property
def title(self):
return self._room_title
# Errbot API
def join(self, username=None, password=None):
log.debug(f'Joining room {self.title} ({self.id})')
try:
self._backend.webex_teams_api.memberships.create(self.id, self._backend.bot_identifier.id)
log.debug(f'{self._backend.bot_identifier.displayName} is NOW a member of {self.title} ({self.id}')
except webexteamssdk.exceptions.ApiError as error:
# API now returning a 403 when trying to add user to a direct conversation and they are already in the
# conversation. For groups if the user is already a member a 409 is returned.
if error.response.status_code == 403 or error.response.status_code == 409:
log.debug(f'{self._backend.bot_identifier.displayName} is already a member of {self.title} ({self.id})')
else:
log.exception(f'HTTP Exception: Failed to join room {self.title} ({self.id})')
return
except Exception:
log.exception("Failed to join room {} ({})".format(self.title, self.id))
return
def leave(self, reason=None):
log.debug("Leave room yet to be implemented") # TODO
pass
def create(self):
"""
Create a new room. Membership to the room is provide by default.
"""
self._room = self._backend.webex_teams_api.rooms.create(self.title)
self._room_id = self._room.id
self._backend.webex_teams_api.messages.create(roomId=self._room_id, text="Welcome to the room!")
log.debug(f'Created room: {self.title}')
def destroy(self):
"""
Destroy (delete) a room
:return:
"""
self._backend.webex_teams_api.rooms.delete(self.id)
# We want to re-init this room so that is accurately reflects that is no longer exists
self.load_room_from_title()
log.debug(f'Deleted room: {self.title}')
@property
def exists(self):
return not self._room.created == None
@property
def joined(self):
rooms = self._backend.webex_teams_api.rooms.list()
return len([room for room in rooms if room.title == room.title]) > 0
@property
def topic(self):
return self.title
@topic.setter
def topic(self, topic):
log.debug("Topic room yet to be implemented") # TODO
pass
@property
def occupants(self):
if not self.exists:
raise RoomDoesNotExistError(f"Room {self.title or self.id} does not exist, or the bot does not have access")
occupants = []
for person in self._backend.webex_teams_api.memberships.list(roomId=self.id):
p = CiscoWebexTeamsPerson(backend=self._backend)
p.id = person.personId
p.email = person.personEmail
occupants.append(CiscoWebexTeamsRoomOccupant(backend=self._backend, room=self, person=p))
log.debug("Total occupants for room {} ({}) is {} ".format(self.title, self.id, len(occupants)))
return occupants
def invite(self, *args):
log.debug("Invite room yet to be implemented") # TODO
pass
def __eq__(self, other):
return str(self) == str(other)
def __unicode__(self):
return self.title
__str__ = __unicode__
class CiscoWebexTeamsBackend(ErrBot):
"""
This is the CiscoWebexTeams backend for errbot.
"""
def __init__(self, config):
super().__init__(config)
bot_identity = config.BOT_IDENTITY
self.md = rendering.md()
# Do we have the basic mandatory config needed to operate the bot
self._bot_token = bot_identity.get('TOKEN', None)
if not self._bot_token:
log.fatal('You need to define the Cisco Webex Teams Bot TOKEN in the BOT_IDENTITY of config.py.')
sys.exit(1)
# Adjust message size limit to cater for the non-standard size limit
if config.MESSAGE_SIZE_LIMIT > CISCO_WEBEX_TEAMS_MESSAGE_SIZE_LIMIT:
log.info(
"Capping MESSAGE_SIZE_LIMIT to {} which is the maximum length allowed by CiscoWebexTeams".
format(CISCO_WEBEX_TEAMS_MESSAGE_SIZE_LIMIT)
)
config.MESSAGE_SIZE_LIMIT = CISCO_WEBEX_TEAMS_MESSAGE_SIZE_LIMIT
log.debug("Setting up SparkAPI")
self.webex_teams_api = webexteamssdk.WebexTeamsAPI(access_token=self._bot_token)
log.debug("Setting up device on Webex Teams")
self.device_info = self._get_device_info()
log.debug("Fetching and building identifier for the bot itself.")
self.bot_identifier = CiscoWebexTeamsPerson(self, self.webex_teams_api.people.me())
log.debug("Done! I'm connected as {}".format(self.bot_identifier.email))
self._register_identifiers_pickling()
@property
def mode(self):
return 'CiscoWebexTeams'
def is_from_self(self, message):
return message.frm.id == message.to.id
def process_websocket(self, message):
"""
Process the data from the websocket and determine if we need to ack on it
:param message: The message received from the websocket
:return:
"""
message = json.loads(message.decode('utf-8'))
if message['data']['eventType'] != 'conversation.activity':
logging.debug('Ignoring message where Event Type is not conversation.activity')
return
activity = message['data']['activity']
if activity['verb'] != 'post':
logging.debug('Ignoring message where the verb is not type "post"')
return
spark_message = self.webex_teams_api.messages.get(activity['id'])
if spark_message.personEmail in self.bot_identifier.emails:
logging.debug('Ignoring message from myself')
return
logging.info('Message from %s: %s\n' % (spark_message.personEmail, spark_message.text))
self.callback_message(self.get_message(spark_message))
def get_message(self, message):
"""
Create an errbot message object
"""
person = CiscoWebexTeamsPerson(self)
person.id = message.id
person.email = message.personEmail
room = CiscoWebexTeamsRoom(backend=self, room_id=message.roomId)
occupant = CiscoWebexTeamsRoomOccupant(self, person=person, room=room)
msg = CiscoWebexTeamsMessage(body=message.markdown or message.text,
frm=occupant,
to=room,
extras={'roomType': message.roomType})
return msg
def follow_room(self, room):
"""
Backend: Follow Room yet to be implemented
:param room:
:return:
"""
log.debug("Backend: Follow Room yet to be implemented") # TODO
pass
def rooms(self):
"""
Backend: Rooms that the bot is a member of
:return:
List of rooms
"""
return [f"{room.title} ({room.type})" for room in self.webex_teams_api.rooms.list()]
def contacts(self):
"""
Backend: Contacts yet to be implemented
:return:
"""
log.debug("Backend: Contacts yet to be implemented") # TODO
pass
def build_identifier(self, strrep):
"""
Build an errbot identifier using the Webex Teams email address of the person
:param strrep: The email address of the Cisco Webex Teams person
:return: CiscoWebexTeamsPerson
"""
person = CiscoWebexTeamsPerson(self)
person.email = strrep
person.find_using_email()
return person
def query_room(self, room_id_or_name):
"""
Create a CiscoWebexTeamsRoom object identified by the ID or name of the room
:param room_id_or_name:
The Cisco Webex Teams room ID or a room name
:return:
:class: CiscoWebexTeamsRoom
"""
if isinstance(room_id_or_name, webexteamssdk.Room):
return CiscoWebexTeamsRoom(backend=self, room_id=room_id_or_name.id)
# query_room can provide us either a room name of an ID, so we need to check
# for both
room = CiscoWebexTeamsRoom(backend=self, room_id=room_id_or_name)
if not room.exists:
room = CiscoWebexTeamsRoom(backend=self, room_title=room_id_or_name)
return room
def send_message(self, mess):
"""
Send a message to Cisco Webex Teams
:param mess: A CiscoWebexTeamsMessage
"""
# Need to strip out "markdown extra" as not supported by Webex Teams
md = markdown(self.md.convert(mess.body),
extensions=['markdown.extensions.nl2br', 'markdown.extensions.fenced_code'])
if type(mess.to) == CiscoWebexTeamsPerson:
self.webex_teams_api.messages.create(toPersonId=mess.to.id, text=mess.body, markdown=md)
else:
self.webex_teams_api.messages.create(roomId=mess.to.room.id, text=mess.body, markdown=md)
def build_reply(self, mess, text=None, private=False, threaded=False):
"""
Build a reply in the format expected by errbot by swapping the to and from source and destination
:param mess: The original CiscoWebexTeamsMessage object that will be replied to
:param text: The text that is to be sent in reply to the message
:param private: Boolean indiciating whether the message should be directed as a private message in lieu of
sending it back to the room
:return: CiscoWebexTeamsMessage
"""
response = self.build_message(text)
response.frm = mess.to
response.to = mess.frm
return response
def disconnect_callback(self):
"""
Disconnection has been requested, lets make sure we clean up
"""
super().disconnect_callback()
def serve_once(self):
"""
Signal that we are connected to the Webex Teams Service and hang around waiting for disconnection request
"""
self.connect_callback()
try:
while True:
async def _run():
logging.debug("Opening websocket connection to %s" % self.device_info['webSocketUrl'])
async with websockets.connect(self.device_info['webSocketUrl']) as ws:
logging.info("WebSocket Opened\n")
msg = {'id' : str(uuid.uuid4()),
'type': 'authorization',
'data': {
'token': 'Bearer ' + self._bot_token
}
}
await ws.send(json.dumps(msg))
while True:
message = await ws.recv()
logging.debug("WebSocket Received Message(raw): %s\n" % message)
try:
loop = asyncio.get_event_loop()
loop.run_in_executor(None, self.process_websocket, message)
except:
logging.warning('An exception occurred while processing message. Ignoring. ')
asyncio.get_event_loop().run_until_complete(_run())
except KeyboardInterrupt:
log.info("Interrupt received, shutting down..")
return True
finally:
self.disconnect_callback()
def _get_device_info(self):
"""
Setup device in Webex Teams to bridge events across websocket
:return:
"""
logging.debug('Getting device list from Webex Teams')
try:
resp = self.webex_teams_api._session.get(DEVICES_URL)
for device in resp['devices']:
if device['name'] == DEVICE_DATA['name']:
self.device_info = device
return device
except webexteamssdk.ApiError:
pass
logging.info('Device does not exist in Webex Teams, creating')
resp = self.webex_teams_api._session.post(DEVICES_URL, json=DEVICE_DATA)
if resp is None:
raise FailedToCreateWebexDevice("Could not create Webex Teams device using {}".format(DEVICES_URL))
self.device_info = resp
return resp
def change_presence(self, status=OFFLINE, message=''):
"""
Backend: Change presence yet to be implemented
:param status:
:param message:
:return:
"""
log.debug("Backend: Change presence yet to be implemented") # TODO
pass
def prefix_groupchat_reply(self, message, identifier):
"""
Backend: Prefix group chat reply yet to be implemented
:param message:
:param identifier:
:return:
"""
log.debug("Backend: Prefix group chat reply yet to be implemented") # TODO
pass
def remember(self, id, key, value):
"""
Save the value of a key to a dictionary specific to a Webex Teams room or person
This is available in backend to provide easy access to variables that can be shared between plugins
:param id: Webex Teams ID of room or person
:param key: The dictionary key
:param value: The value to be assigned to the key
"""
values = self.recall(id)
values[key] = value
self[id] = values
def forget(self, id, key):
"""
Delete a key from a dictionary specific to a Webex Teams room or person
:param id: Webex Teams ID of room or person
:param key: The dictionary key
:return: The popped value or None if the key was not found
"""
values = self.recall(id)
value = values.pop(key, None)
self[id] = values
return value
def recall(self, id):
"""
Access a dictionary for a room or person using the Webex Teams ID as the key
:param id: Webex Teams ID of room or person
:return: A dictionary. If no dictionary was found an empty dictionary will be returned.
"""
values = self.get(id)
return values if values else {}
def recall_key(self, id, key):
"""
Access the value of a specific key from a Webex Teams room or person dictionary
:param id: Webex Teams ID of room or person
:param key: The dictionary key
:return: Either the value of the key or None if the key is not found
"""
return self.recall(id).get(key)
@staticmethod
def _unpickle_identifier(identifier_str):
return CiscoWebexTeamsBackend.__build_identifier(identifier_str)
@staticmethod
def _pickle_identifier(identifier):
return CiscoWebexTeamsBackend._unpickle_identifier, (str(identifier),)
def _register_identifiers_pickling(self):
"""
Register identifiers pickling.
"""
CiscoWebexTeamsBackend.__build_identifier = self.build_identifier
for cls in (CiscoWebexTeamsPerson, CiscoWebexTeamsRoomOccupant, CiscoWebexTeamsRoom):
copyreg.pickle(cls, CiscoWebexTeamsBackend._pickle_identifier, CiscoWebexTeamsBackend._unpickle_identifier)