20 Commits

Author SHA1 Message Date
695a91cad4 Removed echo command and bumped version number. 2025-04-21 15:25:34 -04:00
Tulir Asokan
55ea03df06 Move CI script to main maubot repo 2022-06-19 14:26:00 +03:00
Tulir Asokan
0d2db04ed5 Update CI artifact expiry 2021-11-28 15:35:30 +02:00
Tulir Asokan
546e5eee21 Bump version to 1.4.0 2020-03-01 14:22:14 +02:00
Tulir Asokan
0e5b7bc2db Use RelatesTo and RelationType classes 2020-03-01 14:06:07 +02:00
Tulir Asokan
25aa06e1ff Update copyright year 2020-03-01 13:57:20 +02:00
Tulir Asokan
4ade107370 Change rel_type to xyz.maubot.pong 2019-12-09 21:20:07 +02:00
Jason Volk
e9b4cd47a2 Add m.relates_to support in pong response content. 2019-12-09 21:18:44 +02:00
Tulir Asokan
4d3f4f1556 Add .gitlab-ci.yml 2019-07-28 22:09:46 +03:00
Tulir Asokan
0d8d59eb1a Fix incorrect html pong message and bump version to 1.3.1 2019-07-08 18:23:12 +03:00
Tulir Asokan
e5454924dd Bump version to 1.3.0 2019-07-08 10:55:45 +03:00
Tulir Asokan
d325076c9f Don't allow HTML in ping messages 2019-07-08 10:55:07 +03:00
Tulir Asokan
6268749a2e Include message from ping command in pong 2019-07-08 10:49:33 +03:00
Tulir Asokan
9a6e06cc29 Bump version to 1.2.0 2019-04-14 02:16:20 +03:00
Tulir Asokan
37df8c89d6 Use mention and event link instead of reply for pongs 2019-04-14 02:12:04 +03:00
Tulir Asokan
390267ea95 Bump version to 1.1.1 2019-03-08 01:03:57 +02:00
Tulir Asokan
e42a19df0f Add pong metadata 2019-03-07 21:39:40 +02:00
Tulir Asokan
7786f03b03 Bump version to 1.1.0 2019-03-06 15:43:24 +02:00
Tulir Asokan
c1b15a82a1 Fix pluralization in ping 2019-03-06 15:43:22 +02:00
Tulir Asokan
25b9340266 Round to seconds when time is over a minute in !ping 2019-03-06 14:53:50 +02:00
5 changed files with 49 additions and 16 deletions

3
.gitlab-ci.yml Normal file
View File

@@ -0,0 +1,3 @@
include:
- project: 'maubot/maubot'
file: '/.gitlab-ci-plugin.yml'

View File

@@ -1,6 +1,6 @@
The MIT License (MIT) The MIT License (MIT)
Copyright (c) 2019 Tulir Asokan Copyright (c) 2020 Tulir Asokan
Permission is hereby granted, free of charge, to any person obtaining a copy of Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in this software and associated documentation files (the "Software"), to deal in

View File

@@ -3,4 +3,3 @@ A simple [maubot](https://github.com/maubot/maubot) that echoes pings and other
## Usage ## Usage
* `!ping` - Reply with "Pong!" and the time it took for the message to reach the bot. * `!ping` - Reply with "Pong!" and the time it took for the message to reach the bot.
* `!echo <message>` - Reply with the given message

57
echo.py
View File

@@ -1,4 +1,8 @@
from typing import Optional
from time import time from time import time
from html import escape
from mautrix.types import TextMessageEventContent, MessageType, Format, RelatesTo, RelationType
from maubot import Plugin, MessageEvent from maubot import Plugin, MessageEvent
from maubot.handlers import command from maubot.handlers import command
@@ -6,26 +10,53 @@ from maubot.handlers import command
class EchoBot(Plugin): class EchoBot(Plugin):
@staticmethod @staticmethod
def time_since(ms: int) -> str: def plural(num: float, unit: str, decimals: Optional[int] = None) -> str:
diff = int(time() * 1000) - ms num = round(num, decimals)
if num == 1:
return f"{num} {unit}"
else:
return f"{num} {unit}s"
@classmethod
def prettify_diff(cls, diff: int) -> str:
if abs(diff) < 10 * 1_000: if abs(diff) < 10 * 1_000:
return f"{diff} ms" return f"{diff} ms"
elif abs(diff) < 60 * 1_000: elif abs(diff) < 60 * 1_000:
return f"{round(diff / 1_000, 1)} seconds" return cls.plural(diff / 1_000, 'second', decimals=1)
minutes, seconds = divmod(diff / 1_000, 60) minutes, seconds = divmod(diff / 1_000, 60)
if abs(minutes) < 60: if abs(minutes) < 60:
return f"{minutes} minutes and {seconds} seconds" return f"{cls.plural(minutes, 'minute')} and {cls.plural(seconds, 'second')}"
hours, minutes = divmod(minutes, 60) hours, minutes = divmod(minutes, 60)
if abs(hours) < 24: if abs(hours) < 24:
return f"{hours} hours, {minutes} minutes and {seconds} seconds" return (f"{cls.plural(hours, 'hour')}, {cls.plural(minutes, 'minute')}"
f" and {cls.plural(seconds, 'second')}")
days, hours = divmod(hours, 24) days, hours = divmod(hours, 24)
return f"{days} days, {hours} hours, {minutes} minutes and {seconds} seconds" return (f"{cls.plural(days, 'day')}, {cls.plural(hours, 'hour')}, "
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)
await evt.reply(f"Pong! (ping took {self.time_since(evt.timestamp)} to arrive)") async def ping_handler(self, evt: MessageEvent, message: str = "") -> None:
diff = int(time() * 1000) - evt.timestamp
@command.new("echo", help="Repeat a message") pretty_diff = self.prettify_diff(diff)
@command.argument("message", pass_raw=True) text_message = f'"{message[:20]}" took' if message else "took"
async def echo_handler(self, evt: MessageEvent, message: str) -> None: html_message = f'"{escape(message[:20])}" took' if message else "took"
await evt.respond(message) content = TextMessageEventContent(
msgtype=MessageType.NOTICE, format=Format.HTML,
body=f"{evt.sender}: Pong! (ping {text_message} {pretty_diff} to arrive)",
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> {html_message} "
f"{pretty_diff} to arrive)",
relates_to=RelatesTo(
rel_type=RelationType("xyz.maubot.pong"),
event_id=evt.event_id,
))
pong_from = evt.sender.split(":", 1)[1]
content.relates_to["from"] = pong_from
content.relates_to["ms"] = diff
content["pong"] = {
"ms": diff,
"from": pong_from,
"ping": evt.event_id,
}
await evt.respond(content)

View File

@@ -1,6 +1,6 @@
maubot: 0.1.0 maubot: 0.1.0
id: xyz.maubot.echo id: xyz.maubot.echo
version: 1.0.0 version: 1.4.1
license: MIT license: MIT
modules: modules:
- echo - echo