forked from Kirfi7/Red_Forms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
179 lines (146 loc) · 6.94 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import vk_api
from cfg import TOKEN, COMMANDS
from forms import add_form, get_form, form_accepted, form_denied, form_ranked_up, get_level
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
vk_session = vk_api.VkApi(token=TOKEN)
lp = VkBotLongPoll(vk_session, 218860473)
vk = vk_session.get_api()
def sender(for_user_id, message_text):
vk_session.method("messages.send", {
"user_id": for_user_id,
"message": message_text,
"random_id": 0
})
def chat_sender(for_chat_id, message_text):
vk_session.method("messages.send", {
"chat_id": for_chat_id,
"message": message_text,
"random_id": 0
})
def send_start_keyboard(for_user_id):
keyboard = VkKeyboard(
one_time=False,
inline=False
)
keyboard.add_button(
label="Принять форму",
color=VkKeyboardColor.POSITIVE
)
keyboard.add_button(
label="Помощь по боту",
color=VkKeyboardColor.PRIMARY
)
vk_session.method("messages.send", {
"user_id": for_user_id,
"message": "Ознакомьтесь с руководством по использованию:",
"random_id": 0,
"keyboard": keyboard.get_empty_keyboard()
})
vk_session.method("messages.send", {
"user_id": for_user_id,
"message": open("help.txt", "r", encoding="utf-8").readline(),
"random_id": 0,
"keyboard": keyboard.get_keyboard()
})
while True:
try:
for event in lp.listen():
if event.type == VkBotEventType.MESSAGE_NEW and not event.from_chat:
text = event.object.message['text']
user_id = event.object.message['from_id']
message_id = event.object.message['conversation_message_id']
lvl = get_level(user_id)
if text == "Начать" and lvl > 0:
send_start_keyboard(user_id)
elif text == "Помощь по боту" and lvl > 0:
with open("help.txt", mode="r", encoding="utf-8") as file:
help_text = file.read()
sender(user_id, help_text)
elif text == "Принять форму" and lvl > 0:
form_text, form_arg, form_sender = get_form(lvl)
i_keyboard = VkKeyboard(inline=True)
i_keyboard.add_callback_button(
label="Выполнено",
color=VkKeyboardColor.POSITIVE,
payload={"call_back": f"accept-{form_text}-{form_arg}-{form_sender}"})
i_keyboard.add_callback_button(
label="Нет в БД",
color=VkKeyboardColor.NEGATIVE,
payload={"call_back": f"deny-{form_text}-{form_arg}-{form_sender}"})
i_keyboard.add_line()
i_keyboard.add_callback_button(
label="Передать форму выше",
color=VkKeyboardColor.PRIMARY,
payload={"call_back": f"rank_up-{form_text}-{form_arg}-{form_sender}"})
# form_text = 0 возвращается в случае ошибки
if str(form_text) != "Error":
vk_session.method("messages.send", {
"user_id": user_id,
"message": form_text,
"random_id": 0,
"keyboard": i_keyboard.get_keyboard()
})
else:
sender(user_id, "В данный момент нет подходящих форм")
elif text[0] == "/" and lvl > 0 and "_" in text:
cmd = text[1:].split()
if cmd[0] in COMMANDS:
if len(cmd) >= int(COMMANDS[cmd[0]]['parameters']):
add_form(text, user_id)
elif event.type == VkBotEventType.MESSAGE_EVENT:
call_back = event.object.payload.get('call_back')
if "deny" in call_back:
form_text, form_row, form_sender = call_back.split("-")[1:]
e_keyboard = VkKeyboard(inline=True)
e_keyboard.add_callback_button(
label="ㅤㅤㅤㅤㅤ❌ㅤㅤㅤㅤㅤ",
color=VkKeyboardColor.NEGATIVE,
payload={
"type": "empty_callback"
}
)
vk_session.method("messages.edit", {
"peer_id": event.obj.peer_id,
"message": form_text,
"conversation_message_id": event.obj.conversation_message_id,
"keyboard": e_keyboard.get_keyboard()
})
form_denied(int(form_row))
sender(form_sender, f"⚠️ Внимание ⚠️\nНика по вашей форме нет в БД:\n{form_text}")
elif "accept" in call_back:
form_text, form_row, form_sender = call_back.split("-")[1:]
e_keyboard = VkKeyboard(inline=True)
e_keyboard.add_callback_button(
label="ㅤㅤㅤㅤㅤ✅ㅤㅤㅤㅤㅤ",
color=VkKeyboardColor.POSITIVE,
payload={
"type": "empty_callback"
}
)
vk_session.method("messages.edit", {
"peer_id": event.obj.peer_id,
"message": form_text,
"conversation_message_id": event.obj.conversation_message_id,
"keyboard": e_keyboard.get_keyboard()
})
form_accepted(int(form_row))
elif "rank_up" in call_back:
form_text, form_row, form_sender = call_back.split("-")[1:]
e_keyboard = VkKeyboard(inline=True)
e_keyboard.add_callback_button(
label="ㅤㅤㅤПереданоㅤㅤㅤ",
color=VkKeyboardColor.POSITIVE,
payload={
"type": "empty_callback"
}
)
vk_session.method("messages.edit", {
"peer_id": event.obj.peer_id,
"message": form_text,
"conversation_message_id": event.obj.conversation_message_id,
"keyboard": e_keyboard.get_keyboard()
})
form_ranked_up(int(form_row), get_level(event.obj.user_id))
except Exception as error:
print(error)