Compare commits
20 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
695a91cad4
|
|||
|
|
55ea03df06 | ||
|
|
0d2db04ed5 | ||
|
|
546e5eee21 | ||
|
|
0e5b7bc2db | ||
|
|
25aa06e1ff | ||
|
|
4ade107370 | ||
|
|
e9b4cd47a2 | ||
|
|
4d3f4f1556 | ||
|
|
0d8d59eb1a | ||
|
|
e5454924dd | ||
|
|
d325076c9f | ||
|
|
6268749a2e | ||
|
|
9a6e06cc29 | ||
|
|
37df8c89d6 | ||
|
|
390267ea95 | ||
|
|
e42a19df0f | ||
|
|
7786f03b03 | ||
|
|
c1b15a82a1 | ||
|
|
25b9340266 |
3
.gitlab-ci.yml
Normal file
3
.gitlab-ci.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
include:
|
||||
- project: 'maubot/maubot'
|
||||
file: '/.gitlab-ci-plugin.yml'
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
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
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
|
||||
@@ -3,4 +3,3 @@ A simple [maubot](https://github.com/maubot/maubot) that echoes pings and other
|
||||
|
||||
## Usage
|
||||
* `!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
57
echo.py
@@ -1,4 +1,8 @@
|
||||
from typing import Optional
|
||||
from time import time
|
||||
from html import escape
|
||||
|
||||
from mautrix.types import TextMessageEventContent, MessageType, Format, RelatesTo, RelationType
|
||||
|
||||
from maubot import Plugin, MessageEvent
|
||||
from maubot.handlers import command
|
||||
@@ -6,26 +10,53 @@ from maubot.handlers import command
|
||||
|
||||
class EchoBot(Plugin):
|
||||
@staticmethod
|
||||
def time_since(ms: int) -> str:
|
||||
diff = int(time() * 1000) - ms
|
||||
def plural(num: float, unit: str, decimals: Optional[int] = None) -> str:
|
||||
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:
|
||||
return f"{diff} ms"
|
||||
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)
|
||||
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)
|
||||
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)
|
||||
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")
|
||||
async def ping_handler(self, evt: MessageEvent) -> None:
|
||||
await evt.reply(f"Pong! (ping took {self.time_since(evt.timestamp)} to arrive)")
|
||||
|
||||
@command.new("echo", help="Repeat a message")
|
||||
@command.argument("message", pass_raw=True)
|
||||
async def echo_handler(self, evt: MessageEvent, message: str) -> None:
|
||||
await evt.respond(message)
|
||||
@command.argument("message", pass_raw=True, required=False)
|
||||
async def ping_handler(self, evt: MessageEvent, message: str = "") -> None:
|
||||
diff = int(time() * 1000) - evt.timestamp
|
||||
pretty_diff = self.prettify_diff(diff)
|
||||
text_message = f'"{message[:20]}" took' if message else "took"
|
||||
html_message = f'"{escape(message[:20])}" took' if message else "took"
|
||||
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)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
maubot: 0.1.0
|
||||
id: xyz.maubot.echo
|
||||
version: 1.0.0
|
||||
version: 1.4.1
|
||||
license: MIT
|
||||
modules:
|
||||
- echo
|
||||
|
||||
Reference in New Issue
Block a user