def _recv_sub_socket(self): while True: frame = self.sub_socket.recv_multipart() message = decode_vex_message(frame) if message.type == 'MSG': if not self._duplicate_message(message): user = message.contents.get('author', message.source) msg = message.contents.get('message') if msg: self.message_signal.emit(message.source, user, msg) elif message.type == 'CMD': command = message.contents.get('command') if command == 'clear': self.clear_signal.emit() elif message.type == 'STATUS': state = message.contents.get('status') if state == 'CONNECTED': state = True elif state == 'DISCONNECTED': state = False else: continue self.connected_signal.emit(state, message.source)
def _publish_helper(self, msg): msg = decode_vex_message(msg) self.chatter.on_next(msg) # FIXME: Awkward way to replace the uuid by creating a new vexmessage msg = create_vex_message(msg.target, msg.source, self.uuid, **msg.contents) self.loop.add_callback(self.subscription_socket.send_multipart, msg) self._heartbeat_helper.message_recieved()
async def _run( messaging, live_chat_messages, live_chat_id, ): command_manager = AdapterCommandManager(messaging) frame = None while True: try: frame = messaging.sub_socket.recv_multipart(zmq.NOBLOCK) except zmq.error.ZMQError: pass if frame: message = decode_vex_message(frame) if message.type == 'CMD': command_manager.parse_commands(message) elif message.type == 'RSP': message = message.contents.get('response') body = { 'snippet': { 'type': 'textmessageEvent', 'liveChatId': live_chat_id, 'textMessageDetails': { 'messageText': message } } } live_chat_messages.insert(part='snippet', body=body).execute() frame = None await asyncio.sleep(1)
def run(database_filepath: str=None, addresses: list=None): if database_filepath is None: database_filepath = _get_database_filepath() if addresses is None: addresses = ['tcp://127.0.0.1:4001'] database = DatabaseManager(database_filepath) context = zmq.Context() socket = context.socket(zmq.SUB) socket.setsockopt(zmq.SUBSCRIBE, b'') for address in addresses: socket.connect(address) while True: frame = socket.recv_multipart() try: msg = decode_vex_message(frame) except Exception: continue if msg.type == 'MSG': author = msg.contents.get('author', msg.source) contents = msg.contents.get('message', None) if contents and author: database.record_message(msg.source, author, contents)
def test_send_targeted_message(self): self.test_subscribe_socket.setsockopt_string(zmq.SUBSCRIBE, 'test') self.messaging.send_message('test', test='blue') frame = self.test_subscribe_socket.recv_multipart() message = decode_vex_message(frame) self.assertEqual(message.target, 'test') self.assertEqual(message.source, 'robot') self.assertEqual(message.contents['test'], 'blue') self.assertEqual(message.type, 'MSG')
def run(self): while True: frame = self.messaging.sub_socket.recv_multipart() message = decode_vex_message(frame) if message.type == 'CMD': self.command_manager.parse_commands(message) elif message.type == 'RSP': channel = message.contents.get('channel') contents = message.contents.get('response') self.send_message(channel, contents, mtype='groupchat')
def handle_subscription(self): while True: frame = self.messaging.sub_socket.recv_multipart() message = decode_vex_message(frame) if message.type == 'CMD': self.command_manager.parse_commands(message) if message.type == 'RSP': data = {} data['name'] = 'message' data['args'] = [message.contents.get('response'), self._streamer_name] self.send_packet_helper(5, data)
def handle_subscription(self): while True: frame = self.messaging.sub_socket.recv_multipart() message = decode_vex_message(frame) if message.type == 'CMD': self.command_manager.parse_commands(message) if message.type == 'RSP': data = {} data['name'] = 'message' data['args'] = [ message.contents.get('response'), self._streamer_name ] self.send_packet_helper(5, data)
def run(self): while True: frame = self.messaging.subscription_socket.recv_multipart() msg = None try: msg = decode_vex_message(frame) except Exception: pass if msg: # Right now this is hardcoded into being only # the shell adapter # change this to some kind of auth code if ((msg.source == 'shell' or msg.source == 'command_line') and msg.type == 'CMD'): self.command_manager.parse_commands(msg)
def run(self): while True: frame = self.subscription_socket.recv_multipart() msg = decode_vex_message(frame) # add one to the counter self._counter += 1 if msg.type == 'MSG': # TODO: add in some `None` parsing here string = msg.contents.get('message') # TODO: move into the classify parser # msg = util.clean_text(msg) parse_result = [] for parser in self.parsers: result = parser.parse(string) # TODO: do more complex parsing here # currently just get the first result and stop if result: parse_result.extend(result) break for result in parse_result: past_count = self._memory.get(result, 0) # check to see if this was responded to recently and # not respond if so if (self._counter - past_count > 8 or past_count == 0 or string[0] == '!'): keys_to_pop = ['source', 'to', 'response', 'author', 'message'] past_msg_content = dict(msg.contents) _pop_helper(past_msg_content, keys_to_pop) # FIXME frame = create_vex_message(msg.source, 'vexparser', 'RSP', author='Vex', to=msg.contents.get('author'), response=result, **past_msg_content) self.publish_socket.send_multipart(frame) self._memory[result] = self._counter
def run(self): while True: frame = self.audio_socket.recv_multipart() message = decode_vex_message(frame) if message.type == 'AUDIO': sample_rate = message.contents['sample_rate'] sample_width = message.contents['sample_width'] number_channels = message.contents['number_channels'] stream_data = message.contents['audio'] msg = self.speechtotext.get_msg(stream_data, sample_rate, sample_width, number_channels) """ filename = '{}.wav'.format(uuid.uuid4()) with wave.open(filename, 'wb') as f: f.setnchannels(number_channels) f.setsampwidth(sample_width) f.setframerate(sample_rate) f.writeframes(stream_data) logging.error(msg) """ if msg: try: message = msg.get('alternative')[0]['transcript'] alternative = msg.get('alternative') except AttributeError: message = msg alternative = None response = create_vex_message('', 'speechtotext', 'MSG', message=message, alternative=alternative) self.publish_socket.send_multipart(response) else: pass
async def _check_subscription(bot): while True: await asyncio.sleep(1) msg = None try: msg = bot.messaging.sub_socket.recv_multipart(zmq.NOBLOCK) except zmq.error.Again: pass if msg: msg = decode_vex_message(msg) if msg.type == 'CMD': bot.command_parser.parse_commands(msg) elif msg.type == 'RSP': channel = msg.contents.get('channel') message = msg.contents.get('response') bot.privmsg(channel, message) bot.messaging.send_message(author=bot.nick, message=message, channel=channel)
def run(self): frame = None while True and not self._exit_loop: try: # NOTE: not blocking here to check the _exit_loop condition frame = self.messaging.sub_socket.recv_multipart(zmq.NOBLOCK) except zmq.error.ZMQError: pass sleep(.5) if frame: message = decode_vex_message(frame) if message.type == 'RSP': self.stdout.write("\n{}\n".format(self.doc_leader)) header = message.contents.get('original', 'Response') contents = message.contents.get('response', None) # FIXME if (isinstance(header, (tuple, list)) and isinstance(contents, (tuple, list)) and contents): for head, content in zip(header, contents): self.print_topics(head, (contents,), 15, 70) else: if isinstance(contents, str): contents = (contents,) self.print_topics(header, contents, 15, 70) self.stdout.write("vexbot: ") self.stdout.flush() else: # FIXME print(message.type, message.contents, 'fix me in shell adapter, run function') frame = None
def run(self): messaging = self.messaging # TODO: create better type here startup_frame = create_vex_message('', 'microphone', 'STATUS', status='recording') messaging.publish_socket.send_multipart(startup_frame) while True: # NOTE: `frame` is a list of byte strings # Once we recv here, MUST reply in order to loop again! try: frame = self.messaging.subscribe_socket.recv_multipart() except KeyboardInterrupt: break msg = decode_vex_message(frame) if msg.type == 'CMD': self.command_manager.handle_command(msg)
def _recv_loop(self): frame = None while True and self._shell.running: try: # NOTE: not blocking here to check the _exit_loop condition frame = self.messaging.sub_socket.recv_multipart(zmq.NOBLOCK) except zmq.error.ZMQError: pass _sleep(.5) if frame: message = decode_vex_message(frame) print(message) # NOTE: No message type other than `RSP` currently handeled if message.type == 'RSP': s = "\n{}\n".format(self._shell.doc_leader) self._shell.stdout.write(s) header = message.contents.get('original', 'Response') contents = message.contents.get('response', None) # FIXME -------------------------------------- if (isinstance(header, (tuple, list)) and isinstance(contents, (tuple, list)) and contents): for head, content in zip(header, contents): self._shell.print_topics(head, (contents, ), 15, 70) else: if isinstance(contents, str): contents = (contents, ) self._shell.print_topics(header, contents, 15, 70) # ENDFIXME ---------------------------------- self._shell.stdout.write("vexbot: ") self._shell.stdout.flush() frame = None
def run(self): frame = None while True and not self._exit_loop: try: # NOTE: not blocking here to check the _exit_loop condition frame = self.messaging.sub_socket.recv_multipart(zmq.NOBLOCK) except zmq.error.ZMQError: pass sleep(.5) if frame: message = decode_vex_message(frame) if message.type == 'RSP': self.stdout.write("\n{}\n".format(self.doc_leader)) header = message.contents.get('original', 'Response') contents = message.contents.get('response', None) # FIXME if (isinstance(header, (tuple, list)) and isinstance(contents, (tuple, list)) and contents): for head, content in zip(header, contents): self.print_topics(head, (contents, ), 15, 70) else: if isinstance(contents, str): contents = (contents, ) self.print_topics(header, contents, 15, 70) self.stdout.write("vexbot: ") self.stdout.flush() else: # FIXME print(message.type, message.contents, 'fix me in shell adapter, run function') frame = None
async def _run(messaging, live_chat_messages, live_chat_id, ): command_manager = AdapterCommandManager(messaging) frame = None while True: try: frame = messaging.sub_socket.recv_multipart(zmq.NOBLOCK) except zmq.error.ZMQError: pass if frame: message = decode_vex_message(frame) if message.type == 'CMD': command_manager.parse_commands(message) elif message.type == 'RSP': message = message.contents.get('response') body={'snippet':{'type': 'textmessageEvent', 'liveChatId': live_chat_id, 'textMessageDetails': {'messageText': message}}} live_chat_messages.insert(part='snippet', body=body).execute() frame = None await asyncio.sleep(1)