def _announce( self, message: Dict[str, Any] ) -> Union[Response, Iterable[Response]]: table: str = '\n'.join( self._announcement_msg_table_row_fmt % (group_id, emoji) for group_id, emoji, _ in self._db.execute(self._list_sql) ) # Remove the requesting message. self.client.delete_message(message['id']) # Send own message. result: Dict[str, Any] = self.client.send_response( Response.build_message(message, self._announcement_msg.format(table)) ) if result['result'] != 'success': return Response.none() # Insert the id of the bot's message into the database. try: self._db.execute(self._claim_all_sql, result['id'], commit = True) except Exception as e: return Response.build_message(message, str(e)) # Get all the currently existant emojis. result_sql: List[Tuple[Any, ...]] = self._db.execute(self._get_all_emojis_sql) if not result_sql: return Response.none() # React with all those emojis on this message. for emoji in map(lambda t: cast(str, t[0]), result_sql): self.client.send_response(Response.build_reaction_from_id(result['id'], emoji)) return Response.none()
def handle_reaction_event( self, event: Dict[str, Any], ) -> Union[Response, Iterable[Response]]: group_id: Optional[str] = self._get_group_id_from_emoji_event( event['message_id'], event['emoji_name'] ) if group_id is None: return Response.none() if event['op'] == 'add': return self._subscribe(event['user_id'], group_id) if event['op'] == 'remove': return self._unsubscribe(event['user_id'], group_id) return Response.none()
def handle_message(self, message: Dict[str, Any], **kwargs: Any) -> Union[Response, Iterable[Response]]: if not self.client.user_is_privileged(message['sender_id']): return Response.admin_err(message) # Ask the parent process to restart. os.kill(os.getpid(), signal.SIGUSR1) # dead code return Response.none()
def handle_stream_event( self, event: Dict[str, Any], ) -> Union[Response, Iterable[Response]]: for stream in event['streams']: # Get all the groups this stream belongs to. group_ids: List[str] = self._get_group_ids_from_stream(stream['name']) # Get all user ids to subscribe to this new stream ... user_ids: List[int] = self._get_group_subscribers(group_ids) # ... and subscribe them. self.client.subscribe_users(user_ids, stream['name']) return Response.none()
def handle_event(self, event: Dict[str, Any], **kwargs: Any) -> Union[Response, Iterable[Response]]: if not self._bindings: return Response.none() # Get message content. # Replace markdown links by their textual representation. # Convert to lowercase. content: str = self._markdown_links\ .sub(r'\1', event['message']['content'])\ .lower() return [ Response.build_reaction(message=event['message'], emoji=emoji) for pattern, emoji in self._bindings if randint(1, 6) == 3 and pattern.search(content) is not None ]
def handle_event( self, event: Dict[str, Any], **kwargs: Any ) -> Union[Response, Iterable[Response]]: if event['op'] == 'create': for stream in event['streams']: self._handle_stream(stream['name'], stream['invite_only']) elif event['op'] == 'delete': for stream in event['streams']: self._remove_stream_from_table(stream['name']) elif event['op'] == 'update': if event['property'] == 'invite_only': self._handle_stream(event['name'], event['value']) elif (event['property'] == 'name' and not self.client.private_stream_exists(event['name'])): # Remove the previous stream name from the database. self._remove_stream_from_table(event['name']) # Add the new stream name. self._handle_stream(event['value'], False) return Response.none()