N
N
nintendolyub2020-07-02 12:29:36
Python
nintendolyub, 2020-07-02 12:29:36

Why do you write in a conversation and the bot replies in PM?

import vk_api
from vk_api.utils import get_random_id
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import requests
from vk_api.longpoll import VkEventType


vk_session = vk_api.VkApi(token="я чеченец")

vk = vk_session.get_api()
longpoll = VkBotLongPoll(vk_session, "186308067")

for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW:
        if event.obj.text == 'ок':
            if event.from_chat:
                vk.messages.send(
                        user_id=event.obj.from_id,
                        random_id=get_random_id(),
                        chat_id=event.chat_id,
                        message="bruh")
        if event.obj.text == 'кто':
            if event.from_chat:
                vk.messages.send(
                        user_id=event.obj.from_id,
                        random_id=get_random_id(),
                        chat_id=event.chat_id,
                        message="нет")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Makssof, 2020-07-02
@nintendolyub

It is necessary to understand the difference between user_id and peer_id
user_id can also come from a conversation, this will indicate which person specifically wrote. And peer_id is the ID of the conversation. The dialogue can be with a person personally, then it will be user_id === peer_id, and it can be different when it is a conversation.
Always take peer_id, always send messages to peer_id

S
soremix, 2020-07-02
@SoreMix

This is how you address it to the user
user_id=event.obj.from_id
Then pass event.obj.from_id to the peer_id parameter
https://vk.com/dev/messages.send

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question