async def handle_message(self, room_id: RoomID, user_id: UserID, message: MessageEventContent, event_id: EventID) -> None: sender = await self.bridge.get_user(user_id) if not sender or not await self.allow_message(sender): self.log.debug(f"Ignoring message {event_id} from {user_id} to {room_id}:" " User is not whitelisted.") return self.log.debug(f"Received Matrix event {event_id} from {sender.mxid} in {room_id}") self.log.trace("Event %s content: %s", event_id, message) if isinstance(message, TextMessageEventContent): message.trim_reply_fallback() is_command, text = self.is_command(message) portal = await self.bridge.get_portal(room_id) if not is_command and portal and await self.allow_bridging_message(sender, portal): await portal.handle_matrix_message(sender, message, event_id) return if message.msgtype != MessageType.TEXT or not await self.allow_command(sender): return is_management = await self.is_management(room_id) if is_command or is_management: try: command, arguments = text.split(" ", 1) args = arguments.split(" ") except ValueError: # Not enough values to unpack, i.e. no arguments command = text args = [] await self.commands.handle(room_id, event_id, sender, command, args, message, is_management, is_portal=portal is not None)
def matrix_reply_to_telegram( content: MessageEventContent, tg_space: TelegramID, room_id: Optional[RoomID] = None) -> Optional[TelegramID]: event_id = content.get_reply_to() if not event_id: return content.trim_reply_fallback() message = DBMessage.get_by_mxid(event_id, room_id, tg_space) if message: return message.tgid return None
async def matrix_reply_to_telegram( content: MessageEventContent, tg_space: TelegramID, room_id: RoomID | None = None) -> TelegramID | None: event_id = content.get_reply_to() if not event_id: return content.trim_reply_fallback() message = await DBMessage.get_by_mxid(event_id, room_id, tg_space) if message: return message.tgid return None
async def handle_message( self, room_id: RoomID, user_id: UserID, message: MessageEventContent, event_id: EventID ) -> None: async def bail(error_text: str, step=MessageSendCheckpointStep.REMOTE) -> None: self.log.debug(error_text) await MessageSendCheckpoint( event_id=event_id, room_id=room_id, step=step, timestamp=int(time.time() * 1000), status=MessageSendCheckpointStatus.PERM_FAILURE, reported_by=MessageSendCheckpointReportedBy.BRIDGE, event_type=EventType.ROOM_MESSAGE, message_type=message.msgtype, info=error_text, ).send( self.bridge.config["homeserver.message_send_checkpoint_endpoint"], self.az.as_token, self.log, ) sender = await self.bridge.get_user(user_id) if not sender or not await self.allow_message(sender): await bail( f"Ignoring message {event_id} from {user_id} to {room_id}:" " user is not whitelisted." ) return self.log.debug(f"Received Matrix event {event_id} from {sender.mxid} in {room_id}") self.log.trace("Event %s content: %s", event_id, message) if isinstance(message, TextMessageEventContent): message.trim_reply_fallback() is_command, text = self.is_command(message) portal = await self.bridge.get_portal(room_id) if not is_command and portal: if await self.allow_bridging_message(sender, portal): await portal.handle_matrix_message(sender, message, event_id) else: await bail( f"Ignoring event {event_id} from {sender.mxid}:" " not allowed to send to portal" ) return if message.msgtype != MessageType.TEXT: await bail(f"Ignoring event {event_id}: not a portal room and not a m.text message") return elif not await self.allow_command(sender): await bail( f"Ignoring command {event_id} from {sender.mxid}: not allowed to perform command", step=MessageSendCheckpointStep.COMMAND, ) return has_two_members, bridge_bot_in_room = await self._is_direct_chat(room_id) is_management = has_two_members and bridge_bot_in_room if is_command or is_management: try: command, arguments = text.split(" ", 1) args = arguments.split(" ") except ValueError: # Not enough values to unpack, i.e. no arguments command = text args = [] try: await self.commands.handle( room_id, event_id, sender, command, args, message, portal, is_management, bridge_bot_in_room, ) except Exception as e: await bail(repr(e), step=MessageSendCheckpointStep.COMMAND) else: await MessageSendCheckpoint( event_id=event_id, room_id=room_id, step=MessageSendCheckpointStep.COMMAND, timestamp=int(time.time() * 1000), status=MessageSendCheckpointStatus.SUCCESS, reported_by=MessageSendCheckpointReportedBy.BRIDGE, event_type=EventType.ROOM_MESSAGE, message_type=message.msgtype, ).send( self.bridge.config["homeserver.message_send_checkpoint_endpoint"], self.az.as_token, self.log, ) else: await bail( f"Ignoring event {event_id} from {sender.mxid}:" " not a command and not a portal room" )