-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
76 lines (58 loc) · 2.27 KB
/
main.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
import logging
import os
from logging import handlers
from pyrogram import Client
from dotenv import load_dotenv
from pyrogram.errors import PeerIdInvalid
from pyrogram.types import BotCommand, BotCommandScopeChat
from db import repository
from tg import handlers
from tg.strings import resolve_msg
load_dotenv()
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.ERROR)
# max bytes file 5 MB
file_handler = logging.handlers.RotatingFileHandler(
filename='my_log.log', maxBytes=5 * 1024 * 1024, backupCount=1, mode='D'
)
# file_handler = logging.FileHandler('your_log_file.log')
file_handler.setLevel(logging.DEBUG)
logging.basicConfig(
format='Time> %(asctime)s | Module> %(module)s | LevelName> %(levelname)s | '
'Message> %(message)s | Name> %(name)s | FuncName> %(funcName)s | Line > %(lineno)d',
level=logging.INFO,
handlers=[
file_handler,
console_handler
]
)
logger = logging.getLogger(__name__)
def main():
app = Client(name=os.environ['PYROGRAM_NAME_SESSION'], api_id=os.environ['TELEGRAM_API_ID'],
api_hash=os.environ['TELEGRAM_API_HASH'], bot_token=os.environ['TELEGRAM_BOT_TOKEN'])
for admin in os.environ['ADMINS'].split(','):
if not repository.is_admin_exists(tg_id=int(admin)):
repository.create_admin(tg_id=int(admin))
set_commands_for_admin(c=app, admin_id=int(admin))
logger.debug(f"Bot {app.name} is up and running!")
for handler in handlers.HANDLERS:
app.add_handler(handler)
app.run()
def set_commands_for_admin(c: Client, admin_id: int):
c.start()
try:
c.set_bot_commands(
commands=[
BotCommand(command='start', description='start'),
BotCommand(command='add_group', description=resolve_msg('COMMAND_ADD_GROUP')),
BotCommand(command='delete_group', description=resolve_msg('COMMAND_DELETE_GROUP')),
BotCommand(command='unban', description=resolve_msg('COMMAND_UNBAN')),
BotCommand(command='send', description=resolve_msg('COMMAND_SEND'))
],
scope=BotCommandScopeChat(chat_id=admin_id)
)
except PeerIdInvalid as e:
logger.error(e)
c.stop()
if __name__ == '__main__':
main()