V
V
Vadim_Gornik2020-07-15 08:12:57
Python
Vadim_Gornik, 2020-07-15 08:12:57

Why is the VK bot not responding to PM?

The bot responds to the conversation, but does not respond to the PM, what's the problem? Help me please.

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.utils import get_random_id
token = 'токен'

vk_session = vk_api.VkApi(token = token)
session_api = vk_session.get_api()
longpoll = VkLongPoll(vk_session)

def sender(id, text):
  vk_session.method('messages.send', {'user_id': id, 'message' : text, 'random_id' : get_random_id()})
def sender_chat(peer_id, text):
  vk_session.method('messages.send', {'chat_id': peer_id, 'message' : text, 'random_id' : get_random_id()})


for event in longpoll.listen():
  if event.type == VkEventType.MESSAGE_NEW:
    if event.from_user:

      msg = event.text.lower()
      id = event.user_id
            
      if msg == 'бот лох':
        sender(id, 'bruh')
      if msg == 'тест':
        sender(id, 'и чё ты тестировать собрался?')
for event in longpoll.listen():
  if event.type == VkEventType.MESSAGE_NEW:
    if event.from_chat:

      msg = event.text.lower()
      peer_id = event.chat_id
            
      if msg == 'бот лох':
        sender_chat(chat_id, 'bruh')
      if msg == 'тест':
        sender_chat(chat_id, 'и чё ты тестировать собрался?')

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
WolfInChains, 2020-07-15
@Vadim_Gornik

Why do you listen to the log poll twice
Remove these 2 lines before listening to the long poll from the chat

for event in longpoll.listen():
  if event.type == VkEventType.MESSAGE_NEW:

U
User Unknown, 2020-07-15
@aveBHS

import vk_api
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.utils import get_random_id
token = 'токен'

vk_session = vk_api.VkApi(token = token)
session_api = vk_session.get_api()
longpoll = VkLongPoll(vk_session)

def sender(id, text):
  vk_session.method('messages.send', {'user_id': id, 'message' : text, 'random_id' : get_random_id()})
def sender_chat(peer_id, text):
  vk_session.method('messages.send', {'chat_id': peer_id, 'message' : text, 'random_id' : get_random_id()})


for event in longpoll.listen():
  if event.type == VkEventType.MESSAGE_NEW:
    if event.from_user:

      msg = event.text.lower()
      id = event.user_id
            
      if msg == 'бот лох':
        sender(id, 'bruh')
      if msg == 'тест':
        sender(id, 'и чё ты тестировать собрался?')

    elif event.from_chat:

      msg = event.text.lower()
      peer_id = event.chat_id
            
      if msg == 'бот лох':
        sender_chat(chat_id, 'bruh')
      if msg == 'тест':
        sender_chat(chat_id, 'и чё ты тестировать собрался?')

A
Alex, 2020-07-15
@libalex

Perhaps the point is that


Starting February 15, 2019, the default messages section methods will only be callable with a community access key.
If you plan to start developing the messenger, after February 15, 2019, you will need to get test access in Support, which implies the operation of the methods of the Messages section with the administrator keys of your Standalone application.
To get full access to the custom Messages API, the application will need to pass moderation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question