Simplify Event.reply and add MaubotMatrixClient.send_markdown

This commit is contained in:
Tulir Asokan 2018-11-28 02:33:59 +02:00
parent 8cfe2e8884
commit 6a6e8a818e

View File

@ -13,14 +13,14 @@
#
# 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/>.
from typing import Dict, List, Union, Callable, Awaitable
from typing import Dict, List, Union, Callable, Awaitable, Optional
import attr
import commonmark
from mautrix import Client as MatrixClient
from mautrix.client import EventHandler
from mautrix.types import (EventType, MessageEvent, Event, EventID, RoomID, MessageEventContent,
MessageType, TextMessageEventContent, Format)
MessageType, TextMessageEventContent, Format, RelatesTo)
from .command_spec import ParsedCommand, CommandSpec
@ -35,24 +35,20 @@ class MaubotMessageEvent(MessageEvent):
def respond(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE,
markdown: bool = True) -> Awaitable[EventID]:
markdown: bool = True, reply: bool = False) -> Awaitable[EventID]:
if isinstance(content, str):
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
if markdown:
content.format = Format.HTML
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)
def reply(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE,
markdown: bool = True) -> Awaitable[EventID]:
if isinstance(content, str):
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)
return self.respond(content, event_type, markdown, reply=True)
def mark_read(self) -> Awaitable[None]:
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)
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:
self.command_specs[plugin_id] = spec
self._reparse_command_specs()