-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrepeat.py
61 lines (52 loc) · 2.08 KB
/
repeat.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
import unicodedata
import sys
from collections import defaultdict
from telegram import Update
from telegram.ext.callbackcontext import CallbackContext
punctuations = ''.join(list(chr(i) for i in range(sys.maxunicode)
if unicodedata.category(chr(i)).startswith('P')))
last_text = defaultdict(str)
last_sender = defaultdict(int)
cnt = defaultdict(int)
repeated = defaultdict(bool)
def strip_punctuation(s: str):
return s.strip(punctuations)
def repeat(update: Update, context: CallbackContext):
global last_text, repeated, cnt
if update.message is None or update.message.text is None or update.message.from_user is None:
return
if len(update.message.text) > 50:
# do not flood
return
if update.message.sender_chat is not None:
# ignore messages sent on behalf of a channel
return
t = update.message.text.strip()
e = update.message.entities
f = update.message.from_user.id
if '我' in t and '你' in t:
t = t.replace('你', '他').replace('我', '你')
elif '我' in t:
t = t.replace('我', '你')
chat_id = update.effective_chat.id
if 1 <= len(update.message.text) <= 30 and (update.message.text.endswith("!") or update.message.text.endswith("!")):
# repeat 3 times with "!"
repeated[chat_id] = True
context.bot.send_message(chat_id=chat_id,
text=(strip_punctuation(t) + "!") * 3)
elif f != last_sender[chat_id] and update.message.text == last_text[chat_id] \
and cnt[chat_id] >= 1 and not repeated[chat_id]:
# repeat as follower
repeated[chat_id] = True
context.bot.send_message(chat_id=chat_id,
text=t, entities=e)
last_sender[chat_id] = f
if update.message.text != last_text[chat_id]:
last_text[chat_id] = update.message.text
cnt[chat_id] = 1
repeated[chat_id] = False
else:
cnt[chat_id] += 1
def clean_repeat(update: Update, context: CallbackContext):
chat_id = update.effective_chat.id
last_text[chat_id] = ""