-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmashhadlug_bot.py
69 lines (58 loc) · 1.98 KB
/
mashhadlug_bot.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
#!/usr/bin/env python
import telepot
from pprint import pprint
from telepot.loop import MessageLoop
from datetime import datetime
from time import time, sleep
BOT_TOKEN_KEY = 'TOKEN'
BURST_THRESHOLD_SECS = 5
USER_MAXIMUM_BURST_POSTS = 3
READONLY_TIME_HOUR_START = 2
READONLY_TIME_HOUR_END = 6
bot = telepot.Bot(BOT_TOKEN_KEY)
hot_words = ('foo', 'bar')
users = {}
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
msg_id = telepot.message_identifier(msg)
pprint(msg)
if chat_type not in ['group', 'supergroup']:
return
# Group is read-only between 2 to 7 o'clock in the morning
msg_date = datetime.fromtimestamp(msg['date'])
if msg_date.hour >= READONLY_TIME_HOUR_START and msg_date.hour <= READONLY_TIME_HOUR_END:
bot.deleteMessage(telepot.message_identifier(msg))
return
# Any GIF sent to the group should be removed
if content_type == 'document' and msg['document']['mime_type'] == 'video/mp4':
bot.deleteMessage(msg_id)
return
# Also no sticker
if content_type == 'sticker':
bot.deleteMessage(msg_id)
return
# Any post contaning hot words should also be removed
if content_type == 'text':
for word in hot_words:
if word.lower() in msg['text'].lower():
bot.deleteMessage(msg_id)
return
# Delete all messages sent from a user in a short period of time
user_id = msg['from']['id']
now = time()
if users.get(user_id):
users[user_id]['counts'] += 1
if (now - users[user_id]['datetime']) < BURST_THRESHOLD_SECS:
users[user_id]['datetime'] = now
users[user_id]['ids'].append(msg_id)
if users[user_id]['counts'] > USER_MAXIMUM_BURST_POSTS:
for msg_id in users[user_id]['ids']:
bot.deleteMessage(msg_id)
users[user_id]['ids'] = []
else:
users[user_id] = {'counts': 1, 'datetime': now, 'ids': [msg_id]}
else:
users[user_id] = {'counts': 1, 'datetime': now, 'ids': [msg_id]}
MessageLoop(bot, handle).run_as_thread()
while True:
sleep(10)