This repository has been archived by the owner on Sep 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Bot.py
executable file
·433 lines (330 loc) · 16.2 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
from telegram.ext import Updater, Filters, CommandHandler, ConversationHandler, MessageHandler, RegexHandler
from telegram import ReplyKeyboardMarkup, ReplyKeyboardRemove
from urllib.parse import unquote
import json
import os
from QIWI_API import UserQiwi
from QIWI_API import QiwiError, TokenError, TransactionNotFound, WalletError, MapError, NotFoundAddress, CheckError, \
WrongEmail, WrongNumber, TransactionError
VERSION = "Bot v1.0\nQiwiAPI v1.0"
LANGUAGE = "eng"
LANGUAGES = json.load(open("Languages.json"))
DIALOGS = LANGUAGES[LANGUAGE]
def start(bot, update):
update.message.reply_text(DIALOGS["start"])
return 1
def check_token(bot, update, user_data):
user_data["token"] = update.message.text
try:
user_data["user"] = UserQiwi(user_data["token"])
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 2
except TokenError:
update.message.reply_text(DIALOGS["error"])
start(bot, update)
def balance(bot, update, user_data):
try:
update.message.reply_text(user_data["user"].get_balance())
return 2
except QiwiError:
update.message.reply_text(DIALOGS["q_error"])
return 2
def transactions(bot, update):
markup = ReplyKeyboardMarkup(transactions_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 3
def check_status(bot, update):
update.message.reply_text(DIALOGS["transaction_id"])
return 4
def answer_about_transaction(bot, update, user_data):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
try:
update.message.reply_text(user_data["user"].get_info_about_transaction(update.message.text),
reply_markup=markup)
except TransactionNotFound:
update.message.reply_text(DIALOGS["tr_error"], reply_markup=markup)
return 2
def last(bot, update, user_data):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
try:
answer = user_data["user"].get_last_transactions()
if not answer:
update.message.reply_text(DIALOGS["no_recent_tr"], reply_markup=markup)
else:
update.message.reply_text(answer, reply_markup=markup)
except TransactionNotFound:
update.message.reply_text(DIALOGS["q_error"], reply_markup=markup)
return 2
def terminals(bot, update, user_data):
user_data["location_coords"] = False
markup = ReplyKeyboardMarkup(terminals_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 5
def take_command_found_address(bot, update, user_data):
user_data["map"] = update.message.text == DIALOGS["on map"]
markup = ReplyKeyboardMarkup(terminals2_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 6
def take_address(bot, update):
update.message.reply_text(DIALOGS["ent_address"])
return 7
def take_locaion(bot, update, user_data):
location = update.message.location
coords = (location["longitude"], location["latitude"])
user_data["location_coords"] = ",".join(map(str, coords))
answer_about_terminates(bot, update, user_data)
return 2
def answer_about_terminates(bot, update, user_data):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
if update.message.text != DIALOGS["last ip"]:
if user_data["location_coords"]:
address = user_data["location_coords"]
else:
address = update.message.text
else:
address = None
try:
url, address = user_data["user"].get_map_terminates(address)
except NotFoundAddress:
update.message.reply_text(DIALOGS["wrong_address"], reply_markup=markup)
return 2
except MapError:
update.message.reply_text(DIALOGS["data_error"], reply_markup=markup)
return 2
if user_data["map"]:
bot.sendPhoto(update.message.chat_id, url, reply_markup=markup)
else:
correct_address = []
for i in address:
try:
word = unquote(unquote(i))
if word:
correct_address.append(word)
except TypeError:
pass
update.message.reply_text("\n".join(correct_address), reply_markup=markup)
return 2
def options(bot, update):
markup = ReplyKeyboardMarkup(options_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 8
def get_info(bot, update, user_data):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
update.message.reply_text(user_data["user"].get_info(), reply_markup=markup)
return 2
def take_new_token(bot, update):
update.message.reply_text(DIALOGS["new_token"])
return 1
def update_user(bot, update, user_data):
user_data["user"].update_info()
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
update.message.reply_text(DIALOGS["up_acc_inf"], reply_markup=markup)
return 2
def version(bot, update):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
update.message.reply_text(VERSION, reply_markup=markup)
return 2
def check(bot, update, user_data):
user_data["check_type_rest"] = None
markup = ReplyKeyboardMarkup(check_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 9
def dialog_email(bot, update, user_data):
user_data["check_type_rest"] = "email"
user_data["email"] = None
markup = ReplyKeyboardMarkup(email_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 10
def enter_email(bot, update):
update.message.reply_text(DIALOGS["enter email"])
return 11
def get_email(bot, update, user_data):
user_data["email"] = update.message.text
update.message.reply_text(DIALOGS["enter transaction id"])
return 13
def enter_transaction_id(bot, update):
update.message.reply_text(DIALOGS["enter transaction id"])
return 13
def get_transaction_id(bot, update, user_data):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
if user_data["check_type_rest"] == "email":
if user_data["email"] is None:
try:
user_data["user"].send_check_email(update.message.text)
update.message.reply_text(DIALOGS["sent check"], reply_markup=markup)
except TransactionNotFound:
update.message.reply_text(DIALOGS["tr_error"], reply_markup=markup)
except WrongEmail:
update.message.reply_text(DIALOGS["error email"], reply_markup=markup)
else:
try:
user_data["user"].send_check_email(update.message.text, user_data["email"])
update.message.reply_text(DIALOGS["sent check"], reply_markup=markup)
except TransactionNotFound:
update.message.reply_text(DIALOGS["tr_error"], reply_markup=markup)
except WrongEmail:
update.message.reply_text(DIALOGS["error email"], reply_markup=markup)
else:
name = str(update.message.chat_id) + ".jpeg"
try:
user_data["user"].get_image_check(update.message.text, name)
bot.sendPhoto(update.message.chat_id, open(name, mode="rb"), reply_markup=markup)
except TransactionNotFound:
update.message.reply_text(DIALOGS["tr_error"], reply_markup=markup)
except CheckError:
update.message.reply_text(DIALOGS["check error"], reply_markup=markup)
os.remove(name)
return 2
def pay(bot, update, user_data):
user_data["check_type_rest"] = None
markup = ReplyKeyboardMarkup(pay_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 14
def enter_user_id(bot, update):
update.message.reply_text(DIALOGS["enter user id"])
return 17
def get_user_id(bot, update, user_data):
user_data["user_id"] = update.message.text
update.message.reply_text(DIALOGS["enter amount"])
return 19
def mobile_phone(bot, update, user_data):
user_data["check_type_rest"] = "mobile"
user_data["number"] = None
markup = ReplyKeyboardMarkup(mobile_keyboard, one_time_keyboard=True)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 15
def enter_mobile(bot, update):
update.message.reply_text(DIALOGS["enter phone"])
return 16
def get_mobile(bot, update, user_data):
user_data["number"] = update.message.text
update.message.reply_text(DIALOGS["enter amount"])
return 19
def enter_amount(bot, update):
update.message.reply_text(DIALOGS["enter amount"])
return 19
def get_amount(bot, update, user_data):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
if user_data["check_type_rest"] == "mobile":
if user_data["number"] is None:
try:
update.message.reply_text(user_data["user"].transaction_telephone(update.message.text),
reply_markup=markup)
except TransactionNotFound:
update.message.reply_text(DIALOGS["tr_s_error"], reply_markup=markup)
except WrongNumber:
update.message.reply_text(DIALOGS["error number"], reply_markup=markup)
except WalletError:
update.message.reply_text(DIALOGS["error wallet"], reply_markup=markup)
else:
try:
update.message.reply_text(user_data["user"].transaction_telephone(update.message.text,
user_data["number"]),
reply_markup=markup)
except TransactionError:
update.message.reply_text(DIALOGS["tr_s_error"], reply_markup=markup)
except WrongNumber:
update.message.reply_text(DIALOGS["error number"], reply_markup=markup)
except WalletError:
update.message.reply_text(DIALOGS["error wallet"], reply_markup=markup)
else:
try:
update.message.reply_text(user_data["user"].transaction_qiwi(user_data["user_id"], update.message.text),
reply_markup=markup)
except TransactionError:
update.message.reply_text(DIALOGS["tr_s_error"], reply_markup=markup)
except WalletError:
update.message.reply_text(DIALOGS["error wallet"], reply_markup=markup)
return 2
def wrong_answer(bot, update):
update.message.reply_text(DIALOGS["wrong_answer"])
def back(bot, update):
markup = ReplyKeyboardMarkup(start_keyboard, one_time_keyboard=False)
update.message.reply_text(DIALOGS["command"], reply_markup=markup)
return 2
def stop(bot, update):
update.message.reply_text(DIALOGS["off"], reply_markup=ReplyKeyboardRemove())
return ConversationHandler.END
button_back = ["/{}".format(DIALOGS["back"])]
start_keyboard = [["/{}".format(DIALOGS["balance"]), "/{}".format(DIALOGS["pay"])],
["/{}".format(DIALOGS["transactions"]), "/{}".format(DIALOGS["check"])],
["/{}".format(DIALOGS["terminals"]), "/{}".format(DIALOGS["options"])]]
transactions_keyboard = [[DIALOGS["check status"], DIALOGS["last"]],
button_back]
terminals_keyboard = [[DIALOGS["on map"], DIALOGS["address"]],
button_back]
terminals2_keyboard = [[DIALOGS["last ip"], DIALOGS["enter address"]],
button_back]
options_keyboard = [[DIALOGS["change token"], DIALOGS["version"]],
[DIALOGS["update user"], DIALOGS["get info"]],
button_back]
check_keyboard = [[DIALOGS["get image"], DIALOGS["send to email"]],
button_back]
email_keyboard = [[DIALOGS["my email"], DIALOGS["other email"]],
button_back]
pay_keyboard = [[DIALOGS["mobile phone"], DIALOGS["qiwi user"]],
button_back]
mobile_keyboard = [[DIALOGS["my phone"], DIALOGS["other phone"]],
button_back]
markup = None
def main():
updater = Updater("TOKEN")
dp = updater.dispatcher
command_back = CommandHandler(LANGUAGES[LANGUAGE]["back"], back)
wrong_answer_hd = MessageHandler(Filters.text, wrong_answer)
conv_handler = ConversationHandler(
entry_points=[CommandHandler("start", start)],
states={1: [MessageHandler(Filters.text, check_token, pass_user_data=True)],
2: [CommandHandler(DIALOGS["balance"], balance, pass_user_data=True),
CommandHandler(DIALOGS["pay"], pay, pass_user_data=True),
CommandHandler(DIALOGS["transactions"], transactions),
CommandHandler(DIALOGS["check"], check, pass_user_data=True),
CommandHandler(DIALOGS["terminals"], terminals, pass_user_data=True),
CommandHandler(DIALOGS["options"], options),
wrong_answer_hd],
3: [RegexHandler("^{}$".format(DIALOGS["check status"]), check_status),
RegexHandler("^{}$".format(DIALOGS["last"]), last, pass_user_data=True),
command_back, wrong_answer_hd],
4: [MessageHandler(Filters.text, answer_about_transaction, pass_user_data=True)],
5: [RegexHandler("{}|{}".format(DIALOGS["on map"], DIALOGS["address"]),
take_command_found_address, pass_user_data=True),
command_back, wrong_answer_hd],
6: [RegexHandler("^{}$".format(DIALOGS["enter address"]), take_address),
RegexHandler("^{}$".format(DIALOGS["last ip"]), answer_about_terminates,
pass_user_data=True),
command_back, wrong_answer_hd],
7: [MessageHandler(Filters.text, answer_about_terminates, pass_user_data=True),
MessageHandler(Filters.location, take_locaion, pass_user_data=True)],
8: [RegexHandler("^{}$".format(DIALOGS["change token"]), take_new_token),
RegexHandler("^{}$".format(DIALOGS["update user"]), update_user, pass_user_data=True),
RegexHandler("^{}$".format(DIALOGS["version"]), version),
RegexHandler("^{}$".format(DIALOGS["get info"]), get_info, pass_user_data=True),
command_back, wrong_answer_hd],
9: [RegexHandler("^{}$".format(DIALOGS["get image"]), enter_transaction_id),
RegexHandler("^{}$".format(DIALOGS["send to email"]), dialog_email, pass_user_data=True),
command_back, wrong_answer_hd],
10: [RegexHandler("^{}$".format(DIALOGS["my email"]), enter_transaction_id),
RegexHandler("^{}$".format(DIALOGS["other email"]), enter_email),
command_back, wrong_answer_hd],
11: [MessageHandler(Filters.text, get_email, pass_user_data=True)],
12: [MessageHandler(Filters.text, enter_transaction_id)],
13: [MessageHandler(Filters.text, get_transaction_id, pass_user_data=True)],
14: [RegexHandler("^{}$".format(DIALOGS["qiwi user"]), enter_user_id),
RegexHandler("^{}$".format(DIALOGS["mobile phone"]), mobile_phone, pass_user_data=True),
command_back, wrong_answer_hd],
15: [RegexHandler("^{}$".format(DIALOGS["my phone"]), enter_amount),
RegexHandler("^{}$".format(DIALOGS["other phone"]), enter_mobile),
command_back, wrong_answer_hd],
16: [MessageHandler(Filters.text, get_mobile, pass_user_data=True)],
17: [MessageHandler(Filters.text, get_user_id, pass_user_data=True)],
18: [MessageHandler(Filters.text, enter_amount)],
19: [MessageHandler(Filters.text, get_amount, pass_user_data=True)]},
fallbacks=[CommandHandler("stop", stop)]
)
dp.add_handler(conv_handler)
print("Bot started...")
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()