|
5 | 5 | import logging
|
6 | 6 | import uuid
|
7 | 7 | import websockets
|
| 8 | +import string |
| 9 | +import random |
| 10 | +import os |
8 | 11 | from markdown import markdown
|
9 | 12 |
|
10 | 13 | from errbot.core import ErrBot
|
11 |
| -from errbot.backends.base import Message, Person, Room, RoomOccupant, OFFLINE, RoomDoesNotExistError |
| 14 | +from errbot.backends.base import Message, Person, Room, RoomOccupant, OFFLINE, RoomDoesNotExistError, Stream |
12 | 15 | from errbot import rendering
|
13 | 16 |
|
14 | 17 | import webexteamssdk
|
15 | 18 |
|
| 19 | +__version__="1.4.0" |
| 20 | + |
16 | 21 | log = logging.getLogger('errbot.backends.CiscoWebexTeams')
|
17 | 22 |
|
18 | 23 | CISCO_WEBEX_TEAMS_MESSAGE_SIZE_LIMIT = 7439
|
|
24 | 29 | "deviceType" : "DESKTOP",
|
25 | 30 | "localizedModel": "python",
|
26 | 31 | "model" : "python",
|
27 |
| - "name" : "python-webex-teams-client", |
| 32 | + "name" : f"python-webex-teams-client-{''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(5))}", |
28 | 33 | "systemName" : "python-webex-teams-client",
|
29 | 34 | "systemVersion" : "0.1"
|
30 | 35 | }
|
@@ -523,6 +528,47 @@ def send_message(self, mess):
|
523 | 528 | else:
|
524 | 529 | self.webex_teams_api.messages.create(roomId=mess.to.room.id, text=mess.body, markdown=md)
|
525 | 530 |
|
| 531 | + def _teams_upload(self, stream): |
| 532 | + """ |
| 533 | + Performs an upload defined in a stream |
| 534 | + :param stream: Stream object |
| 535 | + :return: None |
| 536 | + """ |
| 537 | + |
| 538 | + try: |
| 539 | + stream.accept() |
| 540 | + log.exception(f'Upload of {stream.raw.name} to {stream.identifier} has started.') |
| 541 | + |
| 542 | + if type(stream.identifier) == CiscoWebexTeamsPerson: |
| 543 | + self.webex_teams_api.messages.create(toPersonId=stream.identifier.id, files=[stream.raw.name]) |
| 544 | + else: |
| 545 | + self.webex_teams_api.messages.create(roomId=stream.identifier.room.id, files=[stream.raw.name]) |
| 546 | + |
| 547 | + stream.success() |
| 548 | + log.exception(f'Upload of {stream.raw.name} to {stream.identifier} has completed.') |
| 549 | + |
| 550 | + except Exception: |
| 551 | + stream.error() |
| 552 | + log.exception(f'Upload of {stream.raw.name} to {stream.identifier} has failed.') |
| 553 | + |
| 554 | + finally: |
| 555 | + stream.close() |
| 556 | + |
| 557 | + def send_stream_request(self, identifier, fsource, name='file', size=None, stream_type=None): |
| 558 | + """ |
| 559 | + Send a file to Cisco Webex Teams |
| 560 | +
|
| 561 | + :param user: is the identifier of the person you want to send it to. |
| 562 | + :param fsource: is a file object you want to send. |
| 563 | + :param name: is an optional filename for it. |
| 564 | + :param size: not supported in Webex Teams backend |
| 565 | + :param stream_type: not supported in Webex Teams backend |
| 566 | + """ |
| 567 | + log.debug(f'Requesting upload of {fsource.name} to {identifier}.') |
| 568 | + stream = Stream(identifier, fsource, name, size, stream_type) |
| 569 | + self.thread_pool.apply_async(self._teams_upload, (stream,)) |
| 570 | + return stream |
| 571 | + |
526 | 572 | def build_reply(self, mess, text=None, private=False, threaded=False):
|
527 | 573 | """
|
528 | 574 | Build a reply in the format expected by errbot by swapping the to and from source and destination
|
@@ -563,6 +609,8 @@ async def _run():
|
563 | 609 | }
|
564 | 610 | await ws.send(json.dumps(msg))
|
565 | 611 |
|
| 612 | + self.reset_reconnection_count() |
| 613 | + |
566 | 614 | while True:
|
567 | 615 | message = await ws.recv()
|
568 | 616 | logging.debug("WebSocket Received Message(raw): %s\n" % message)
|
|
0 commit comments