Got rid of proxy methods in MatrixBot, v0.6.0

- Updated README.md
- Updated examples
- `MatrixBot` does not redefine methods from classes it is composed of anymore
This commit is contained in:
2026-09-27 16:17:40 +03:00
parent 16b849ebd8
commit 2867582c01
10 changed files with 107 additions and 311 deletions

View File

@@ -29,18 +29,18 @@ async def on_help_command(ctx: EventContext) -> None:
"<li><code>!args arg1 [arg2] ... [arg5]</code> - command that takes 1..5 arguments</li>"
"</ul>"
)
await ctx.bot.send_text(ctx.room, HELP_MESSAGE)
await ctx.bot.send.text(ctx.room, HELP_MESSAGE)
async def on_time_command(ctx: EventContext) -> None:
"""!time"""
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
f"Current UNIX timestamp is <strong>{int(time.time())}</strong>"
)
async def on_raise_command(ctx: EventContext) -> None:
"""!raise"""
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
"<strong>Executing <code>raise RuntimeError()</code>...</strong>"
)
@@ -48,7 +48,7 @@ async def on_raise_command(ctx: EventContext) -> None:
async def on_assert_command(ctx: EventContext) -> None:
"""!assert"""
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
"<strong>Executing <code>assert False</code>...</strong>"
)
@@ -63,9 +63,9 @@ async def on_mul_command(ctx: EventContext) -> None:
v *= n
response = " * ".join(html.escape("%.2f" % n) for n in numbers)
response += f" = <strong>{html.escape(str(v))}<strong>"
await ctx.bot.send_text(ctx.room, response)
await ctx.bot.send.text(ctx.room, response)
except Exception as e:
await ctx.bot.send_text(ctx.room, f"Could not process the command: {e}")
await ctx.bot.send.text(ctx.room, f"Could not process the command: {e}")
async def on_args_command(ctx: EventContext) -> None:
"""!args"""
@@ -79,19 +79,19 @@ async def on_args_command(ctx: EventContext) -> None:
for arg in ctx[CTX_CMD_ARGS]:
response += f"<li><code>{html.escape(arg)}</code></li>"
response += "</ol>"
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
response
)
except:
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
f"Could not process the command: {traceback.format_exc()}"
)
async def invalid_usage(ctx: EventContext) -> None:
"""This callback is called when the bot used incorrectly."""
await ctx.bot.send_text(ctx.room, "Use <code>!help</code>")
await ctx.bot.send.text(ctx.room, "Use <code>!help</code>")
async def main() -> None:

View File

@@ -15,7 +15,7 @@ from _environment import check_environment
async def on_text_message(ctx: EventContext) -> None:
"""This callback is called when a text message arrives."""
await ctx.bot.send_text(ctx.room, ctx[CTX_BODY])
await ctx.bot.send.text(ctx.room, ctx[CTX_BODY])
async def main() -> None:
"""Application entry point"""

View File

@@ -18,18 +18,18 @@ from _environment import check_environment
async def on_text_message(ctx: EventContext) -> None:
"""This callback is called when a text message arrives."""
await ctx.bot.send_text(ctx.room, "Please send a file/image/video")
await ctx.bot.send.text(ctx.room, "Please send a file/image/video")
async def on_file_message(ctx: EventContext) -> None:
"""This callback is called when a file arrives."""
sha = hashlib.sha256()
does_temp_exist = False
if ctx[CTX_FILE_SIZE] > 1_000_000:
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
"The file is larger than 1 MB, downloading to filesystem"
)
await ctx.bot.download_file(ctx, path="temp.tmp")
await ctx.bot.download.file(ctx, path="temp.tmp")
async with aiofiles.open("temp.tmp", "rb") as f:
while True:
data = await f.read(64 * 1024)
@@ -38,16 +38,16 @@ async def on_file_message(ctx: EventContext) -> None:
sha.update(data)
does_temp_exist = True
else:
await ctx.bot.send_text(
await ctx.bot.send.text(
ctx.room,
"The file is smaller than 1 MB, downloading to RAM"
)
content = await ctx.bot.download_file(ctx, path=None)
content = await ctx.bot.download.file(ctx, path=None)
sha.update(content)
# result
response = f"SHA256 for file `{ctx[CTX_FILE_NAME]}`"
response += f" ({ctx[CTX_FILE_SIZE]} bytes, {ctx[CTX_FILE_MIME]})"
await ctx.bot.send_file_bytes(
await ctx.bot.send.file_bytes(
room=ctx.room,
data=sha.hexdigest().encode("utf-8"),
filename="hash of the file.txt",
@@ -56,7 +56,7 @@ async def on_file_message(ctx: EventContext) -> None:
)
# resend the file to test uploading
if does_temp_exist:
await ctx.bot.send_file(
await ctx.bot.send.file(
room=ctx.room,
path="temp.tmp",
filename=ctx[CTX_FILE_NAME],

View File

@@ -24,9 +24,9 @@ async def on_gen_command(data: EventContext) -> None:
try:
r, g, b = [float(v) for v in data[CTX_CMD_ARGS]]
except:
await data.bot.send_text(data.room, "Invalid arguments")
await data.bot.send.text(data.room, "Invalid arguments")
return
await data.bot.send_text(data.room, "Generating the noise...")
await data.bot.send.text(data.room, "Generating the noise...")
# create the basic noise
img = Image.new("RGB", (16, 16))
for x in range(img.width):
@@ -44,13 +44,13 @@ async def on_gen_command(data: EventContext) -> None:
buf.seek(0)
buf = buf.read()
# send
await data.bot.send_image_bytes(data.room, buf, "noise.png")
await data.bot.send.image_bytes(data.room, buf, "noise.png")
async def on_image(ctx: EventContext) -> None:
"""This callback is called when an image is received."""
# download
s = time.time()
data = await ctx.bot.download_file(ctx)
data = await ctx.bot.download.file(ctx)
took_time = time.time() - s
# convert to Image
with BytesIO(data) as buf:
@@ -66,7 +66,7 @@ async def on_image(ctx: EventContext) -> None:
buf.seek(0)
buf = buf.read()
# send
await ctx.bot.send_image_bytes(
await ctx.bot.send.image_bytes(
ctx.room,
buf,
"blurred.png",
@@ -75,7 +75,7 @@ async def on_image(ctx: EventContext) -> None:
async def on_wrong_message(data: EventContext) -> None:
"""This callback is called when a wrong message is received."""
await data.bot.send_text(
await data.bot.send.text(
data.room,
"Text me something like <code>!gen 0.1 0.7 1.0</code> or send an image to blur"
)