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")
return
try:
await self.client.versions()
whoami = await self.client.whoami()
except MatrixInvalidToken as e:
self.log.error(f"Invalid token: {e}. Disabling client")

View File

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

View File

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