def handle_message(self, message: Dict[str, Any], **kwargs: Any) -> Union[Response, Iterable[Response]]: result: Optional[Tuple[str, CommandParser.Opts, CommandParser.Args]] if not self.client.user_is_privileged(message['sender_id']): return Response.admin_err(message) result = self.command_parser.parse(message['command']) if result is None: return Response.command_not_found(message) command, _, args = result if command == 'list': response: str = 'Key | Value\n ---- | ----' for key, value in self._db.execute(self._list_sql): response += f'\n{key} | {value}' return Response.build_message(message, response) if command == 'remove': self._db.execute(self._remove_sql, args.key, commit=True) return Response.ok(message) if command == 'set': try: self._db.execute(self._update_sql, args.key, args.value, commit=True) except Exception as e: logging.exception(e) return Response.build_message(message, 'Failed: %s' % str(e)) return Response.ok(message) return Response.command_not_found(message)
def handle_message( self, message: Dict[str, Any], **kwargs: Any ) -> Union[Response, Iterable[Response]]: result: Optional[Tuple[str, CommandParser.Opts, CommandParser.Args]] result_sql: List[Tuple[Any, ...]] if not self.client.user_is_privileged(message['sender_id']): return Response.admin_err(message) # Get command and parameters. result = self.command_parser.parse(message['command']) if result is None: return Response.command_not_found(message) command, _, args = result if command == 'list': result_sql = self._db.execute(self._list_sql) response: str = 'Alert word or phrase | Emoji\n---- | ----' for (phrase, emoji) in result_sql: response += '\n`{0}` | {1} :{1}:'.format(phrase, emoji) return Response.build_message(message, response) # Use lowercase -> no need for case insensitivity. alert_phrase: str = args.alert_phrase.lower() if command == 'add': # Add binding to database or update it. self._db.execute(self._update_sql, alert_phrase, args.emoji, commit = True) elif command == 'remove': self._db.execute(self._remove_sql, alert_phrase, commit = True) return Response.ok(message)
def _move_stream( self, message: Dict[str, Any], dest: str ) -> Union[Response, Iterable[Response]]: # Get destination stream name. dest_stream: Optional[str] = Regex.get_stream_name(dest) if dest_stream is None: return Response.command_not_found(message) # Get destination stream id. result: Dict[str, Any] = self.client.get_stream_id(dest_stream) if result['result'] != 'success': return Response.error(message) dest_stream_id: int = result['stream_id'] # Get the stream id ... src_stream_id: int = message['stream_id'] # ... and the topic of the current message. topic: str = message['subject'] # Get message which started the topic. request: Dict[str, Any] = { 'anchor': 'oldest', 'num_before': 0, 'num_after': 1, 'narrow': [ { 'operator': 'stream', 'operand': src_stream_id }, { 'operator': 'topic', 'operand': topic } ] } result = self.client.get_messages(request) if result['result'] != 'success': return Response.error(message) first_message: Dict[str, Any] = result['messages'][0] # Move message (and all following in the same topic) = move topic. request = { 'message_id': first_message['id'], 'topic': topic, 'stream_id': dest_stream_id, 'send_notification_to_old_thread': False, 'propagate_mode': 'change_all' } result = self.client.update_message(request) if result['result'] != 'success': return Response.error(message) # Remove requesting message. self.client.delete_message(message['id']) return Response.build_message( first_message, self.msg_template_stream.format(first_message['sender_full_name'], topic, dest_stream), msg_type = 'private' )
def handle_message( self, message: Dict[str, Any], **kwargs: Any ) -> Union[Response, Iterable[Response]]: result: Optional[Tuple[str, CommandParser.Opts, CommandParser.Args]] # Get command and parameters. result = self.command_parser.parse(message['command']) if result is None: return Response.command_not_found(message) command, _, args = result if command == 'subscribe': return self._subscribe(message['sender_id'], args.group_id, message) if command == 'unsubscribe': return self._unsubscribe(message['sender_id'], args.group_id, message) if not self.client.user_is_privileged(message['sender_id']): return Response.admin_err(message) if command == 'list': return self._list(message) if command == 'announce': if message['type'] != 'stream': return Response.build_message(message, 'Claim only stream messages.') return self._announce(message) if command == 'unannounce': return self._unannounce(message, args.message_id) if command == 'claim': if message['type'] != 'stream': return Response.build_message(message, 'Claim only stream messages.') return self._claim(message, args.group_id) if command == 'unclaim': return self._unclaim(message, args.group_id, args.message_id) if command == 'add': return self._add(message, args.group_id, args.emoji) if command == 'remove': return self._remove(message, args.group_id) if command in ['add_streams', 'remove_streams']: return self._change_streams(message, args.group_id, command, args.streams) return Response.command_not_found(message)
def handle_message( self, message: Dict[str, Any], **kwargs: Any ) -> Union[Response, Iterable[Response]]: result: Optional[Tuple[str, CommandParser.Opts, CommandParser.Args]] result_sql: List[Tuple[Any, ...]] if not self.client.user_is_privileged(message['sender_id']): return Response.admin_err(message) # Get command and parameters. result = self.command_parser.parse(message['command']) if result is None: return Response.command_not_found(message) command, _, args = result if command == 'list': response: str = '***List of Identifiers and Messages***\n' for (ident, text) in self._db.execute(self._list_sql): response += '\n--------\nTitle: **{}**\n{}'.format(ident, text) return Response.build_message(message, response) # Use lowercase -> no need for case insensitivity. ident = args.id.lower() if command == 'send': result_sql = self._db.execute(self._search_sql, ident) if not result_sql: return Response.command_not_found(message) # Remove requesting message. self.client.delete_message(message['id']) return Response.build_message(message, result_sql[0][0]) if command == 'add': self._db.execute(self._update_sql, ident, args.text, commit = True) return Response.ok(message) if command == 'remove': self._db.execute(self._delete_sql, ident, commit = True) return Response.ok(message) return Response.command_not_found(message)
def handle_message(self, message: Dict[str, Any], **kwargs: Any) -> Union[Response, Iterable[Response]]: result: Optional[Tuple[str, CommandParser.Opts, CommandParser.Args]] result = self.command_parser.parse(message['command']) if result is None: return Response.command_not_found(message) command, _, args = result if command == 'streams': return self.subscribe_streams(message, args.dest_stream, args.streams) if command == 'users': return self.subscribe_users(message, args.dest_stream, args.users) if command == 'user_emails': return self.subscribe_user_emails(message, args.dest_stream, args.user_emails) if command == 'all_users': return self.subscribe_all_users(message, args.dest_stream) return Response.command_not_found(message)
def _help_command(self, message: Dict[str, Any], command: str) -> Union[Response, Iterable[Response]]: info_tuple: Optional[Tuple[str, str, str]] = None for ituple in self.help_info: if ituple[0] == command: info_tuple = ituple break if info_tuple is None: return Response.command_not_found(message) help_message: str = '\n'.join(info_tuple[1:]) return Response.build_message(message, help_message, msg_type='private', to=message['sender_email'])
def handle_message( self, message: Dict[str, Any], **kwargs: Any ) -> Union[Response, Iterable[Response]]: result: Optional[Tuple[str, CommandParser.Opts, CommandParser.Args]] if not self.client.user_is_privileged(message['sender_id']): return Response.admin_err(message) if 'stream_id' not in message: return Response.build_message(message, 'Error: Not a stream topic.') result = self.command_parser.parse('move ' + message['command']) if result is None: return Response.command_not_found(message) _, opts, args = result dest: str = ' '.join(args.dest) if opts.m is not None: return self._move_topic(message, dest, opts.m) return self._move_stream(message, dest)