-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
139 lines (117 loc) · 6.15 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
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
import logging, phonenumbers
from aiogram import Bot, Dispatcher, types, executor
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from config import TOKEN
import keyboard as kb
import message_generator as mg
from bot_sql_api import SpamDB
logging.basicConfig(level=logging.INFO)
bot = Bot(token=TOKEN)
dp = Dispatcher(bot, storage=MemoryStorage())
bd = SpamDB(auto_save=True)
@dp.message_handler(commands=["help"])
async def start(massage: types.Message):
await massage.answer('This bot is a database with "bad" numbers. Enter /start to start or /reviews to check your reviews.')
@dp.message_handler(commands=["start"])
async def start(massage: types.Message):
await bot.send_photo(massage.chat.id, open("img/spamy.png", "rb"))
await massage.answer(mg.create_hi_string()[0])
await massage.answer(mg.create_hi_string()[1])
@dp.message_handler(commands=["reviews"])
async def reviews(massage: types.Message):
reviews = bd.find_personal_reviews(types.User.get_current().id)
if reviews == -1:
await massage.answer("You have no reviews.")
else:
for rv in range(len(reviews)):
dell_or_edit_inkd = InlineKeyboardMarkup()
dell_or_edit_inkd.add(InlineKeyboardButton("Delete", callback_data=kb.call_back.new(edit_or_del = "del",
numb = reviews[rv][3])),
InlineKeyboardButton("Edit", callback_data=kb.call_back.new(edit_or_del="edit",
numb=reviews[rv][3])))
await massage.answer(str(reviews[rv][0]) + " " + str(reviews[rv][1]) + " " + str(reviews[rv][2]), reply_markup=dell_or_edit_inkd)
@dp.callback_query_handler()
async def callback(callback_query: types.CallbackQuery):
data = str(callback_query.data).split(sep=":")
if data[0] == "rew":
if data[1] == "del":
await callback_query.answer("This review deleted.")
await callback_query.message.answer("This review deleted.")
bd.delete_review(int(data[2]))
bd.save()
if data[1] == "edit":
await callback_query.answer("Please enter new comment.")
await callback_query.message.answer(mg.create_need_comment_string())
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
await state.update_data(rew_id = int(data[2]))
await state.set_state("Edit")
@dp.message_handler(state="Edit")
async def Edit(massage: types.Message):
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
data = await state.get_data()
bd.update_review((data["rew_id"], massage.text))
bd.save()
await massage.answer(mg.create_thank_update_string())
await state.set_state(None)
@dp.message_handler(state="Comment")
async def Comment(massage: types.Message):
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
data = await state.get_data()
bd.insert_review((data["number"], data["bad_class"], massage.text, types.User.get_current().id))
bd.save()
await massage.answer(mg.create_thank_reply_string())
await state.set_state(None)
@dp.message_handler(state="Review")
async def Review(massage: types.Message):
bad_class = -1
if massage.text == "Spam":
bad_class = 0
elif massage.text == "Fraud":
bad_class = 1
elif massage.text == "Threats":
bad_class = 2
elif massage.text == "Advertisement":
bad_class = 3
else:
bad_class = 4 # other
await massage.answer(mg.create_need_comment_string(), reply_markup=types.ReplyKeyboardRemove())
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
await state.set_state("Comment")
await state.update_data(bad_class=bad_class)
@dp.message_handler(state="Number")
async def Number(massage: types.Message):
if massage.text == "Reply":
await massage.answer(mg.create_choose_class_string(), reply_markup=kb.class_kb)
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
await state.set_state("Review")
elif massage.text == "Continue checking":
await massage.answer("Ok", reply_markup=types.ReplyKeyboardRemove())
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
await state.set_state(None)
@dp.message_handler()
async def work(massage: types.Message):
try:
number = phonenumbers.parse(massage.text)
data = bd.find_phone(str(number.country_code) + str(number.national_number))
if data != -1:
bd_data = bd.find_reviews(data[0])
await bot.send_photo(massage.chat.id, open("img/spamy_attention.png", "rb"))
await massage.answer(mg.create_attention_message("+" + data[1], data[3], ', '.join(bd.find_phone_classes(data[0])),
data[2],
bd_data)
, reply_markup=kb.review_or_continue_kb)
else:
await bot.send_photo(massage.chat.id, open("img/spamy_ok.png", "rb"))
await massage.answer(mg.create_ok_message("+" + str(number.country_code) + str(number.national_number)),
reply_markup=kb.review_or_continue_kb)
state = dp.current_state(chat=types.Chat.get_current().id, user=types.User.get_current().id)
await state.set_state("Number")
await state.update_data(number=str(number.country_code) + str(number.national_number))
except:
await massage.answer("Invalid number")
await massage.answer(mg.create_default_string())
def main():
executor.start_polling(dp, skip_updates=True)
if __name__ == "__main__":
main()