-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.py
More file actions
85 lines (69 loc) · 2.87 KB
/
Copy pathcommon.py
File metadata and controls
85 lines (69 loc) · 2.87 KB
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
import traceback
from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.utils.exceptions import BotBlocked
import config
import user as us
from db import dml_actions
user_dict = {}
# Bot initialization
bot = Bot(token=config.BOT_TG_TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())
async def print_log(user, message, state, command_dict=None):
tg_id = message.from_user.id
text = "No text"
if message.text is not None:
text = message.text
elif message.caption is not None:
text = message.caption
user_state = str(await state.get_state())
if user is None:
config.logger.info("Unknown user found:\nID: " + str(tg_id))
if message.from_user.username is not None:
config.logger.info("UserName: " + message.from_user.username)
return
elif command_dict is not None and text[1:] in command_dict or text == '/start':
config.logger.info("ID: " + str(tg_id) + ", state: " + user_state + ", message: " + text)
elif command_dict is not None and text in list(command_dict.values()):
config.logger.info("ID: " + str(tg_id) + ", state: " + user_state + ", message: " + text)
else:
config.logger.info("ID: " + str(tg_id) + ", state: " + user_state + ", message: text")
config.logger.debug("ID: " + str(tg_id) + ", state: " + user_state + ", message: " + text)
async def send_message(tg_id, text, reply_markup=None):
try:
if config.DEBUG_TG_ID is not None:
await bot.send_message(config.DEBUG_TG_ID, "Message to " + str(tg_id) + "\n" + text,
reply_markup=reply_markup)
else:
await bot.send_message(tg_id, text, reply_markup=reply_markup)
except BotBlocked:
config.logger.warn("Bot blocked for user: " + str(tg_id))
dml_actions.disable_user(get_user(tg_id=tg_id).user_id)
user_dict.pop(tg_id)
except:
config.logger.debug("Can not send a message to: " + str(tg_id))
traceback.print_exc()
def get_user(message=None, tg_id=None, user_id=None):
if message is None and tg_id is None and user_id is None:
return None
if message is not None:
tg_id = message.from_user.id
user = None
if tg_id is not None and tg_id in user_dict:
user = user_dict[tg_id]
if not user.check():
user.refresh()
elif tg_id is None:
for tg_id_tmp, user_tmp in user_dict.items():
if user_id is not None and user_tmp.user_id == user_id:
user = user_tmp
if not user.check():
user.refresh()
break
if user is None:
# If tg_id not in dict, create User instance
user = us.User(user_id, tg_id)
if user.user_id is None:
return None
user_dict[tg_id] = user
return user