-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.py
165 lines (142 loc) · 5.81 KB
/
worker.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
import MySQLdb
import tbot_api.tbot_api as tbot_api
import tbot_api.tbot_exception as tbot_exception
import commands
# ------------------------------------------------------------------------------- #
#
# ------------------------------------------------------------------------------- #
class worker():
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def __init__(self, request_data, services_obj):
""" Init """
self.response_message = None
self.command = None
self.command_params = None
self.services = services_obj
command_dict = {
'/start': 'command_start',
'/stop': 'command_stop',
'/password': 'command_password',
'/time': 'command_time',
'/test': 'command_test',
'/keyboard_on': 'command_keyboard_show',
'/keyboard_off': 'command_keyboard_hide',
}
self.tbot_api = tbot_api.tbot_api(
token=self.services.get_config()["tg_token"],
request_data=request_data)
self.commands = commands.commands(tbot_api=self.tbot_api)
self.tbot_api.set_bot_commands(
command_dict=command_dict, command_exec=self.commands)
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def start_worker(self):
"""
"""
if self.tbot_api.is_command():
try:
self.tbot_api.select_command()
except tbot_exception.TbotExceptionInvalidCommand as err:
self.tbot_api.send_message(err.message)
try:
self.tbot_api.run_command()
except Exception as err:
self.tbot_api.send_message(str(err))
elif self.is_callback():
self.callback_action()
else:
self.default_action()
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def check_user_info(self):
"""
"""
mydb = MySQLdb.connect(
host=self.services.get_config()["db"]["host"],
user=self.services.get_config()["db"]["user"],
passwd=self.services.get_config()["db"]["passwd"],
db=self.services.get_config()["db"]["db"],
)
mycursor = mydb.cursor()
sql = "SELECT * FROM tbot WHERE id = {}".format(
self.tbot_api.get_chat_id())
mycursor.execute(sql)
myresult = mycursor.fetchone()
mycursor.close()
mydb.close()
if not myresult:
self.services.logs(msg=myresult, file_name="worker")
self.tbot_api.send_message(
"User id {} doesn't exist".format(self.tbot_api.get_chat_id()))
# # --------------------------------------------------------------------------- #
# #
# # --------------------------------------------------------------------------- #
# def select_command(self):
# """
# Проверяем есть такая команда
# """
# command_dict = {
# '/start': 'command_start',
# '/stop': 'command_stop',
# '/password': 'command_password',
# '/time': 'command_time',
# '/test': 'command_test',
# '/keyboard_on': 'command_keyboard_show',
# '/keyboard_off': 'command_keyboard_hide',
# # '/inline_keyboard': 'command_inline_keyboard',
# }
# command, *params = self.tbot_api.get_text().lower().split(' ')
# if command in command_dict:
# self.command = command_dict.get(command)
# self.command_params = ' '.join(params).strip()
# else:
# self.response_message = 'Нет такой команды *{}*'.format(
# self.tbot_api.get_text())
# self.tbot_api.send_message(self.response_message)
# # --------------------------------------------------------------------------- #
# #
# # --------------------------------------------------------------------------- #
# def is_command(self):
# """
# Проверить является ли request_message командой
# """
# if self.tbot_api.get_text().lower()[0] == '/':
# return True
# return False
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def is_callback(self):
"""
Проверить является ли request callback (inline keyboard)
"""
if self.tbot_api.callback_data:
return True
return False
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def callback_action(self):
"""
"""
self.response_message = 'Вы нажали *{}*'.format(
self.tbot_api.get_callback_data())
self.tbot_api.send_message(self.response_message)
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def default_action(self):
"""
"""
self.echo_server()
# --------------------------------------------------------------------------- #
#
# --------------------------------------------------------------------------- #
def echo_server(self):
"""
"""
self.commands.command_echo_server()