Beispiel #1
0
    def send_text(self,
                  room_id: RoomID,
                  text: str,
                  html: Optional[str] = None,
                  msgtype: MessageType = MessageType.TEXT,
                  relates_to: Optional[RelatesTo] = None,
                  **kwargs) -> Awaitable[EventID]:
        """
        Send a text message to a room.

        Args:
            room_id: The ID of the room to send the message to.
            text: The text to send. If set to None, the given HTML is used instead.
            html: The HTML to send.
            msgtype: The message type to send.
                Defaults to :attr:`MessageType.TEXT` (normal text message)
            relates_to: Message relation metadata used for things like replies.
            **kwargs: Optional parameters to pass to the :meth:`HTTPAPI.request` method.

        Returns:
            The ID of the event that was sent.
        """
        if html:
            if not text:
                text = html
            content = TextMessageEventContent(msgtype=msgtype,
                                              body=text,
                                              format=Format.HTML,
                                              formatted_body=html)
        else:
            content = TextMessageEventContent(msgtype=msgtype, body=text)
        if relates_to:
            content.relates_to = relates_to
        return self.send_message(room_id, content, **kwargs)
Beispiel #2
0
 def send_markdown(self,
                   room_id: RoomID,
                   markdown: str,
                   msgtype: MessageType = MessageType.TEXT,
                   relates_to: Optional[RelatesTo] = None,
                   **kwargs) -> Awaitable[EventID]:
     content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
     content.body, content.formatted_body = parse_markdown(markdown)
     if relates_to:
         content.relates_to = relates_to
     return self.send_message(room_id, content, **kwargs)
Beispiel #3
0
 def send_markdown(self,
                   room_id: RoomID,
                   markdown: str,
                   msgtype: MessageType = MessageType.TEXT,
                   edits: Optional[Union[EventID, MessageEvent]] = None,
                   relates_to: Optional[RelatesTo] = None,
                   **kwargs) -> Awaitable[EventID]:
     content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
     content.body, content.formatted_body = parse_markdown(markdown)
     if relates_to:
         if edits:
             raise ValueError(
                 "Can't use edits and relates_to at the same time.")
         content.relates_to = relates_to
     elif edits:
         content.set_edit(edits)
     return self.send_message(room_id, content, **kwargs)
async def _add_reply_header(source: 'AbstractUser', content: TextMessageEventContent, evt: Message,
                            main_intent: IntentAPI):
    space = (evt.to_id.channel_id
             if isinstance(evt, Message) and isinstance(evt.to_id, PeerChannel)
             else source.tgid)

    msg = DBMessage.get_one_by_tgid(TelegramID(evt.reply_to_msg_id), space)
    if not msg:
        return

    content.relates_to = RelatesTo(rel_type=RelationType.REFERENCE, event_id=msg.mxid)

    try:
        event: MessageEvent = await main_intent.get_event(msg.mx_room, msg.mxid)
        if isinstance(event.content, TextMessageEventContent):
            event.content.trim_reply_fallback()
        content.set_reply(event)
    except MatrixRequestError:
        pass
Beispiel #5
0
    async def send_text(
        self,
        room_id: RoomID,
        text: str | None = None,
        html: str | None = None,
        msgtype: MessageType = MessageType.TEXT,
        relates_to: RelatesTo | None = None,
        **kwargs,
    ) -> EventID:
        """
        Send a text message to a room.

        Args:
            room_id: The ID of the room to send the message to.
            text: The text to send. If set to ``None``, the given HTML will be parsed to generate
                  a plaintext representation.
            html: The HTML to send.
            msgtype: The message type to send.
                     Defaults to :attr:`MessageType.TEXT` (normal text message).
            relates_to: Message relation metadata used for things like replies.
            **kwargs: Optional parameters to pass to the :meth:`HTTPAPI.request` method.

        Returns:
            The ID of the event that was sent.

        Raises:
            ValueError: if both ``text`` and ``html`` are ``None``.
        """
        if html is not None:
            if text is None:
                text = await parse_html(html)
            content = TextMessageEventContent(msgtype=msgtype,
                                              body=text,
                                              format=Format.HTML,
                                              formatted_body=html)
        elif text is not None:
            content = TextMessageEventContent(msgtype=msgtype, body=text)
        else:
            raise TypeError(
                "send_text() requires either text or html to be set")
        if relates_to:
            content.relates_to = relates_to
        return await self.send_message(room_id, content, **kwargs)
async def _add_reply_header(source: 'AbstractUser', content: TextMessageEventContent, evt: Message,
                            main_intent: IntentAPI):
    space = (evt.peer_id.channel_id
             if isinstance(evt, Message) and isinstance(evt.peer_id, PeerChannel)
             else source.tgid)

    msg = DBMessage.get_one_by_tgid(TelegramID(evt.reply_to.reply_to_msg_id), space)
    if not msg:
        return

    content.relates_to = RelatesTo(rel_type=RelationType.REPLY, event_id=msg.mxid)

    try:
        event: MessageEvent = await main_intent.get_event(msg.mx_room, msg.mxid)
        if isinstance(event.content, TextMessageEventContent):
            event.content.trim_reply_fallback()
        puppet = await pu.Puppet.get_by_mxid(event.sender, create=False)
        content.set_reply(event, displayname=puppet.displayname if puppet else event.sender)
    except MatrixRequestError:
        log.exception("Failed to get event to add reply fallback")
Beispiel #7
0
 async def send_markdown(
     self,
     room_id: RoomID,
     markdown: str,
     *,
     allow_html: bool = False,
     msgtype: MessageType = MessageType.TEXT,
     edits: EventID | MessageEvent | None = None,
     relates_to: RelatesTo | None = None,
     **kwargs,
 ) -> EventID:
     content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
     content.body, content.formatted_body = await parse_formatted(
         markdown, allow_html=allow_html
     )
     if relates_to:
         if edits:
             raise ValueError("Can't use edits and relates_to at the same time.")
         content.relates_to = relates_to
     elif edits:
         content.set_edit(edits)
     return await self.send_message(room_id, content, **kwargs)