mirror of
https://github.com/maubot/maubot.git
synced 2024-10-01 01:06:10 -04:00
Simplify Event.reply and add MaubotMatrixClient.send_markdown
This commit is contained in:
parent
8cfe2e8884
commit
6a6e8a818e
@ -13,14 +13,14 @@
|
|||||||
#
|
#
|
||||||
# You should have received a copy of the GNU Affero General Public License
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
from typing import Dict, List, Union, Callable, Awaitable
|
from typing import Dict, List, Union, Callable, Awaitable, Optional
|
||||||
import attr
|
import attr
|
||||||
import commonmark
|
import commonmark
|
||||||
|
|
||||||
from mautrix import Client as MatrixClient
|
from mautrix import Client as MatrixClient
|
||||||
from mautrix.client import EventHandler
|
from mautrix.client import EventHandler
|
||||||
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
|
||||||
MessageType, TextMessageEventContent, Format)
|
MessageType, TextMessageEventContent, Format, RelatesTo)
|
||||||
|
|
||||||
from .command_spec import ParsedCommand, CommandSpec
|
from .command_spec import ParsedCommand, CommandSpec
|
||||||
|
|
||||||
@ -35,24 +35,20 @@ class MaubotMessageEvent(MessageEvent):
|
|||||||
|
|
||||||
def respond(self, content: Union[str, MessageEventContent],
|
def respond(self, content: Union[str, MessageEventContent],
|
||||||
event_type: EventType = EventType.ROOM_MESSAGE,
|
event_type: EventType = EventType.ROOM_MESSAGE,
|
||||||
markdown: bool = True) -> Awaitable[EventID]:
|
markdown: bool = True, reply: bool = False) -> Awaitable[EventID]:
|
||||||
if isinstance(content, str):
|
if isinstance(content, str):
|
||||||
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
|
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
|
||||||
if markdown:
|
if markdown:
|
||||||
content.format = Format.HTML
|
content.format = Format.HTML
|
||||||
content.formatted_body = commonmark.commonmark(content.body)
|
content.formatted_body = commonmark.commonmark(content.body)
|
||||||
|
if reply:
|
||||||
|
content.set_reply(self)
|
||||||
return self._client.send_message_event(self.room_id, event_type, content)
|
return self._client.send_message_event(self.room_id, event_type, content)
|
||||||
|
|
||||||
def reply(self, content: Union[str, MessageEventContent],
|
def reply(self, content: Union[str, MessageEventContent],
|
||||||
event_type: EventType = EventType.ROOM_MESSAGE,
|
event_type: EventType = EventType.ROOM_MESSAGE,
|
||||||
markdown: bool = True) -> Awaitable[EventID]:
|
markdown: bool = True) -> Awaitable[EventID]:
|
||||||
if isinstance(content, str):
|
return self.respond(content, event_type, markdown, reply=True)
|
||||||
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
|
|
||||||
if markdown:
|
|
||||||
content.format = Format.HTML
|
|
||||||
content.formatted_body = commonmark.commonmark(content.body)
|
|
||||||
content.set_reply(self)
|
|
||||||
return self._client.send_message_event(self.room_id, event_type, content)
|
|
||||||
|
|
||||||
def mark_read(self) -> Awaitable[None]:
|
def mark_read(self) -> Awaitable[None]:
|
||||||
return self._client.send_receipt(self.room_id, self.event_id, "m.read")
|
return self._client.send_receipt(self.room_id, self.event_id, "m.read")
|
||||||
@ -67,6 +63,14 @@ class MaubotMatrixClient(MatrixClient):
|
|||||||
|
|
||||||
self.add_event_handler(self._command_event_handler, EventType.ROOM_MESSAGE)
|
self.add_event_handler(self._command_event_handler, EventType.ROOM_MESSAGE)
|
||||||
|
|
||||||
|
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, body=markdown, format=Format.HTML,
|
||||||
|
formatted_body=commonmark.commonmark(markdown))
|
||||||
|
if relates_to:
|
||||||
|
content.relates_to = relates_to
|
||||||
|
return self.send_message(room_id, content, **kwargs)
|
||||||
|
|
||||||
def set_command_spec(self, plugin_id: str, spec: CommandSpec) -> None:
|
def set_command_spec(self, plugin_id: str, spec: CommandSpec) -> None:
|
||||||
self.command_specs[plugin_id] = spec
|
self.command_specs[plugin_id] = spec
|
||||||
self._reparse_command_specs()
|
self._reparse_command_specs()
|
||||||
|
Loading…
Reference in New Issue
Block a user