forked from realByg/digitalocean-helper-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
78 lines (58 loc) · 1.74 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
import json
import logging
import traceback
from os import environ
from typing import Union
import urllib.parse as urlparse
from urllib.parse import parse_qs
import telebot
from telebot.types import CallbackQuery, Message
from _bot import bot
# noinspection PyUnresolvedReferences
from modules import *
bot_admins = json.loads(environ.get('bot_admins'))
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)
command_dict = {
'/start': 'start',
'/aa': 'add_account',
'/ma': 'manage_accounts',
'/bta': 'batch_test_accounts',
'/cd': 'create_droplet',
'/md': 'manage_droplets',
}
@bot.message_handler(content_types=['text'])
def text_handler(m: Message):
try:
logger.info(m)
if m.from_user.id not in bot_admins:
return
if m.text in command_dict.keys():
globals()[command_dict[m.text]](m)
except Exception as e:
traceback.print_exc()
handle_exception(m, e)
@bot.callback_query_handler(func=lambda call: True)
def callback_query_handler(call: CallbackQuery):
try:
logger.info(call)
if call.from_user.id not in bot_admins:
return
callback_data = urlparse.urlparse(call.data)
func_name = callback_data.path
data = parse_qs(callback_data.query)
if func_name in globals():
args = [call]
if len(data.keys()) > 0:
args.append(data)
globals()[func_name](*args)
except Exception as e:
traceback.print_exc()
handle_exception(call, e)
def handle_exception(d: Union[Message, CallbackQuery], e):
bot.send_message(
text=f'出错啦\n'
f'<code>{e}</code>',
chat_id=d.from_user.id,
parse_mode='HTML'
)