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

View File

@ -4,12 +4,25 @@ Contains code used by other kbass_* plugins
from uniborg import util 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 self_reply_cmd(borg, pattern):
def wrapper(function): def wrapper(function):
@borg.on(util.admin_cmd(pattern)) @borg.on(util.admin_cmd(pattern))
async def wrapped(event, *args, **kwargs): async def wrapped(event, *args, **kwargs):
await event.delete() await event.delete()
target = await util.get_target_message(borg, event) target = await get_target_message(borg, event)
if not target: if not target:
return return
return await function(event, target, *args, **kwargs) 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$") @self_reply_cmd(borg, r"^\.e$")
async def on_edit_start(event, target): async def on_edit_start(event, target):
await asyncio.sleep(3) # tdesktop doesn't sync drafts when the window is active
await borg(SaveDraftRequest( await borg(SaveDraftRequest(
peer=await event.get_input_chat(), peer=await event.get_input_chat(),
message=(target.message or '.') + '\n.e', 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): async def on_edit_end(event, target):
text = event.pattern_match.group(1) text = event.pattern_match.group(1)
message = event.message.message[:-2] if text == '.':
if message.strip() == '.': text = ''
message = '' chat = await event.get_input_chat()
try: try:
await borg(EditMessageRequest( await borg(EditMessageRequest(
peer=await event.get_input_chat(), peer=chat,
id=target.id, id=target.id,
no_webpage=not target.media, no_webpage=not target.media,
message=message, message=text,
entities=event.message.entities entities=event.message.entities
)) ))
except MessageEmptyError: 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 = '\n\n'.join(get_message_html(target) for target in targets)
message, entities = thtml.parse(message) message, entities = thtml.parse(message)
await borg(SaveDraftRequest( await borg(SaveDraftRequest(
peer='me', peer=await event.get_input_chat(),
message=message, message=message,
entities=entities, entities=entities,
no_webpage=True, no_webpage=True,

View File

@ -11,6 +11,12 @@ import telethon.utils
from uniborg import util 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): async def await_read(chat, message):
if isinstance(chat, InputPeerSelf): if isinstance(chat, InputPeerSelf):
return return
@ -37,7 +43,7 @@ async def delete(event):
text = event.pattern_match.group(2) text = event.pattern_match.group(2)
if not text: if not text:
return return
target = await util.get_target_message(borg, event) target = await get_target_message(event)
if target: if target:
chat = await event.get_input_chat() chat = await event.get_input_chat()
await await_read(chat, target) 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 return message_id <= max_id
async def get_target_message(borg, event): async def get_recent_self_message(borg, event):
if event.is_reply and (await event.get_reply_message()).from_id == borg.uid:
return await event.get_reply_message()
async for message in borg.iter_messages( async for message in borg.iter_messages(
await event.get_input_chat(), limit=20): await event.get_input_chat(), limit=20):
if message.out: if message.out: