This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
core.py
81 lines (67 loc) · 2.56 KB
/
core.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
import sys
import telebot
from telebot import types
import bot_functions as bf
from configloader import config
import logging
# Trying to import colored logs
try:
import coloredlogs
logs_level_styles = dict(
spam=dict(color=22, faint=True),
debug=dict(color=28),
verbose=dict(color='blue'),
info=dict(),
notice=dict(color=220),
warning=dict(color=202),
success=dict(color=118, bold=True),
error=dict(color=124),
critical=dict(background='red', bold=True),
)
except ImportError:
coloredlogs = None
def main():
"""The core code of the program"""
# Logs initialization
log = logging.getLogger(__name__)
logging.root.setLevel(config["Logging"]["level"])
stream_handler = logging.StreamHandler()
if coloredlogs is not None:
stream_handler.formatter = coloredlogs.ColoredFormatter(config["Logging"]["format"], style="{",
level_styles=logs_level_styles)
else:
stream_handler.formatter = logging.Formatter(config["Logging"]["format"], style="{")
logging.root.handlers.clear()
logging.root.addHandler(stream_handler)
log.debug("Logging setup successfully!")
# Ignore most python-telegram-bot logs, as they are useless most of the time
logging.getLogger("urllib3.connectionpool").setLevel("ERROR")
# logging.getLogger("Telegram").setLevel("ERROR")
# Trying to connect with the Telegram bot by the token
try:
bot = telebot.TeleBot(config["Telegram"]["token"])
bot.get_me()
log.debug('Connection to the bot was successful!')
except telebot.apihelper.ApiException:
log.error('A request to the Telegram API was unsuccessful.')
log.fatal('Write the valid token in the config file and restart the script')
sys.exit(1)
log.debug("Bot connection is valid!")
log.info("avarice is started!")
@bot.message_handler(commands=["start"])
def start(message):
bf.sending_start_message(bot, message, types)
bf.start_func(message)
@bot.message_handler(commands=["help"])
def helping(message):
bf.sending_help_message(bot, message)
@bot.message_handler(content_types=["text"])
def message_handler(message):
if message.chat.type == 'private':
bf.handler(bot, types, message, None)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
bf.handler(bot, types, None, call)
bot.polling(none_stop=True)
if __name__ == '__main__':
main()