M
M
myhabr032020-08-04 21:30:17
Python
myhabr03, 2020-08-04 21:30:17

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

3 answer(s)
A
Alexander Pikeev, 2020-08-04
@Baryon

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.

S
soremix, 2020-08-04
@SoreMix

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.

A
Andrey, 2020-08-04
@anerev

1. The group id is here 2. The loop needs to be removed 3. replaced with https://github.com/python273/vk_api/blob/master/ex...longpoll = VkBotLongPoll(bot_session, "мой айди")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question