M
M
merchfck2022-02-07 18:44:27
Python
merchfck, 2022-02-07 18:44:27

How to mention a certain person in a conversation through vk api?

Let's say I write to a conversation, then the bot replies, that is, if id = my id, then messages.send
Probably, you need to find out the id of all participants in the conversation

import vk_api
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

token = "тут токен"
vk_session = vk_api.VkApi(token = token)
longpoll = VkBotLongPoll(vk_session, 210290297)

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

      id = event.chat_id
      vk_session.method('messages.send', {'chat_id':id, 'message' : id2, 'random_id' : 0})

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wearant, 2022-02-08
@merchfck

from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType
import vk_api

token = "********"
group_id = "********"

vk_session = vk_api.VkApi(token = token)
longpoll = VkBotLongPoll(vk_session, group_id)
vk = vk_session.get_api()

def main():
    for event in longpoll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat:
            user_id = event.message.from_id
            if "-" in str(user_id):
                pass
            else:
                  chat(event) 
                
def chat(event):             
    user_id = event.message.from_id
    chat_id = event.message.peer_id % 2000000000
    user_info = vk.users.get(user_ids=user_id)
    user_info = user_info[0]
    first_name = user_info["first_name"]
    id = event.chat_id
    vk.messages.send(
      message= (f"@id{user_id} ({first_name}), СООБЩЕНИЕ"),
      chat_id=chat_id, 
      random_id= 0
      )


if __name__ == '__main__':
    main()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question