Add #haste command to assistant and fix typo

This commit is contained in:
Lonami Exo 2018-10-26 10:25:29 +02:00
parent 091180b32d
commit 0d8b15a109
2 changed files with 49 additions and 2 deletions

View File

@ -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

View File

@ -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 ==============================