-
Notifications
You must be signed in to change notification settings - Fork 29
/
haste.py
85 lines (71 loc) · 2.84 KB
/
haste.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
import asyncio
import re
from telethon import events, types
_MAX_MSG_DISTANCE = 10
async def init(bot, modules):
try:
import aiohttp
except ImportError:
aiohttp = None
logging.warning('aiohttp module not available; #haste command disabled')
return
utils = modules['utils']
@bot.on(events.NewMessage(pattern='(?i)#([hp]aste|dog|inu)(bin)?', forwards=False))
async def handler(event):
"""
#haste: Replaces the message you reply to with a dogbin link.
"""
await event.delete()
if not event.reply_to_msg_id:
return
msg = await event.get_reply_message()
if msg.photo or msg.document:
await event.respond('Hey, that\'s not how you send faulty code or an error returned by the library.' \
' Please send a minimal reproducible code.' \
'\nWhen sending a code snippet or error use either nekobin.com or any other preferred pastebin website.')
return
if len(msg.raw_text or '') < 200:
sent = await event.respond('Not bothering to paste such a short message.')
await asyncio.sleep(10)
await sent.delete()
return
if event.id - msg.id > _MAX_MSG_DISTANCE:
sent = await event.respond('The message is too old for a paste to matter now.')
await asyncio.sleep(10)
await sent.delete()
return
sent = await event.respond(
'Uploading paste…', reply_to=msg.reply_to_msg_id)
name = utils.get_display(await msg.get_sender())
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://pasty.lus.pm/api/v2/pastes",
json={"content": code},
headers={
"User-Agent": "TelethonianBot/Version",
"Content-Type": "application/json",
}
) as r:
if r.status >= 300:
await sent.edit("pasty seems to be down… ( ^^')")
return
paste = f"pasty.lus.pm/{(await r.json())['id']}"
await asyncio.gather(
msg.delete(),
sent.edit(f'<a href="tg://user?id={msg.sender_id}">{name}</a> '
f'said: {text} {paste}'
.replace(' ', ' '), parse_mode='html')
)