From 0d8b15a1094dffff791fcd85fd1071bdaffeb512 Mon Sep 17 00:00:00 2001 From: Lonami Exo Date: Fri, 26 Oct 2018 10:25:29 +0200 Subject: [PATCH] Add #haste command to assistant and fix typo --- readthedocs/extra/basic/telegram-client.rst | 2 +- telethon_examples/assistant.py | 49 ++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/readthedocs/extra/basic/telegram-client.rst b/readthedocs/extra/basic/telegram-client.rst index fd059fd7..9d2f5011 100644 --- a/readthedocs/extra/basic/telegram-client.rst +++ b/readthedocs/extra/basic/telegram-client.rst @@ -101,4 +101,4 @@ or even the TelegramClient_ itself to learn how it works. See the mentioned :ref:`telethon-client` to find the available methods. .. _InteractiveTelegramClient: https://github.com/LonamiWebs/Telethon/blob/master/telethon_examples/interactive_telegram_client.py -.. _TelegramClient: https://github.com/LonamiWebs/Telethon/blob/master/telethon/telegram_client.py +.. _TelegramClient: https://github.com/LonamiWebs/Telethon/tree/master/telethon/client diff --git a/telethon_examples/assistant.py b/telethon_examples/assistant.py index e8e8b2db..2f6c981d 100644 --- a/telethon_examples/assistant.py +++ b/telethon_examples/assistant.py @@ -7,12 +7,17 @@ import sys import time import urllib.parse -from telethon import TelegramClient, events, custom +from telethon import TelegramClient, events, types, custom, utils from telethon.extensions import markdown logging.basicConfig(level=logging.WARNING) logging.getLogger('asyncio').setLevel(logging.ERROR) +try: + import aiohttp +except ImportError: + aiohttp = None + logging.warning('aiohttp module not available; #haste command disabled') def get_env(name, message, cast=str): if name in os.environ: @@ -298,6 +303,48 @@ async def handler(event): await message.delete() +if aiohttp: + @bot.on(events.NewMessage(pattern='(?i)#[hp]aste(bin)?', forwards=False)) + async def handler(event): + """ + #haste: Replaces the message you reply to with a hastebin link. + """ + await event.delete() + if not event.reply_to_msg_id: + return + + msg = await event.get_reply_message() + sent = await event.respond( + 'Uploading paste...', reply_to=msg.reply_to_msg_id) + + name = utils.get_display_name(await msg.get_sender()) or 'A user' + text = msg.raw_text + code = '' + for _, string in msg.get_entities_text(( + types.MessageEntityCode, types.MessageEntityPre)): + code += f'{string}\n' + text = text.replace(string, '') + + code = code.rstrip() + if code: + text = re.sub(r'\s+', ' ', text) + else: + code = msg.raw_text + text = '' + + async with aiohttp.ClientSession() as session: + async with session.post('https://hastebin.com/documents', + data=code.encode('utf-8')) as resp: + haste = (await resp.json())['key'] + + await asyncio.wait([ + msg.delete(), + sent.edit(f'[{name}](tg://user?id={msg.sender_id}) ' + f'said: {text} hastebin.com/{haste}.py' + .replace(' ', ' ')) + ]) + + # ============================== Commands ============================== # ============================== Inline ==============================