-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
397 lines (322 loc) Β· 15 KB
/
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
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
import telebot
import psycopg2
import re
from prometheus_client import start_http_server, Summary, Counter
from telebot.types import Chat, Message, User, CallbackQuery, InlineQuery
import logging
import config
opt_in = Summary('opt_in_latency_seconds', 'Opt in latency')
opt_out = Summary('opt_out_latency_seconds', 'Opt out latency')
ping = Summary('ping_latency_seconds', 'Ping latency')
lists = Summary('list_latency_seconds', 'List latency')
inline = Summary('inline_latency_seconds', 'Inline latency')
reply = Summary('reply_latency_seconds', 'Reply latency')
exceptions = Counter('exception_counts', 'Exceptions count')
logger = telebot.logger
logger.setLevel(logging.WARNING)
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(message)s')
bot = telebot.TeleBot(config.TELEGRAM_KEY)
@bot.message_handler(commands=['create'])
def create(message: Message):
chat_member = bot.get_chat_member(message.chat.id, message.from_user.id)
if not (chat_member.status in ['creator', 'administrator'] or message.chat.type == 'private'):
bot.reply_to(message, "You don't have permissions. Only admins can create and remove aliases")
return
args = message.text.split(' ')
if len(args) != 2 or not args[1].isprintable() or len(args[1].lstrip('@')) == 0:
bot.reply_to(message, "You need to use create with an alias name. See /help for more information")
return
try:
alias = args[1].lstrip('@')
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('insert into chat (id) values (%s) on conflict do nothing', (message.chat.id,))
c.execute('insert into ping (chat_id, alias) values (%s, %s)',
(message.chat.id, alias))
conn.commit()
conn.close()
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
bot.reply_to(message, rf"You created `@{alias}`\. Now you can `/opt_in {alias}`", parse_mode='MarkdownV2')
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
@bot.message_handler(commands=['remove'])
def remove(message: Message):
chat_member = bot.get_chat_member(message.chat.id, message.from_user.id)
if not (chat_member.status in ['creator', 'administrator'] or message.chat.type == 'private'):
bot.reply_to(message, "You don't have permissions. Only admins can create and remove aliases")
return
args = message.text.split(' ')
if len(args) not in [2, 3] or not args[1].isprintable() or len(args[1].lstrip('@')) == 0:
bot.reply_to(message, "You need to use remove with an alias name. See /help for more information")
return
try:
alias = args[1].lstrip('@')
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('select count(user_id) from ping_user '
'left join ping on ping_user.ping_id = ping.id '
'where chat_id = %s and alias = %s',
(message.chat.id, alias))
cnt = c.fetchone()[0]
if cnt == 0 or (len(args) == 3 and args[2] == '--force'):
c.execute('delete from ping_user where ping_id = '
'(select id from ping where chat_id = %s and alias = %s)',
(message.chat.id, alias))
c.execute('delete from ping where chat_id=%s and alias=%s',
(message.chat.id, alias))
conn.commit()
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
else:
bot.reply_to(message, rf'Alias `{alias}` is not empty\. To get all users inside run `/list`\.'
f'\nIf you are sure you want to delete it run `/remove {alias} --force`',
parse_mode='MarkdownV2')
conn.close()
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
@bot.message_handler(commands=['help'])
def help_(message: Message):
bot.reply_to(message, rf"""
/help \- this message
/create \- create new alias for pings
/remove \- remove alias
/opt\_in name \- add yourself (or user, on which you reply with the command) to the alias with name
/opt\_out name \- remove yourself (or user, on which you reply with the command) from the name
/get\_out \- remove yourself from all the pings in this chat
/list \- get list of aliases with people
Also, there is inline mode: use `@{bot.get_me().username} ping` and `@{bot.get_me().username} opt_out`""",
parse_mode='MarkdownV2')
@bot.message_handler(commands=['opt_in', 'opt-in'])
@opt_in.time()
def opt_in(message: Message):
args = message.text.split(' ')
if len(args) != 2:
bot.reply_to(message, "You need to use opt-in with an alias name. See /help for more information")
return
try:
if message.reply_to_message is not None and not message.reply_to_message.from_user.is_bot:
user_id = message.reply_to_message.from_user.id
else:
user_id = message.from_user.id
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('insert into "user" (id) values (%s) on conflict do nothing', (user_id,))
c.execute('insert into ping_user (ping_id, user_id) '
'(select id, %s from ping where chat_id = %s and alias = %s)',
(user_id, message.chat.id, args[1]))
conn.commit()
conn.close()
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
@bot.message_handler(commands=['opt_out', 'opt-out'])
@opt_out.time()
def opt_out(message: Message):
args = message.text.split(' ')
if len(args) != 2:
bot.reply_to(message, "You need to use opt-out with an alias name. See /help for more information")
return
try:
if message.reply_to_message is not None and not message.reply_to_message.from_user.is_bot:
user_id = message.reply_to_message.from_user.id
else:
user_id = message.from_user.id
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('delete from ping_user where user_id = %s and ping_id = '
'(select id from ping where chat_id = %s and alias = %s)',
(user_id, message.chat.id, args[1]))
conn.commit()
conn.close()
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
@bot.message_handler(commands=['list'])
@lists.time()
def list_(message: Message):
try:
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('select alias, user_id from ping '
'left join ping_user on ping.id = ping_user.ping_id '
'where chat_id = %s', (message.chat.id,))
if c.rowcount == 0:
bot.reply_to(message, 'There are no aliases yet')
return
result = dict()
for alias, user_id in c:
if user_id is None:
if alias not in result:
result[alias] = []
else:
if alias in result:
result[alias] += [user_id]
else:
result[alias] = [user_id]
conn.close()
ret = ''
for alias, users in result.items():
ret += f'β’ {alias}: ' # •
for user_id in users:
ret += f'{bot.get_chat_member(message.chat.id, user_id).user.username} '
ret += '\n'
bot.reply_to(message, ret)
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
@bot.message_handler(commands=['get-out', 'get_out'])
def get_out(message: Message):
try:
remove_from_all(message.from_user, message.chat)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
except Exception:
exceptions.inc()
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
def remove_from_all(user: User, chat: Chat):
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('delete from ping_user where user_id = %s and ping_id in '
'(select id from ping where chat_id = %s)',
(user.id, chat.id))
conn.commit()
conn.close()
@bot.message_handler(content_types=['left_chat_member'])
def user_left(message: Message):
try:
remove_from_all(message.left_chat_member, message.chat)
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.reply_to(message, 'Unable to auto delete user from pings.\n'
'You will run into problems later =(\n'
'You can try to manually delete by opt-outing left user from every ping')
@bot.message_handler(regexp=r'(.*[@/]\S+.*)')
@ping.time()
def ping(message: Message):
try:
aliases = re.findall(r'.*?[@/](\w+).*?', message.text)
user_ids = set()
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('select distinct user_id from ping '
'left join ping_user on ping.id = ping_user.ping_id '
'where chat_id = %s and alias in %s', (message.chat.id, tuple(aliases)))
if c.rowcount == 0:
conn.close()
return
for user_id in c:
if user_id[0] is not None:
user_ids.add(user_id[0])
conn.close()
if message.from_user.id in user_ids:
user_ids.remove(message.from_user.id)
write = list()
cnt = 0
ind = 0
for user_id in user_ids:
if cnt == 0:
write += ['']
write[ind] += f'@{bot.get_chat_member(message.chat.id, user_id).user.username}, '
cnt += 1
if cnt >= config.MAX_PINGS_PER_MESSAGE - 1:
cnt = 0
ind += 1
try:
bot.delete_message(message.chat.id, message.id)
except Exception:
exceptions.inc()
if len(user_ids) == 0:
bot.send_message(message.chat.id, f'@{message.from_user.username} said "{message.text}".\n'
f'Noone will be pinged from your request, because {aliases} are empty. '
f'You can /list them')
return
bot.send_message(message.chat.id,
f'@{message.from_user.username} said "{message.text}".\n'
f'And pinged: {write[0][:-2]}{"..." if len(write) > 1 else ""}',
reply_markup=config.KEYBOARD_MARKUP)
ind = 0
for msg in write[1:]:
if msg == '':
continue
ind += 1
bot.send_message(message.chat.id,
f'...{msg[:-2]}{"..." if len(write) > ind + 1 else "."}')
except Exception as ex:
exceptions.inc()
logger.exception(ex)
bot.set_message_reaction(message.chat.id, message.id, [telebot.types.ReactionTypeEmoji("π")])
@bot.callback_query_handler(func=lambda callback: True)
def handle_query(callback: CallbackQuery):
try:
previous_text = callback.message.text
text = re.sub(rf'((^|\n)[{"".join(config.KEYBOARD_MARKUP_CHARS)}]:.*?)(?: {callback.from_user.username})(.*?($|\n))',
r'\g<1>\g<3>',
previous_text)
text = re.sub(rf'(?:^|\n)[{"".join(config.KEYBOARD_MARKUP_CHARS)}]:($|\n)',
r'\g<1>',
text)
text, changes = re.subn(rf'((^|\n){callback.data}:.*?)($|\n)',
rf'\g<1> {callback.from_user.username}\g<3>',
text)
if changes == 0:
text += f'\n{callback.data}: {callback.from_user.username}'
bot.edit_message_text(text, callback.message.chat.id, callback.message.id, reply_markup=config.KEYBOARD_MARKUP)
except Exception as ex:
exceptions.inc()
logger.exception(ex)
@bot.message_handler(regexp='(^(?![/]).*)')
@reply.time()
def check_reply(message: Message):
try:
if message.reply_to_message is not None and message.reply_to_message.from_user.id == bot.get_me().id and \
message.reply_to_message.text.split(' ')[0].startswith('@'):
try:
bot.delete_message(message.chat.id, message.id)
except Exception:
exceptions.inc()
bot.reply_to(message.reply_to_message,
f'@{message.from_user.username} said "{message.text[:config.MESSAGE_LENGTH]}".\n'
f'For: {message.reply_to_message.text.split(" ")[0]}')
except Exception as ex:
exceptions.inc()
logger.exception(ex)
@bot.inline_handler(lambda query: query.query == 'ping')
def ping_query(inline_query: InlineQuery):
inline_mode(inline_query, "/")
@bot.inline_handler(lambda query: query.query in ['opt_out', 'opt-out'])
def opt_out_query(inline_query: InlineQuery):
inline_mode(inline_query, "/opt_out ")
@inline.time()
def inline_mode(inline_query: InlineQuery, message: str):
try:
aliases = list()
conn = psycopg2.connect(config.DATABASE_URL, sslmode='require')
c = conn.cursor()
c.execute('select alias, chat_id from ping '
'join ping_user on ping.id = ping_user.ping_id '
'where user_id = %s order by alias', (inline_query.from_user.id,))
ind = 1
for alias, chat_id in c:
chat = bot.get_chat(chat_id)
aliases.append(
telebot.types.
InlineQueryResultArticle(f'{ind}',
f'{"@" + bot.get_me().username if chat.type == "private" else chat.title}: {alias}',
telebot.types.InputTextMessageContent(f'{message}{alias}')))
ind += 1
conn.close()
bot.answer_inline_query(inline_query.id, aliases, is_personal=True, cache_time=config.INLINE_CACHE_TIME)
except Exception as ex:
exceptions.inc()
logger.exception(ex)
if __name__ == "__main__":
start_http_server(port=10000)
bot.infinity_polling()