Update mautrix-python

This commit is contained in:
Tulir Asokan 2022-03-04 15:38:34 +02:00
parent 6f57c88e4a
commit 7679a0e97c
4 changed files with 24 additions and 21 deletions

View File

@ -168,6 +168,7 @@ class Client:
self.log.warning("Ignoring start() call to started client") self.log.warning("Ignoring start() call to started client")
return return
try: try:
await self.client.versions()
whoami = await self.client.whoami() whoami = await self.client.whoami()
except MatrixInvalidToken as e: except MatrixInvalidToken as e:
self.log.error(f"Invalid token: {e}. Disabling client") self.log.error(f"Invalid token: {e}. Disabling client")

View File

@ -39,15 +39,15 @@ class MaubotHTMLParser(MatrixParser[HumanReadableString]):
fs = HumanReadableString fs = HumanReadableString
def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True async def parse_formatted(message: str, allow_html: bool = False, render_markdown: bool = True
) -> Tuple[str, str]: ) -> Tuple[str, str]:
if render_markdown: if render_markdown:
html = markdown.render(message, allow_html=allow_html) html = markdown.render(message, allow_html=allow_html)
elif allow_html: elif allow_html:
html = message html = message
else: else:
return message, escape(message) return message, escape(message)
return MaubotHTMLParser.parse(html).text, html return (await MaubotHTMLParser().parse(html)).text, html
class MaubotMessageEvent(MessageEvent): class MaubotMessageEvent(MessageEvent):
@ -60,17 +60,17 @@ class MaubotMessageEvent(MessageEvent):
self.client = client self.client = client
self.disable_reply = client.disable_replies self.disable_reply = client.disable_replies
def respond(self, content: Union[str, MessageEventContent], async def respond(self, content: Union[str, MessageEventContent],
event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True, event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
allow_html: bool = False, reply: Union[bool, str] = False, allow_html: bool = False, reply: Union[bool, str] = False,
edits: Optional[Union[EventID, MessageEvent]] = None) -> Awaitable[EventID]: edits: Optional[Union[EventID, MessageEvent]] = None) -> EventID:
if isinstance(content, str): if isinstance(content, str):
content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content) content = TextMessageEventContent(msgtype=MessageType.NOTICE, body=content)
if allow_html or markdown: if allow_html or markdown:
content.format = Format.HTML content.format = Format.HTML
content.body, content.formatted_body = parse_formatted(content.body, content.body, content.formatted_body = await parse_formatted(
render_markdown=markdown, content.body, render_markdown=markdown, allow_html=allow_html
allow_html=allow_html) )
if edits: if edits:
content.set_edit(edits) content.set_edit(edits)
elif reply: elif reply:
@ -82,7 +82,7 @@ class MaubotMessageEvent(MessageEvent):
f'</a>: {fmt_body}') f'</a>: {fmt_body}')
else: else:
content.set_reply(self) content.set_reply(self)
return self.client.send_message_event(self.room_id, event_type, content) return await 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, markdown: bool = True, event_type: EventType = EventType.ROOM_MESSAGE, markdown: bool = True,
@ -110,20 +110,22 @@ class MaubotMatrixClient(MatrixClient):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.disable_replies = False self.disable_replies = False
def send_markdown(self, room_id: RoomID, markdown: str, *, allow_html: bool = False, async def send_markdown(self, room_id: RoomID, markdown: str, *, allow_html: bool = False,
msgtype: MessageType = MessageType.TEXT, msgtype: MessageType = MessageType.TEXT,
edits: Optional[Union[EventID, MessageEvent]] = None, edits: Optional[Union[EventID, MessageEvent]] = None,
relates_to: Optional[RelatesTo] = None, **kwargs relates_to: Optional[RelatesTo] = None, **kwargs
) -> Awaitable[EventID]: ) -> EventID:
content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML) content = TextMessageEventContent(msgtype=msgtype, format=Format.HTML)
content.body, content.formatted_body = parse_formatted(markdown, allow_html=allow_html) content.body, content.formatted_body = await parse_formatted(
markdown, allow_html=allow_html
)
if relates_to: if relates_to:
if edits: if edits:
raise ValueError("Can't use edits and relates_to at the same time.") raise ValueError("Can't use edits and relates_to at the same time.")
content.relates_to = relates_to content.relates_to = relates_to
elif edits: elif edits:
content.set_edit(edits) content.set_edit(edits)
return self.send_message(room_id, content, **kwargs) return await self.send_message(room_id, content, **kwargs)
def dispatch_event(self, event: Event, source: SyncStream) -> List[asyncio.Task]: def dispatch_event(self, event: Event, source: SyncStream) -> List[asyncio.Task]:
if isinstance(event, MessageEvent) and not isinstance(event, MaubotMessageEvent): if isinstance(event, MessageEvent) and not isinstance(event, MaubotMessageEvent):

View File

@ -1,4 +1,4 @@
mautrix>=0.12.2,<0.13 mautrix==0.15.0rc4
aiohttp>=3,<4 aiohttp>=3,<4
yarl>=1,<2 yarl>=1,<2
SQLAlchemy>=1,<1.4 SQLAlchemy>=1,<1.4

View File

@ -39,7 +39,7 @@ setuptools.setup(
install_requires=install_requires, install_requires=install_requires,
extras_require=extras_require, extras_require=extras_require,
python_requires="~=3.7", python_requires="~=3.8",
classifiers=[ classifiers=[
"Development Status :: 4 - Beta", "Development Status :: 4 - Beta",
@ -48,9 +48,9 @@ setuptools.setup(
"Framework :: AsyncIO", "Framework :: AsyncIO",
"Programming Language :: Python", "Programming Language :: Python",
"Programming Language :: Python :: 3", "Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8", "Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9", "Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
], ],
entry_points=""" entry_points="""
[console_scripts] [console_scripts]