-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSlashBot.py
More file actions
458 lines (381 loc) · 15.4 KB
/
SlashBot.py
File metadata and controls
458 lines (381 loc) · 15.4 KB
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
from __future__ import annotations
import asyncio
import os
import sys
import re
import html
import httpx
import telegram
from loguru import logger as _logger
from typing import Optional, Union, Any, Callable, Final, Sequence, Iterable
from telegram.ext import Application, MessageHandler, filters
from functools import partial, wraps
from itertools import product as _product
from itertools import starmap
from random import Random, SystemRandom
from collections import deque, Counter
from contextvars import ContextVar
from http.cookiejar import CookieJar, DefaultCookiePolicy
parser = re.compile(
r'^(?P<slash>[\\/]_?\$?)'
r'(?P<predicate>([^\s\\]|\\.)*((?<=\S)\\)?)'
r'(\s+(?P<complement>.+))?$'
)
ouenParser = re.compile(
r'^('
r'\\+ .* /+'
r'|'
r'\+ .* /+'
r'|'
r'(\\.*/\s*){2,}'
r'|'
r'(\.*/\s*){2,}'
r'|'
r'\\{2,}/{2,}'
r'|'
r'\{2,}/{2,}'
r')$'
)
pinParser = re.compile(
r'^[\\/]_?pin$'
)
randomStickerParser = re.compile(
r'^([\\/]_?){2,}$'
)
convertEscapes = partial(re.compile(r'\\(\s)').sub, r'\1')
htmlEscape = lambda s: s.replace("&", "&").replace("<", "<").replace(">", ">")
mentionParser = re.compile(r'@([a-zA-Z]\w{4,})')
product = lambda a, b: tuple(map(','.join, _product(a, b)))
PUNCTUATION_TAIL = (
'.,?!;:~('
'。,?!;:~('
)
try:
random = SystemRandom()
except NotImplementedError:
random = Random()
_logger.warning('SystemRandom is not available, using Random instead')
# env
TOKENS = re.compile(r'[^a-zA-Z\-_\d:]+').split(os.environ.get('TOKEN', ''))
if not TOKENS:
raise ValueError('no any valid token found')
PROXY = os.environ.get('PROXY')
# Set proxy and disallow cookies
HTTPX_CLIENT = httpx.AsyncClient(http2=True, proxy=PROXY, cookies=CookieJar(DefaultCookiePolicy(allowed_domains=())))
_logger.remove()
_logger.add(
sys.stderr,
format="<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green>"
"|<level>{level:^8}</level>"
"|<cyan>{extra[username]:^15}</cyan>"
"|<level>{message}</level>",
level="DEBUG",
)
logger_var: ContextVar[_logger] = ContextVar("logger_var", default=_logger)
class RandomizerMeta(type):
def __init__(cls, name, bases, namespace):
super().__init__(name, bases, namespace)
cls._counter = Counter()
class Randomizer(metaclass=RandomizerMeta):
def __class_getitem__(cls, item: str) -> Any:
collection = getattr(cls, item)
if cls._counter[item] % len(collection) == 0:
random.shuffle(collection)
cls._counter[item] += 1
collection.rotate()
return collection[-1]
class Vegetable(Randomizer):
permission_denied = deque(
product(
{'我太菜了', '我好菜', '我好菜啊', '我菜死了'},
{'pin 不了这条消息', '学不会怎么 pin 这条消息', '连 pin 都不被允许'}
)
+
product(
{'我学不会怎么 pin 这条消息', '这么简单的消息我都 pin 不了', '好想 pin 这条消息啊,但我做不到'},
{'需要浇浇', '怎么会有我这么菜的 bot', '我只能混吃等死', '我怎么会菜成这样'}
)
+
product(
{'这可要我怎么 pin 呀', '怎么才能 pin 这条消息呀', 'pin 不动呀,这可怎么办'},
{'拿大头针钉上吗', '找把锤子敲到柱子上吗', '触及知识盲区了都'}
)
)
reject = deque(
product(
{'我累了', '我好懒,又懒又菜', '我的 bot 生只要像这样躺着混日子就已经很幸福了'},
{'根本不想 pin 这条消息', '才不要 pin 这条消息', '还是另请高明吧', '一点 pin 的动力都没有'}
)
)
class Stickers(Randomizer):
_cnstr = partial(telegram.Sticker,
width=512,
height=512,
is_animated=False,
is_video=False,
type=telegram.Sticker.REGULAR)
stickers = deque(
starmap(
_cnstr,
(
('CAACAgUAAxkBAAInuWOcpUaH6eiL7vc9bIw6GedK-DNzAALNAQAC3x9yGXsiKdzwYgnWLAQ', 'AgADzQEAAt8fchk'),
('CAACAgUAAxkBAAInu2OcpakDQaHnVWVo_80AAVgE7EcujAAC2gEAAt8fchkAAbkxR5lrlfUsBA', 'AgAD2gEAAt8fchk'),
('CAACAgUAAxkBAAInvWOcpcUcJDWODiIxUoSTs840QJaFAALbAQAC3x9yGcc6smv9nZmELAQ', 'AgAD2wEAAt8fchk'),
('CAACAgUAAxkBAAKNaWOcn1dZP5Ooe5wX8JrCKkK2qXGzAALnAQAC3x9yGfJozMIlJl_kLAQ', 'AgAD5wEAAt8fchk'),
('CAACAgUAAxkBAAInt2Ocor-N-gnaJTGR-RtyopIgI0l5AALrAQAC3x9yGWlN_RGM1AESLAQ', 'AgAD6wEAAt8fchk'),
('CAACAgUAAxkBAAInv2Ocph3PT5XiCArmoehOYzCn1sJRAALyAQAC3x9yGSLk1aHaA09kLAQ', 'AgAD8gEAAt8fchk'),
('CAACAgUAAxkBAAInwWOcplEELbRuIBkuy3Yyv6YCScxdAAISAgAC3x9yGaW3ftfZrg8eLAQ', 'AgADEgIAAt8fchk'),
('CAACAgUAAxkBAAInw2Ocpm8VgaPVTUhDPTGHWjnDjzsDAAIbAgAC3x9yGWCDLF4OLhaMLAQ', 'AgADGwIAAt8fchk'),
)
)
)
def __new__(cls) -> telegram.Sticker:
return cls.__class_getitem__('stickers')
def log(func: Callable = None, verbose: bool = True):
if func is None:
return partial(log, verbose=verbose)
@wraps(func)
async def wrapper(update: telegram.Update, ctx: telegram.ext.CallbackContext):
logger = logger_var.get()
logger.debug(str(update.to_dict()))
return await func(update, ctx, logger)
return wrapper
class User:
def __init__(self, uid: Optional[int] = None, username: Optional[str] = None, name: Optional[str] = None):
if not (uid and name) and not username:
raise ValueError('invalid user')
self.name = name
self.uid = uid
self.username = username
async def __get_user_by_username(self):
r = await HTTPX_CLIENT.get(f'https://t.me/{self.username}')
og_t = re.search(r'(?<=<meta property="og:title" content=").*(?=")', r.text, re.IGNORECASE).group(0)
name = html.unescape(og_t) if og_t else None
page_title = re.search(r'(?<=<title>).*(?=</title>)', r.text, re.IGNORECASE).group(0)
if page_title == og_t: # user does not exist
self.name = None
elif name:
self.name = name
async def mention(self, mention_self: bool = False, pure: bool = False) -> str:
if not self.name and self.username:
await self.__get_user_by_username()
if not self.name:
return f'@{self.username}'
mention_deep_link = (f'tg://resolve?domain={self.username}'
if (self.username and (not self.uid or self.uid < 0))
else f'tg://user?id={self.uid}')
name = self.name if not mention_self else "自己"
name = htmlEscape(name)
return f'<a href="{mention_deep_link}">{name}</a>' if not pure else name
def __eq__(self, other):
return (
type(self) == type(other)
and (
(self.uid and other.uid and self.uid == other.uid)
or (self.username and other.username and self.username == other.username)
)
)
def get_user(msg: telegram.Message) -> User:
user = msg.sender_chat or msg.from_user
return User(name=user.full_name or user.title, uid=user.id, username=user.username)
def get_reply(msg: telegram.Message) -> Optional[telegram.Message]:
"""
Telegram creates a false reply to the forum_topic_created service message
when a topic message is not replying to anything.
Filter it out.
"""
rpl = msg.reply_to_message
if rpl and not rpl.forum_topic_created:
return rpl
return None
def get_users(msg: telegram.Message) -> tuple[User, User]:
msg_from = msg
msg_rpl = get_reply(msg) or msg_from
from_user, rpl_user = get_user(msg_from), get_user(msg_rpl)
return from_user, rpl_user
def parse_command(ctx: telegram.ext.CallbackContext) -> Optional[dict[str, Union[str, bool]]]:
match = ctx.match
parsed = match.groupdict()
predicate = parsed['predicate']
complement = parsed['complement']
if not predicate and complement:
return None # invalid command
omit_le = predicate.endswith('\\')
predicate = predicate[:-1] if omit_le else predicate
predicate = convertEscapes(predicate)
predicate = ctx.bot_data['delUsername'](predicate)
result = {'predicate': htmlEscape(predicate),
'complement': htmlEscape(complement or ''),
'slash': parsed['slash'],
'swap': parsed['slash'] not in ('/', '/$'),
'omit_le': omit_le}
return result
def get_tail(tail_char: str) -> str:
if tail_char in PUNCTUATION_TAIL:
return ''
halfwidth_mark = tail_char.isascii()
return '!' if halfwidth_mark else '!'
async def get_text(user_from: User, user_rpl: User, command: dict):
is_self_rpl = user_from == user_rpl
mention_from, mention_rpl = await asyncio.gather(user_from.mention(), user_rpl.mention(mention_self=is_self_rpl))
slash, predicate, complement, omit_le = \
command['slash'], command['predicate'], command['complement'], command['omit_le']
if predicate == '':
ret = '!' if not command['swap'] else '¡'
elif predicate == 'me':
ret = f"{mention_from}{bool(complement) * ' '}{complement}"
ret += get_tail((complement or user_from.mention(pure=True))[-1])
elif predicate == 'you':
ret = f"{mention_rpl}{bool(complement) * ' '}{complement}"
ret += get_tail((complement or user_rpl.mention(mention_self=is_self_rpl, pure=True))[-1])
elif complement:
ret = f"{mention_from} {predicate} {mention_rpl} {complement}"
ret += get_tail(complement[-1])
else:
ret = f"{mention_from} {predicate} "
ret += '了 ' if not omit_le else ''
ret += mention_rpl
ret += get_tail(mention_rpl[-1])
return ret
@log(verbose=False)
async def reply(update: telegram.Update, ctx: telegram.ext.CallbackContext, logger: _logger = _logger):
command = parse_command(ctx)
if not command:
return
logger.debug(str(update.to_dict()))
msg = update.effective_message
from_user, rpl_user = get_users(msg)
if from_user == rpl_user:
mention_match = mentionParser.search(command['predicate'])
if mention_match:
mention = mentionParser.search(msg.text).group(1)
rpl_user = User(username=mention)
command['predicate'] = command['predicate'][:mention_match.start()]
else:
mention_match = mentionParser.search(command['complement'])
if mention_match:
mention = mentionParser.search(msg.text).group(1)
rpl_user = User(username=mention)
complement = command['complement']
complement = complement[:mention_match.start()] + complement[mention_match.end():]
command['complement'] = complement.strip()
if command['swap'] and (not from_user == rpl_user):
(from_user, rpl_user) = (rpl_user, from_user)
text = await get_text(from_user, rpl_user, command)
logger.info(text)
await msg.reply_text('\u200e' + text, parse_mode='HTML')
@log
async def repeat(update: telegram.Update, _ctx: telegram.ext.CallbackContext, logger: _logger = _logger):
chat = update.effective_chat
msg = update.effective_message
tid = msg.message_thread_id
logger.info(msg.text)
if msg.has_protected_content:
await msg.copy(chat.id, message_thread_id=tid)
else:
await msg.forward(chat.id, message_thread_id=tid)
@log
async def pin(update: telegram.Update, _ctx: telegram.ext.CallbackContext, logger: _logger = _logger):
msg = update.effective_message
msg_to_pin = get_reply(msg)
if not msg_to_pin:
vegetable = f'{Vegetable["reject"]} (Reply to a message to use the command)'
await msg.reply_text(vegetable)
logger.warning(vegetable)
return
try:
await msg_to_pin.unpin()
await msg_to_pin.pin(disable_notification=True)
logger.info(f'Pinned {msg_to_pin.text}')
except telegram.error.BadRequest as e:
vegetable = f'{Vegetable["permission_denied"]} ({e})'
await msg_to_pin.reply_text(vegetable)
logger.warning(vegetable)
@log
async def random_sticker(update: telegram.Update, _ctx: telegram.ext.CallbackContext, logger: _logger = _logger):
msg = update.effective_message
sticker = Stickers()
logger.info(sticker)
await msg.reply_sticker(sticker)
class App:
_apps: Final[set["App"]] = set()
# MessageHandler is stateless and reusable, so we can reuse the same instance for all handlers.
# Note: this is not always true for other handlers, e.g., ConversationHandler.
_handlers: Final[Sequence[MessageHandler]] = (
MessageHandler(
filters.Regex(ouenParser) & ~filters.UpdateType.EDITED,
repeat,
block=False,
),
MessageHandler(
filters.Regex(randomStickerParser) & ~filters.UpdateType.EDITED,
random_sticker,
block=False,
),
MessageHandler(
filters.Regex(pinParser) & ~filters.UpdateType.EDITED,
pin,
block=False,
),
MessageHandler(
filters.Regex(parser) & ~filters.UpdateType.EDITED,
reply,
block=False,
),
)
def __init__(self, token: str):
self.token = token
ab = Application.builder().token(token)
if PROXY:
ab = ab.proxy(PROXY).get_updates_proxy(PROXY)
self.application = ab.build()
self.application.add_handlers(self._handlers)
async def start(self):
app = self.application
await app.initialize()
username = f'@{app.bot.username}'
logger = _logger.bind(username=username)
logger_var.set(logger)
app.bot_data['delUsername'] = partial(re.compile(username, re.I).sub, '')
if app.post_init:
await app.post_init(app)
await app.updater.start_polling()
await app.start()
logger.info('Started')
self._apps.add(self)
async def shutdown(self):
app = self.application
logger = logger_var.get()
await app.updater.stop()
await app.stop()
if app.post_stop:
await app.post_stop(app)
await app.shutdown()
if app.post_shutdown:
await app.post_shutdown(app)
logger.info('Stopped')
self._apps.discard(self)
@classmethod
async def start_all(cls, tokens: Iterable[str]):
await asyncio.gather(*(cls(token).start() for token in tokens))
@classmethod
async def shutdown_all(cls):
if cls._apps:
await asyncio.gather(*(app.shutdown() for app in cls._apps))
assert not cls._apps, 'Not all apps were stopped'
@classmethod
async def run(cls, tokens: Iterable[str]):
try:
# Initialize and reuse the HTTPX client for all instances, and shut it down on exit.
async with HTTPX_CLIENT:
await cls.start_all(tokens)
# The Event is never set to finish, so it is equivalent to asyncio.get_running_loop().run_forever().
await asyncio.Event().wait()
except (KeyboardInterrupt, SystemExit):
pass
finally:
await cls.shutdown_all()
def main():
asyncio.run(App.run(TOKENS))
if __name__ == '__main__':
main()