Added file downloading, updated to v0.5.1

This commit is contained in:
2026-09-13 03:17:12 +03:00
parent f3f1e24c0b
commit 0e9b621895
5 changed files with 315 additions and 7 deletions

View File

@@ -9,9 +9,10 @@ the bot.
import asyncio
from io import BytesIO
import os
import time
import logging
import random
from PIL import Image
from PIL import Image, ImageFilter
from mab import *
@@ -45,11 +46,41 @@ async def on_gen_command(data: EventContext) -> None:
# send
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)
took_time = time.time() - s
if not isinstance(data, bytes):
await ctx.bot.send_text(ctx.room, "😧")
return
# convert to Image
with BytesIO(data) as buf:
img = Image.open(buf)
# apply effects
blur = ImageFilter.GaussianBlur(
radius=min(img.size[0] // 10, 5)
)
img = img.filter(blur)
# save to buffer
buf = BytesIO()
img.save(buf, format="PNG")
buf.seek(0)
buf = buf.read()
# send
await ctx.bot.send_image_bytes(
ctx.room,
buf,
"blurred.png",
text="Download and decryption took %.4f seconds" % took_time
)
async def on_wrong_message(data: EventContext) -> None:
"""This callback is called when a wrong message is received."""
await data.bot.send_text(
data.room,
"Text me something like <code>!gen 0.1 0.7 1.0</code>"
"Text me something like <code>!gen 0.1 0.7 1.0</code> or send an image to blur"
)
async def main() -> None:
@@ -76,12 +107,18 @@ async def main() -> None:
bot.add_callback(
~SenderIsBotFilter() & command_filter,
on_gen_command)
# callback for new image message
bot.add_callback(
~SenderIsBotFilter() & MessageTypeFilter(MessageType.IMAGE) & NewMessageFilter(),
on_image
)
# callback for message that
# 1. are sent not by this bot
# 2. do NOT match the command filter
# 2. are new messages (not edits)
bot.add_callback(
~SenderIsBotFilter() & ~command_filter,
~SenderIsBotFilter() & BodyExistsFilter() & NewMessageFilter(),
on_wrong_message
)