class IMListenerImpl(IMListener): def __init__(self, sym_bot_client): self.bot_client = sym_bot_client self.message_parser = SymMessageParser() self.watchlist = [] async def on_im_message(self, im_message): logging.debug('IM Message Received') msg_text = self.message_parser.get_text(im_message) first_name = self.message_parser.get_im_first_name(im_message) stream_id = self.message_parser.get_stream_id(im_message) command = msg_text[0] if command == '/watch': if len(msg_text) != 2: response = 'Usage: /watch [currency]' else: currency = msg_text[1] self.watchlist.append(currency) response = f'Watching {currency}' elif command == '/list': items = ''.join(f'<li>{c} @ {get_fx_rate(c)}</li>' for c in self.watchlist) response = f'Watchlist:<ul>{items}</ul>' message = f'<messageML> {response}</messageML>' self.bot_client.get_message_client().send_msg(stream_id, dict(message=message)) async def on_im_created(self, im_created): logging.debug('IM created', im_created)
def setUp(self): logging.debug('testing Message Parser File:') self.message_parser = SymMessageParser() self.test_message_json = { "messageId": "LKX55AtesGPxrv16ed_TIX___pMru4hmbQ", "timestamp": 1566929352601, "message": "<div data-format=\"PresentationML\" data-version=\"2.0\" class=\"wysiwyg\"><p><span class=\"entity\" data-entity-id=\"0\">$aapl</span> <span class=\"entity\" data-entity-id=\"1\">$amzn</span> <span class=\"entity\" data-entity-id=\"2\">$nasdaq</span></p></div>", "data": "{\"0\":{\"id\":[{\"type\":\"org.symphonyoss.fin.security.id.ticker\",\"value\":\"aapl\"}],\"type\":\"org.symphonyoss.fin.security\",\"version\":\"1.0\"},\"1\":{\"id\":[{\"type\":\"org.symphonyoss.fin.security.id.ticker\",\"value\":\"amzn\"}],\"type\":\"org.symphonyoss.fin.security\",\"version\":\"1.0\"},\"2\":{\"id\":[{\"type\":\"org.symphonyoss.fin.security.id.ticker\",\"value\":\"nasdaq\"}],\"type\":\"org.symphonyoss.fin.security\",\"version\":\"1.0\"}}", "user": { "userId": 349026222344902, "firstName": "Reed", "lastName": "Feldman", "displayName": "Reed Feldman (Develop 2)", "email": "*****@*****.**", "username": "******" }, "stream": { "streamId": "sOKpwRk_5_N838P10ATuFX___pNk9zJndA", "streamType": "ROOM" }, "externalRecipients": "false", "userAgent": "DESKTOP-40.0.0-10665-MacOSX-10.14.6-Chrome-76.0.3809.100", "originalFormat": "com.symphony.messageml.v2" }
class RoomProcessor: def __init__(self, bot_client): self.bot_client = bot_client self.message_formatter = MessageFormatter() self.sym_message_parser = SymMessageParser() #hard code to the userId of bot you are using. self.bot_id = '349026222344891' self.default_message = self.message_formatter.format_message('type @karlPythonDemo help to view commands') def process_room_message(self, msg): logging.debug('room_processor/process_room_message()') logging.debug(json.dumps(msg, indent=4)) self.help_message = dict(message = """<messageML> <h2>This is an demo of how to create, update, and submit an expense form using Symphony Elements</h2> <p>Type @karlPythonDemo expense to view expense approval form</p> </messageML> """) mentioned_users = self.sym_message_parser.get_mention_ids(msg) commands = self.sym_message_parser.get_text(msg) if mentioned_users: if mentioned_users[0] == self.bot_id and commands[0] == 'help': print('in room') self.bot_client.get_message_client().send_msg(msg['stream']['streamId'], self.help_message) if mentioned_users[0] == self.bot_id and commands[0] == 'expense': self.bot_client.get_message_client().send_msg(msg['stream']['streamId'], render_expense_approval_form('listeners/expense_approval_form/html/expense_approval_table.html')) else: self.bot_client.get_message_client().send_msg(msg['stream']['streamId'], self.default_message)
def __init__(self, bot_client, msg): self.bot_client = bot_client self.msg = msg self.bot_id = '349026222344891' self.message_formatter = MessageFormatter() self.sym_message_parser = SymMessageParser() self.process(self.msg)
def __init__(self, bot_client): self.bot_client = bot_client self.message_formatter = MessageFormatter() self.sym_message_parser = SymMessageParser() #hard code to the userId of bot you are using. self.bot_id = '349026222344891' self.default_message = self.message_formatter.format_message('type @karlPythonDemo help to view commands')
class AsyncIMListenerImp(IMListener): """Example implementation of IMListener with asynchronous functionality Call the bot with /wait to see an example of a non-blocking wait """ def __init__(self, sym_bot_client): self.bot_client = sym_bot_client self.message_parser = SymMessageParser() async def on_im_message(self, msg): logging.debug('message received in IM', msg) msg_text = self.message_parser.get_text(msg) if "/wait" in msg_text: await asyncio.sleep(5) msg_to_send = dict( message= '<messageML>Hello {}, sorry to keep you waiting!</messageML>'. format(self.message_parser.get_im_first_name(msg))) else: msg_to_send = dict( message= '<messageML>Hello {}, hope you are doing well!</messageML>'. format(self.message_parser.get_im_first_name(msg))) if msg_text: stream_id = self.message_parser.get_stream_id(msg) self.bot_client.get_message_client(). \ send_msg(stream_id, msg_to_send) async def on_im_created(self, im_created): logging.debug("IM created!", im_created)
class IMProcessor: def __init__(self, bot_client): self.bot_client = bot_client self.message_formatter = MessageFormatter() self.sym_message_parser = SymMessageParser() #reads message and processes it #look inside logs/example.log to see the payload (metadata representing event coming over the datafeed) def process(self, msg): logging.debug('im_processor/process_im_message()') logging.debug(json.dumps(msg, indent=4)) self.help_message = dict(message="""<messageML> <h3>Type '/elements' to render a form</h3> </messageML> """) commands = self.sym_message_parser.get_text(msg) if commands[0] == '/elements': self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], render_simple_form( 'listeners/simple_form/html/simple_form.html')) else: self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.help_message)
class MessageProcessor: def __init__(self, bot_client): self.bot_client = bot_client self.message_parser = SymMessageParser() def process(self, msg): logging.debug('insdie of process') msg_text = self.message_parser.get_text(msg) msg_to_send = dict( message='<messageML>Hello {}, hope you are doing well!</messageML>' .format(self.message_parser.get_im_first_name(msg))) if msg_text: stream_id = self.message_parser.get_stream_id(msg) self.bot_client.get_message_client(). \ send_msg(stream_id, msg_to_send)
class IMProcessor: def __init__(self, bot_client, msg): self.bot_client = bot_client self.msg = msg self.bot_id = '349026222344891' self.message_formatter = MessageFormatter() self.sym_message_parser = SymMessageParser() self.process(self.msg) #reads message and processes it #look inside logs/example.log to see the payload (metadata representing event coming over the datafeed) def process(self, msg): logging.debug('im_processor/process_im_message()') logging.debug(json.dumps(msg, indent=4)) self.help_message = dict(message="""<messageML> <h3>Use ExpenseBot to create, update, and submit an expense form using Symphony Elements</h3> <p>Type @karlPythonDemo <b>'create expense'</b> to create an expense approval form</p> <p>In order to assign your expense approval form to your manager, you must first add an expense</p> </messageML> """) mentioned_users = self.sym_message_parser.get_mention_ids(msg) commands = self.sym_message_parser.get_text(msg) if mentioned_users: if mentioned_users[0] == self.bot_id and commands[0] == 'help': self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.help_message) elif mentioned_users[0] == self.bot_id and commands[ 0] == 'create' and commands[1] == 'expense': expense_data['ExpenseApprovalForm'][ 'person_name'] = self.sym_message_parser.get_im_name(msg) self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], render_expense_approval_form( 'listeners/expense_approval_form/html/create_expense_approval_form.html' )) else: print('catching else') self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.help_message) else: self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.help_message)
class IMListenerImpl(IMListener): def __init__(self, sym_bot_client): self.bot_client = sym_bot_client self.message_parser = SymMessageParser() async def on_im_message(self, im_message): logging.debug('IM Message Received') msg_text = self.message_parser.get_text(im_message) first_name = self.message_parser.get_im_first_name(im_message) stream_id = self.message_parser.get_stream_id(im_message) message = f'<messageML>Hello {first_name}, hope you are doing well!</messageML>' self.bot_client.get_message_client().send_msg(stream_id, dict(message=message)) async def on_im_created(self, im_created): logging.debug('IM created', im_created)
class MessageProcessor: def __init__(self, bot_client): self.bot_client = bot_client self.message_parser = SymMessageParser() def process(self, msg): logging.debug('insdie of process') msg_text = self.message_parser.get_text(msg) if not msg_text: return if re.match("Holding", msg_text[0]): f = BloombergProcessor() reply = f.get_response() stream_id = self.message_parser.get_stream_id(msg) config.global_stream_id = stream_id msg_to_send = dict(message=reply) self.bot_client.get_message_client(). \ send_msg(stream_id, msg_to_send)
class RoomListenerImpl(RoomListener): def __init__(self, sym_bot_client): self.bot_client = sym_bot_client self.message_parser = SymMessageParser() async def on_room_msg(self, room_message): logging.debug('Room Message Received') msg_text = self.message_parser.get_text(room_message) first_name = self.message_parser.get_im_first_name(room_message) stream_id = self.message_parser.get_stream_id(room_message) message = f'<messageML>Hello {first_name}, hope you are doing well!</messageML>' self.bot_client.get_message_client().send_msg(stream_id, dict(message=message)) async def on_room_created(self, room_created): logging.debug('Room Created', room_created) async def on_room_updated(self, room_updated): logging.debug('Room Updated', room_updated) async def on_user_joined_room(self, user_joined_room): logging.debug('User Joined Room', user_joined_room) async def on_user_left_room(self, user_left_room): logging.debug('User Left Room', user_left_room) async def on_room_member_demoted_from_owner( self, room_member_demoted_from_owner): logging.debug('Room Member Demoted From Owner', room_member_demoted_from_owner) async def on_room_member_promoted_to_owner(self, room_member_promoted_to_owner): logging.debug('Room Member Promoted To Owner', room_member_promoted_to_owner) async def on_room_deactivated(self, room_deactivated): logging.debug('Room Deactivated', room_deactivated) async def on_room_reactivated(self, room_reactivated): logging.debug('Room Reactivated', room_reactivated)
class MessageProcessor: def __init__(self, bot_client): self.bot_client = bot_client self.message_parser = SymMessageParser() def process(self, msg): logging.debug('insdie of process') msg_text = self.message_parser.get_text(msg) if not msg_text: return if re.match("flagdeals", msg_text[0]): f = FlagDealsProcessor() reply = f.get_response() stream_id = self.message_parser.get_stream_id(msg) config.global_stream_id = stream_id msg_to_send = dict(message=reply) self.bot_client.get_message_client(). \ send_msg(stream_id, msg_to_send) #else: msg_to_send = dict( message= '<messageML>Hello {}, hope you are doing well! "{}"</messageML>' .format(self.message_parser.get_im_first_name(msg), msg_text))
class GeneralRoomListener(RoomListener): def __init__(self, SymBotClient, controllers): self._bot_client = SymBotClient self._controllers = controllers self.msg_processor = SymMessageParser() def on_room_msg(self, message): logging.debug('room message recieved', message) streamId = self.msg_processor.get_stream_id(message) logging.debug(f"Stream ID: {streamId}") #sample code for developer to implement --> use MessageClient and #data recieved from message event to reply with a #reed try: msg = dict(message=self._controllers.render(message)) except UnknownCommandError as e: logging.debug(e) else: mc = self._bot_client.get_message_client() mc.send_msg(streamId, msg) def on_room_created(self, roomCreated): logging.debug('room created', roomCreated) def on_room_deactivated(self, roomDeactivated): logging.debug('room Deactivated', roomDeactivated) def on_room_member_demoted_from_owner(self, roomMemberDemotedFromOwner): logging.debug('room member demoted from owner', roomMemberDemotedFromOwner) def on_room_member_promoted_to_owner(self, roomMemberPromotedToOwner): logging.debug('room member promoted to owner', roomMemberPromotedToOwner) def on_room_reactivated(self, roomReactivated): logging.debug('room reactivated', roomReactivated) def on_room_updated(self, roomUpdated): logging.debug('room updated', roomUpdated) def on_user_joined_room(self, userJoinedRoom): logging.debug('USER JOINED ROOM', userJoinedRoom) def on_user_left_room(self, userLeftRoom): logging.debug('USER LEFT ROOM', userLeftRoom)
class IMProcessor: def __init__(self, bot_client): self.bot_client = bot_client self.bot_id = self.bot_client.bot_id self.messages = Messages(self.bot_id) self.sym_message_parser = SymMessageParser() self.data_service = DataService() def user_has_active_expense(self, streamId, userObj): if self.data_service.has_active_expense_report(userObj['userId']): self.bot_client.get_message_client().send_msg( streamId, self.messages.end_message) return True else: return False def handle_create_expense(self, streamId, userObj): if self.user_has_active_expense(streamId, userObj): return else: logging.debug('new expense report requested by {}'.format( userObj['displayName'])) self.bot_client.get_message_client().send_msg( streamId, self.messages.create_message) def process(self, msg): logging.debug('im_processor/process_im_message()') logging.debug(json.dumps(msg, indent=4)) userId = msg['user']['userId'] mentioned_users = self.sym_message_parser.get_mention_ids(msg) commands = self.sym_message_parser.get_text(msg) if mentioned_users: if mentioned_users[0] == self.bot_id and commands[1] == 'clear': self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.messages.clear_message) elif mentioned_users[0] == self.bot_id and commands[1] == 'create': self.handle_create_expense(msg['stream']['streamId'], msg['user']) elif mentioned_users[0] == self.bot_id and commands[ 1] == 'upload' and commands[2] == 'receipt': img_data = parse_attachment(msg, self.bot_client) print(img_data) save_image(self.bot_client, userId, img_data) self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], render_expense_approval_from_message( userId, './listeners/expense_approval_form/html/create_expense_approval_form.html' )) elif mentioned_users[0] == self.bot_id and commands[1] == 'end': expenses = ExpenseReport.objects(owner=str(userId)) expenses.delete() self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.messages.instruction_message) else: self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.messages.help_message) else: self.bot_client.get_message_client().send_msg( msg['stream']['streamId'], self.messages.help_message)
def __init__(self, bot_client): self.bot_client = bot_client self.message_parser = SymMessageParser()
def __init__(self, SymBotClient, controllers): self._bot_client = SymBotClient self._controllers = controllers self.msg_processor = SymMessageParser()
def __init__(self, bot_client): self.bot_client = bot_client self.bot_id = self.bot_client.bot_id self.messages = Messages(self.bot_id) self.sym_message_parser = SymMessageParser() self.data_service = DataService()
def __init__(self, bot_client): self.bot_client = bot_client self.message_formatter = MessageFormatter() self.sym_message_parser = SymMessageParser() self.stream_client = StreamClient(bot_client)
def __init__(self, sym_bot_client): self.bot_client = sym_bot_client self.message_parser = SymMessageParser() self.watchlist = []