Include message from ping command in pong

This commit is contained in:
Tulir Asokan 2019-07-08 10:49:33 +03:00
parent 9a6e06cc29
commit 6268749a2e

10
echo.py
View file

@ -1,5 +1,6 @@
from typing import Optional from typing import Optional
from time import time from time import time
from html import escape
from mautrix.types import TextMessageEventContent, MessageType, Format from mautrix.types import TextMessageEventContent, MessageType, Format
@ -34,14 +35,17 @@ class EchoBot(Plugin):
f"{cls.plural(minutes, 'minute')} and {cls.plural(seconds, 'second')}") f"{cls.plural(minutes, 'minute')} and {cls.plural(seconds, 'second')}")
@command.new("ping", help="Ping") @command.new("ping", help="Ping")
async def ping_handler(self, evt: MessageEvent) -> None: @command.argument("message", pass_raw=True, required=False)
async def ping_handler(self, evt: MessageEvent, message: str = "") -> None:
diff = int(time() * 1000) - evt.timestamp diff = int(time() * 1000) - evt.timestamp
pretty_diff = self.prettify_diff(diff) pretty_diff = self.prettify_diff(diff)
message = f'"{message[:20]}" took' if message else "took"
html_message = f'"{escape(message[:20])}" took' if message else "took"
content = TextMessageEventContent( content = TextMessageEventContent(
msgtype=MessageType.NOTICE, format=Format.HTML, msgtype=MessageType.NOTICE, format=Format.HTML,
body=f"{evt.sender}: Pong! (ping took {pretty_diff} to arrive)", body=f"{evt.sender}: Pong! (ping {message} {pretty_diff} to arrive)",
formatted_body=f"<a href='https://matrix.to/#/{evt.sender}'>{evt.sender}</a>: Pong! " formatted_body=f"<a href='https://matrix.to/#/{evt.sender}'>{evt.sender}</a>: Pong! "
f"(<a href='https://matrix.to/#/{evt.room_id}/{evt.event_id}'>ping</a> took " f"(<a href='https://matrix.to/#/{evt.room_id}/{evt.event_id}'>ping</a> {message} "
f"{pretty_diff} to arrive)") f"{pretty_diff} to arrive)")
content["pong"] = { content["pong"] = {
"ms": diff, "ms": diff,