-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.py
287 lines (239 loc) · 10.9 KB
/
main.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# Main commands that used to live in the Telethon repository
import asyncio
import urllib.parse
from telethon import events, custom
READ_FULL = (
'Please read [Accessing the Full API](https://docs.telethon.dev'
'/en/stable/concepts/full-api.html)'
)
SEARCH = (
'Remember [search is your friend]'
'(https://tl.telethon.dev/?q={}&redirect=no)'
)
DOCS = 'TL Reference for [{}](https://tl.telethon.dev/?q={})'
RTD = '[Read The Docs!](https://docs.telethon.dev)'
RTFD = '[Read The F* Docs!](https://docs.telethon.dev)'
UPDATES = (
'Check out [Working with Updates](https://docs.telethon.dev'
'/en/stable/basic/updates.html) in the documentation.'
)
SPAM = (
"Telethon is free software. That means using it is a right: you are "
"free to use it for absolutely any purpose whatsoever. However, help "
"and support with using it is a privilege. If you misbehave or want "
"to do bad things, despite possibly having good intentions, nobody "
"is obligated to help you."
)
OFFTOPIC = {
-1001109500936:
'That is not related to Telethon. '
'Please move to a different group with the correct topic to continue that conversation',
-1001200633650:
'That seems to be related to Telethon. Try asking in @TelethonChat'
}
TOPIC = (
"This group is for **usage questions about Telethon only**, and "
"occasionally about semi-related topics like MTProto. Anything that does "
"not directly involve any of the two does not belong here, and questions "
"regarding general Python knowledge or use of any other libraries do not "
"belong here. Please use other groups or search online instead."
)
UNKNOWN_OFFTOPIC = (
"I don't know of any off-topic group for this chat! Maybe you want to "
"visit the on-topic @TelethonChat?"
)
ASK = (
"Hey, that's not how you ask a question! If you want helpful advice "
"(or any response at all) [read this first](https://stackoverflow.com"
"/help/how-to-ask) and then ask again. If you have the time, [How To "
"Ask Questions The Smart Way](catb.org/~esr/faqs/smart-questions.html)"
" is another wonderful resource worth reading."
)
LOGGING = '''
**Please enable logging:**
```import logging
logging.basicConfig(level=logging.WARNING)```
If you need more information, use `logging.DEBUG` instead.
'''
ALREADY_FIXED = (
"This issue has already been fixed, but it's not yet available in PyPi. "
"You can upgrade now with `pip3 install -U https://github.com/LonamiWebs"
"/Telethon/archive/v1.zip`."
)
GOOD_RESOURCES = (
"Some good resources to learn Python:\n"
"• [Official Docs](https://docs.python.org/3/tutorial/index.html).\n"
"• [Dive Into Python 3](https://rawcdn.githack.com/diveintomark/"
"diveintopython3/master/table-of-contents.html).\n"
"• [Learn Python](https://www.learnpython.org/).\n"
"• [Project Python](http://projectpython.net/).\n"
"• [Computer Science Circles](https://cscircles.cemc.uwaterloo.ca/).\n"
"• [MIT OpenCourse](https://ocw.mit.edu/courses/electrical-engineering-"
"and-computer-science/6-0001-introduction-to-computer-science-and-progr"
"amming-in-python-fall-2016/).\n"
"• [Hitchhiker’s Guide to Python](https://docs.python-guide.org/).\n"
"• The @PythonRes Telegram Channel.\n"
"• Corey Schafer videos for [beginners](https://www.youtube.com/watch?v="
"YYXdXT2l-Gg&list=PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7) and in [general]"
"(https://www.youtube.com/watch?v=YYXdXT2l-Gg&list=PL-osiE80TeTt2d9bfV"
"yTiXJA-UTHn6WwU)."
)
LEARN_PYTHON = (
"That issue is no longer related with Telethon, and this group is not "
"meant to be a group for learning Python. " + GOOD_RESOURCES
)
BUG_REPORT = (
"If you believe you have found a bug in the library and you are pretty "
"sure that the issue is not in your code, please [report it in GitHub]"
"(https://github.com/LonamiWebs/Telethon/issues/new/choose). This group "
"is only intended to help people with Telethon, it is __not__ the right "
"place to report bugs.\n\n"
"Before reporting in GitHub, **please make sure to provide**:\n"
"• A small, self-contained snippet of code that reproduces the issue. "
"Pasting several hundred lines of messy code is unlikely to get any help "
"soon.\n"
"• Description of the environment. Does it happen with users, bots, in "
"private chats, groups, megagroups, broadcast channels, with proxy, with "
"certain messages, media, datacenters… You get the idea. Include "
"everything that may be relevant, and even provide the media / message / "
"link if possible.\n"
"• [Search for your issue first](https://github.com/LonamiWebs/Telethon/"
"issues). It might already have been fixed.\n"
"• Make sure you __truly__ are using the latest version. Run your code "
"with `import telethon; print(telethon.__version__)` to verify this."
)
async def init(bot):
@bot.on(events.NewMessage(pattern='#full', forwards=False))
async def handler(event):
"""#full: Advises to read "Accessing the full API" in the docs."""
await asyncio.gather(
event.delete(),
event.respond(READ_FULL, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='#search (.+)', forwards=False))
async def handler(event):
"""#search query: Searches for "query" in the method reference."""
query = urllib.parse.quote(event.pattern_match.group(1))
await asyncio.gather(
event.delete(),
event.respond(SEARCH.format(query), reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#ref (.+)', forwards=False))
async def handler(event):
"""#ref query: Like #search but shows the query."""
q1 = event.pattern_match.group(1)
q2 = urllib.parse.quote(q1)
await asyncio.gather(
event.delete(),
event.respond(DOCS.format(q1, q2), reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='#(?:rt(f)?d|docs)', forwards=False))
async def handler(event):
"""#docs or #rtd: Tells the user to please read the docs."""
rtd = RTFD if event.pattern_match.group(1) else RTD
await asyncio.gather(
event.delete(),
event.respond(rtd, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='#(updates|events)', forwards=False))
async def handler(event):
"""#updates: Advices the user to read "Working with Updates"."""
await asyncio.gather(
event.delete(),
event.respond(UPDATES, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#(ask|question)', forwards=False))
async def handler(event):
"""#ask or #question: Advices the user to ask a better question."""
await asyncio.gather(
event.delete(),
event.respond(
ASK, reply_to=event.reply_to_msg_id, link_preview=False)
)
@bot.on(events.NewMessage(pattern='(?i)#spam(mer|ming)?', forwards=False))
async def handler(event):
"""#spam, #spammer, #spamming: Informs spammers that they are not welcome here."""
await asyncio.gather(
event.delete(),
event.respond(SPAM, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#(ot|offtopic)', forwards=False))
async def handler(event):
"""#ot, #offtopic: Tells the user to move to an offtopic channel."""
await asyncio.gather(
event.delete(),
event.respond(OFFTOPIC.get(event.chat_id, UNKNOWN_OFFTOPIC), reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#topic', forwards=False))
async def handler(event):
"""#topic: Explains the topic of the group to the user."""
await asyncio.gather(
event.delete(),
event.respond(TOPIC, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#log(s|ging)?', forwards=False))
async def handler(event):
"""#log, #logs or #logging: Explains how to enable logging."""
await asyncio.gather(
event.delete(),
event.respond(LOGGING, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#(master|v1)', forwards=False))
async def handler(event):
"""#master or #v1: The bug has been fixed in the `v1` branch."""
await asyncio.gather(
event.delete(),
event.respond(ALREADY_FIXED, reply_to=event.reply_to_msg_id)
)
@bot.on(events.NewMessage(pattern='(?i)#(learn|python)', forwards=False))
async def handler(event):
"""#learn or #python: Tells the user to learn some Python first."""
await asyncio.gather(
event.delete(),
event.respond(
LEARN_PYTHON, reply_to=event.reply_to_msg_id, link_preview=False)
)
@bot.on(events.NewMessage(pattern='(?i)#bugs?', forwards=False))
async def handler(event):
"""#bug or #bugs: Advices the user to report bugs in GitHub."""
await asyncio.gather(
event.delete(),
event.respond(
BUG_REPORT, reply_to=event.reply_to_msg_id, link_preview=False)
)
@bot.on(events.NewMessage(pattern='(?i)#(list|help)', forwards=False))
async def handler(event):
await event.delete()
text = 'Available commands:\n'
for callback, handler in bot.list_event_handlers():
if isinstance(handler, events.NewMessage) and callback.__doc__:
text += f'\n{callback.__doc__.strip()}'
text += '\n\nYou can suggest new commands [here](https://docs.google.com/'\
'spreadsheets/d/12yWwixUu_vB426_toLBAiajXxYKvR2J1DD6yZtQz9l4/edit).'
message = await event.respond(text, link_preview=False)
await asyncio.sleep(1 * text.count(' ')) # Sleep ~1 second per word
await message.delete()
@bot.on(events.InlineQuery)
async def handler(event):
builder = event.builder
result = None
query = event.text.lower()
if query == 'ping':
result = builder.article('Pong!', text='This bot works inline')
elif query == 'group':
result = builder.article(
'Move to the right group!',
text='Try moving to the [right group](t.me/TelethonChat)',
buttons=custom.Button.url('Join the group!', 't.me/TelethonChat'),
link_preview=False
)
elif query in ('python', 'learn'):
result = builder.article(
'Resources to Learn Python',
text=GOOD_RESOURCES,
link_preview=False
)
# NOTE: You should always answer, but we want plugins to be able to answer
# too (and we can only answer once), so we don't always answer here.
if result:
await event.answer([result])