-
Notifications
You must be signed in to change notification settings - Fork 0
/
longpoll_bot.py
190 lines (171 loc) · 7.88 KB
/
longpoll_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
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
180
181
182
183
184
185
186
187
188
189
190
"""
this file establishes communication
with longpoll server
Author: Raman Zatsarenko
"""
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api
import random
import time
logs_file = "/home/pi/Documents/logs.txt"
ID_ZHENYA = 369591033
gifs = ["doc151898113_553723460", "doc151898113_553723478", "doc151898113_553723471", "doc151898113_553723914",
"doc151898113_553723932", "doc151898113_553723949", "doc151898113_553724109"]
def connect(token, group_id):
"""
this method established connection between server and bot
:param token: group token
:param group_id: group id
:return: longpoll object
"""
vk = vk_api.VkApi(token=token)
vk._auth_token()
vk.get_api()
longpoll = VkBotLongPoll(vk, group_id)
return longpoll, vk
def bot_loop(longpoll_object, vk):
"""
bot's main loop
listens until the program is terminated
:param longpoll_object: longpoll object
:return: none
"""
users_storage = []
for event in longpoll_object.listen():
print(event)
received_event_logs(logs_file)
if event.type == VkBotEventType.MESSAGE_NEW:
# bot was added to the conversation
if event.object.message["text"] == "":
if event.object.message["action"]["type"] == "chat_invite_user":
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "Pidor detector activated. Initializing all systems...",
"random_id": random.random(), "attachment": "doc151898113_553717223"})
else:
if event.object.peer_id != event.object.from_id:
# received message from a user
if event.object.message["text"] == "hello":
# debugging print statement
print(event.object.message["peer_id"])
vk.method("messages.send",
{"user_id": event.object.message["from_id"], "peer_id": event.object.message["peer_id"],
"message": "hi there", "random_id": random.random()})
# received a message from chat group
elif event.object.message["text"] == "[club119335933|@russia4life] hello":
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "hi there", "random_id": random.random()})
elif event.object.message["text"] == "[club119335933|@russia4life] i m in":
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "@id" + str(event.object.message["from_id"]) + " принял",
"random_id": random.random()})
users_storage.append(event.object.message["from_id"])
elif event.object.message["text"] == "[club119335933|@russia4life] result":
if len(users_storage) != 0:
pidor_id = choose_pidor(users_storage)
time.sleep(3)
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "@id" + str(pidor_id) + " " + random_phrase(),
"random_id": random.random(), "attachment": gifs[random.randint(0, len(gifs)-1)]})
users_storage = []
else:
# received message from a user
if event.object.message["text"] == "hello":
vk.method("messages.send",
{"user_id": event.object.message["from_id"], "peer_id": event.object.message["peer_id"],
"message": "hi there", "random_id": random.random()})
# received a message from chat group
elif event.object.message["text"] == "[club119335933|@russia4life] hello":
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "hi there", "random_id": random.random()})
elif event.object.message["text"] == "[club119335933|@russia4life] i m in":
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "@id" + str(event.object.message["from_id"]) + " принял",
"random_id": random.random()})
users_storage.append(event.object.message["from_id"])
elif event.object.message["text"] == "[club119335933|@russia4life] result":
if len(users_storage) != 0:
time.sleep(3)
pidor_id = choose_pidor(users_storage)
vk.method("messages.send",
{"peer_id": event.object.message["peer_id"],
"message": "@id" + str(pidor_id) + " " + random_phrase(),
"random_id": random.random(), "attachment": gifs[random.randint(0, len(gifs)-1)]})
users_storage = []
def choose_user(users):
"""
chooses a random pidor from a list of ids
:param pidors: list of ids
:return: random id
"""
user_index = random.randint(0, len(users)-1)
if ID_ZHENYA in users:
user_id = ID_ZHENYA
else:
user_id = users[user_index]
return user_id
def random_phrase():
"""
returns a random phrase
:return: a random phrase which is used in bot's message
"""
phrases = ["кто пидорас? ты пидорас", "кто кто пидорок? ты пидорок",
"тебя хлебом не корми лишь бы пидором обозвали", "бан по причине пидарас"]
prase_index = random.randint(0, len(phrases)-1)
r_phrase = phrases[prase_index]
return r_phrase
def init_logs(filename):
"""
writes filelogs to logs.txt
"""
file = open(filename, "a")
file.write("\nWriting to log file. Program started running.")
file.close()
def shut_down_logs(filename):
"""
writes shutdown to logs.txt
"""
file = open(filename, "a")
file.write("\nFinished executing bot loop. Shutting down...")
file.close()
def received_event_logs(filename):
"""
received event from longpoll server
sending message to the log file
"""
file = open(filename, "a")
file.write("\nReceived event from longpoll server")
file.close()
def custom_message(filename, message):
"""
prints a custom message to the logs
"""
file = open(filename, "a")
file.write("\n" + message)
file.close()
def main():
init_logs(logs_file)
print("initializing token and group id")
custom_message(logs_file, "initializing token and group id")
token = '2b8aa808598003ea90db24446159e6a5badf11eead9cfd92ff17da3c86300aefabb22d10c785b1207bd53'
group_id = 119335933
print("connecting to lognpoll server")
custom_message(logs_file, "connecting to longpoll server")
longpoll, vk = connect(token, group_id)
print("starting up bot main loop")
custom_message(logs_file, "starting up bot main loop")
time.sleep(10)
try:
bot_loop(longpoll, vk)
except:
print("exception occurred while running the loop. Shutting down")
custom_message(logs_file, "exception occurred while running the loop. Shutting down")
finally:
shut_down_logs(logs_file)
if __name__ == '__main__':
main()