Answer the question
In order to leave comments, you need to log in
Bot vk does not work on longpoll, why?
Thanks everyone, I figured it out, here is the working code, maybe someone with a similar problem will come in handy
import vk_api
import time
import requests
import random
from vk_api.bot_longpoll import VkBotLongPoll
from vk_api.bot_longpoll import VkBotEventType
group_id ='id вашей группы'
token = "Ваш токен"
def send_msg( message = None,attachment = None,**args):
vk.messages.send(
peer_id = peer_id,
random_id = get_random_id(),
disable_mentions = 1,
message = message,**args) #Функция отправки сообщения
try:
for event in longpoll.listen():
if event.type == VkBotEventType.MESSAGE_NEW:
vk_message = event.obj['message']
peer_id = vk_message['peer_id']#id места назначения ответа
text = vk_message['text']#Текст сообщения,которое присылает юзер
if text.lower() == '1':
send_msg(message="Test")
except requests.exceptions.ReadTimeout:
time.sleep(3)
Answer the question
In order to leave comments, you need to log in
How does it not work at all? Does nothing or still throws an error? In the first case, check if the bot just ate and was sleeping.
Use code editors, your code glows from errors like a Christmas tree.
1. Define vk and bot_session globally
2. Import randomly
And how can it be that you don't have any errors.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question