Compare commits

..

10 Commits

6 changed files with 44 additions and 20 deletions

View File

@ -3,13 +3,17 @@ Reply to a file with .f to send it as a photo
"""
from io import BytesIO
from stdplugins.kbass_core import self_reply_cmd
from uniborg import util
from telethon import types
from telethon.errors import PhotoInvalidDimensionsError
from telethon.tl.functions.messages import SendMediaRequest
@self_reply_cmd(borg, r"^\.f$")
async def on_file_to_photo(event, target):
@borg.on(util.admin_cmd(r"^\.f$"))
async def on_file_to_photo(event):
await event.delete()
target = await event.get_reply_message()
try:
image = target.media.document
except AttributeError:
@ -24,11 +28,15 @@ async def on_file_to_photo(event, target):
file = await borg.download_media(target, file=BytesIO())
file.seek(0)
img = await borg.upload_file(file)
img.name = 'image.png'
try:
await event.respond(
reply_to=target,
file=types.InputMediaUploadedPhoto(img)
)
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
))
except PhotoInvalidDimensionsError:
return

View File

@ -4,12 +4,25 @@ Contains code used by other kbass_* plugins
from uniborg import util
async def get_target_message(borg, event):
"""
If the event is a reply, returns the reply message if it's from us
If event is not a reply, then it tries to return the most recent message
from us
"""
target = await event.get_reply_message()
if event.is_reply and target.from_id == borg.uid:
return target
if not target:
return await util.get_recent_self_message(borg, event)
def self_reply_cmd(borg, pattern):
def wrapper(function):
@borg.on(util.admin_cmd(pattern))
async def wrapped(event, *args, **kwargs):
await event.delete()
target = await util.get_target_message(borg, event)
target = await get_target_message(borg, event)
if not target:
return
return await function(event, target, *args, **kwargs)

View File

@ -14,7 +14,6 @@ from stdplugins.kbass_core import self_reply_cmd
@self_reply_cmd(borg, r"^\.e$")
async def on_edit_start(event, target):
await asyncio.sleep(3) # tdesktop doesn't sync drafts when the window is active
await borg(SaveDraftRequest(
peer=await event.get_input_chat(),
message=(target.message or '.') + '\n.e',
@ -24,18 +23,18 @@ async def on_edit_start(event, target):
))
@self_reply_cmd(borg, r'(?ms)^(.+)\.e$')
@self_reply_cmd(borg, r'(?ms)^(.+\n|\.)\.e$')
async def on_edit_end(event, target):
text = event.pattern_match.group(1)
message = event.message.message[:-2]
if message.strip() == '.':
message = ''
if text == '.':
text = ''
chat = await event.get_input_chat()
try:
await borg(EditMessageRequest(
peer=await event.get_input_chat(),
peer=chat,
id=target.id,
no_webpage=not target.media,
message=message,
message=text,
entities=event.message.entities
))
except MessageEmptyError:

View File

@ -21,7 +21,7 @@ async def on_yank(event, targets, num_offset):
message = '\n\n'.join(get_message_html(target) for target in targets)
message, entities = thtml.parse(message)
await borg(SaveDraftRequest(
peer='me',
peer=await event.get_input_chat(),
message=message,
entities=entities,
no_webpage=True,

View File

@ -11,6 +11,12 @@ import telethon.utils
from uniborg import util
async def get_target_message(event):
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
return await event.get_reply_message()
return await util.get_recent_self_message(borg, event)
async def await_read(chat, message):
if isinstance(chat, InputPeerSelf):
return
@ -37,7 +43,7 @@ async def delete(event):
text = event.pattern_match.group(2)
if not text:
return
target = await util.get_target_message(borg, event)
target = await get_target_message(event)
if target:
chat = await event.get_input_chat()
await await_read(chat, target)

View File

@ -30,9 +30,7 @@ async def is_read(borg, entity, message, is_out=None):
return message_id <= max_id
async def get_target_message(borg, event):
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
return await event.get_reply_message()
async def get_recent_self_message(borg, event):
async for message in borg.iter_messages(
await event.get_input_chat(), limit=20):
if message.out: