2018-11-24 21:22:44 +00:00
|
|
|
"""
|
|
|
|
Reply to a file with .f to send it as a photo
|
|
|
|
"""
|
2018-11-24 21:14:04 +00:00
|
|
|
from io import BytesIO
|
|
|
|
|
2018-11-26 17:58:35 +00:00
|
|
|
from uniborg import util
|
|
|
|
|
2018-11-24 21:14:04 +00:00
|
|
|
from telethon import types
|
|
|
|
from telethon.errors import PhotoInvalidDimensionsError
|
2018-11-26 17:58:35 +00:00
|
|
|
from telethon.tl.functions.messages import SendMediaRequest
|
2018-11-24 21:14:04 +00:00
|
|
|
|
|
|
|
|
2018-11-26 17:58:35 +00:00
|
|
|
@borg.on(util.admin_cmd(r"^\.f$"))
|
|
|
|
async def on_file_to_photo(event):
|
|
|
|
await event.delete()
|
|
|
|
target = await event.get_reply_message()
|
2018-11-24 21:14:04 +00:00
|
|
|
try:
|
|
|
|
image = target.media.document
|
|
|
|
except AttributeError:
|
|
|
|
return
|
2018-11-25 00:45:10 +00:00
|
|
|
if not image.mime_type.startswith('image/'):
|
|
|
|
return # This isn't an image
|
|
|
|
if image.mime_type == 'image/webp':
|
|
|
|
return # Telegram doesn't let you directly send stickers as photos
|
|
|
|
if image.size > 10 * 1024 * 1024:
|
|
|
|
return # We'd get PhotoSaveFileInvalidError otherwise
|
2018-11-24 21:14:04 +00:00
|
|
|
|
|
|
|
file = await borg.download_media(target, file=BytesIO())
|
|
|
|
file.seek(0)
|
|
|
|
img = await borg.upload_file(file)
|
2018-11-26 17:58:35 +00:00
|
|
|
img.name = 'image.png'
|
2018-11-24 21:14:04 +00:00
|
|
|
|
|
|
|
try:
|
2018-11-26 17:58:35 +00:00
|
|
|
await borg(SendMediaRequest(
|
|
|
|
peer=await event.get_input_chat(),
|
|
|
|
media=types.InputMediaUploadedPhoto(img),
|
|
|
|
message=target.message,
|
|
|
|
entities=target.entities,
|
|
|
|
reply_to_msg_id=target.id
|
|
|
|
))
|
2018-11-24 21:14:04 +00:00
|
|
|
except PhotoInvalidDimensionsError:
|
|
|
|
return
|