-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
139 lines (132 loc) · 7.16 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
from config import *
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
from bot_functions import *
import time
# Text pinned message
TEXT = None
# Peoples answered in pinned message (only '+' or '-')
plus = []
minus = []
plus_minus = []
# conversation_message_id pinned msg
bot_conv_msg_id = None
while True:
try:
# bot auth
auth = vk_api.VkApi(token=token)
# Process of reading messages
longpoll = VkBotLongPoll(auth, group_id=group_id)
# Listening messages
for event in longpoll.listen():
print(event)
# If message was from VK chat and not empty
if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat and event.message.get('text') != '':
# Getting message's text
text_message = event.message.get('text')
# If message has a flag 'bot_help', we should reset plus and minus
if 'bot_help' in text_message and event.obj['message']['from_id'] == admin_id:
plus = []
minus = []
plus_minus = []
# Getting inclusive message id in chat
conv_msg_id = event.obj['message']['conversation_message_id']
peer_id = event.obj['message']['peer_id']
# Getting inclusive chat_id
sender = event.chat_id
# Duplicate admin massage
write_msg(auth, sender, text_message)
# Pin message which duplicated
bot_conv_msg_id = conv_msg_id + 1
lego = pin_msg(auth, peer_id, bot_conv_msg_id, text_message)
if 'bot_edit' in text_message and event.obj['message'][
'from_id'] == admin_id and bot_conv_msg_id is not None:
lego[0] = text_message[:-8]
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
if 'bot_add+' in text_message and 'bot_add+-' not in text_message and event.obj['message'][
'from_id'] == admin_id and bot_conv_msg_id is not None:
name_need_to_add = (text_message.replace('bot_add+ ', ''))
if name_need_to_add in plus:
plus.remove(name_need_to_add)
if name_need_to_add in minus:
minus.remove(name_need_to_add)
if name_need_to_add in plus_minus:
plus_minus.remove(name_need_to_add)
plus.append(name_need_to_add)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
if 'bot_add+-' in text_message and event.obj['message'][
'from_id'] == admin_id and bot_conv_msg_id is not None:
name_need_to_add = (text_message.replace('bot_add+- ', ''))
if name_need_to_add in plus:
plus.remove(name_need_to_add)
if name_need_to_add in minus:
minus.remove(name_need_to_add)
if name_need_to_add in plus_minus:
plus_minus.remove(name_need_to_add)
plus_minus.append(name_need_to_add)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
if 'bot_add-' in text_message and event.obj['message'][
'from_id'] == admin_id and bot_conv_msg_id is not None:
name_need_to_add = (text_message.replace('bot_add- ', ''))
if name_need_to_add in plus:
plus.remove(name_need_to_add)
if name_need_to_add in minus:
minus.remove(name_need_to_add)
if name_need_to_add in plus_minus:
plus_minus.remove(name_need_to_add)
minus.append(name_need_to_add)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
if 'delete' in text_message and event.obj['message'][
'from_id'] == admin_id and bot_conv_msg_id is not None:
name_need_to_add = (text_message.replace('delete ', ''))
if name_need_to_add in plus:
plus.remove(name_need_to_add)
if name_need_to_add in minus:
minus.remove(name_need_to_add)
if name_need_to_add in plus_minus:
plus_minus.remove(name_need_to_add)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
# Processing another messages
try:
# If this is a reply to a bot message
if event.obj['message']['reply_message']['conversation_message_id'] == bot_conv_msg_id:
# Getting fullname of sender
full_name = get_user(auth, event.obj['message']['from_id'])
# if answer was '+'
if '+' in text_message and '+-' not in text_message and '-+' not in text_message:
if full_name in plus:
plus.remove(full_name)
if full_name in minus:
minus.remove(full_name)
if full_name in plus_minus:
plus_minus.remove(full_name)
plus.append(full_name)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
# if answer was '-'
if '-' in text_message and '+-' not in text_message and '-+' not in text_message:
if full_name in minus:
minus.remove(full_name)
if full_name in plus:
plus.remove(full_name)
if full_name in plus_minus:
plus_minus.remove(full_name)
minus.append(full_name)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
# if answer was '+-' or '-+'
if '+-' in text_message or '-+' in text_message:
if full_name in plus:
plus.remove(full_name)
if full_name in minus:
minus.remove(full_name)
if full_name in plus_minus:
plus_minus.remove(full_name)
plus_minus.append(full_name)
edit_msg(auth, peer_id, bot_conv_msg_id, lego, plus, minus, plus_minus)
# if message isn't a reply to a bot message
except Exception as ex:
print(ex.args)
continue
except Exception as ex:
print(ex)
print('\n Переподключерие к серверам ВК \n')
time.sleep(5)