forked from sunDalik/vk-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_bot.py
executable file
·59 lines (49 loc) · 1.55 KB
/
basic_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
import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import random
from config import token, group_id
vk_session = vk_api.VkApi(token=token)
vk = vk_session.get_api()
def send_message(peer, msg, random):
if msg.strip() != "":
vk.messages.send(peer_id=peer, message=msg, random_id=random)
def msg_mode():
while True:
peer = input("Choose peer: ")
print("You can start messaging!\nType /exit to exit mode or /change to change peer.")
while True:
msg = input()
if msg == "/change":
break
elif msg == "/exit":
return
else:
send_message(peer, msg, random.randint(1, 1000000000))
def get_mode():
print("You will now receive message events!")
while True:
longpoll = VkBotLongPoll(vk_session, group_id)
try:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
print(event)
# e = event.object
# print('New message: ' + e.text)
except requests.exceptions.ReadTimeout as timeout:
continue
def main():
while True:
print("\n==Choose mode==")
print("1. Message mode")
print("2. Get mode")
print("exit. Exit")
option = input()
if option == "1":
msg_mode()
elif option == "2":
get_mode()
elif option == "exit":
return 0
else:
print("Invalid mode")
main()