-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
152 lines (121 loc) · 3.66 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
import telebot
import yaml
from glossary import gls
import requests
from rich import print
from rich.console import Console
from datetime import datetime
from dotenv import load_dotenv
import os
# CONSTANTS
load_dotenv()
TOKEN = os.environ.get("TOKEN")
ADMIN = os.environ.get("ADMIN")
# GLOBALS
console = Console()
# FUNCTIONS
def _handler(bot):
@bot.message_handler(content_types=['text'])
def send_text(message):
console.log(f'Allowed: {message.chat.id} -> {check_banned(message)}')
if check_banned(message) == True:
pick_cmd(bot, message)
else:
_cnf(bot, message)
def pick_cmd(bot, message):
console.log(message.text.lower())
if message.text.lower() in gls:
try:
data = gls[message.text.lower()]()
bot.send_message(
message.chat.id,
data
)
logs(message)
except Exception as ex:
logs(message, ex)
bot.send_message(
message.chat.id,
f'{ex}'
)
else:
_cnf(bot, message)
def _cnf(bot, message) -> None:
cnf = f"CNF: {message.text.lower()}"
logs(message, cnf)
bot.send_message(
message.chat.id,
cnf
)
def logs(msg: object, ex='None'):
data = {
msg.date: {
'ID': msg.from_user.id,
'User Details': msg.from_user,
'Message': msg.text,
'Exeption': ex
}
}
try:
with open('logs.yml', 'a') as file:
_extracted_from_logs_12(file, data)
except Exception as ex:
with open('logs.yml', 'w') as file:
_extracted_from_logs_12(file, data)
# TODO Rename this here and in `logs`
def _extracted_from_logs_12(file, data):
console = Console(file=file)
console.rule(f"Report Generated {datetime.now().ctime()}")
yaml.dump(data, file, default_flow_style=False)
def ban_user(msg: object):
data = {
msg.date: {
'ID': msg.from_user.id
}
}
try:
with open('banned.yml', 'a') as file:
_extracted_from_logs_12(file, data)
except Exception as ex:
with open('banned.yml', 'w') as file:
_extracted_from_logs_12(file, data)
def check_banned(msg) -> bool:
try:
with open('banned.yml') as file:
data = yaml.load(file, Loader=yaml.Loader)
for k, v in data.items():
for x in v.keys():
if int(v[x]) == int(msg.from_user.id):
return False
return True
except Exception as ex:
# TODO WARNING!!!!!! AHTUNG!!!!
return True
# 523nwfn3p42uagrjfnkb8trv44hy
def telegram_bot(api_token):
bot = telebot.TeleBot(api_token)
@bot.message_handler(commands=['start'])
def start_msg(message):
if message.chat.id == int(ADMIN):
bot.send_message(
message.chat.id, f"Hello {message.from_user.first_name}!")
console.log(
f'Loggen in: {message.from_user.first_name, message.chat.id}')
else:
bot.send_message(message.chat.id, "403")
console.log(
f'Tried: {message.from_user.first_name, message.chat.id}')
ban_user(message)
logs(message)
_handler(bot)
bot.polling()
# TODO when in prod uncomment
# while True:
# try:
# console.log("[bold magenta]Started[/bold magenta]")
# bot.polling()
# except requests.exceptions.ReadTimeout:
# console.log("[bold magenta]Reloaded[/bold magenta]")
# continue
if __name__ == '__main__':
telegram_bot(TOKEN)