D
D
Dmitry2020-07-24 18:45:57
Python
Dmitry, 2020-07-24 18:45:57

How to get username with vkbot_longpoll?

Good afternoon! I want to create a variable that will receive the id (name) of the user who contacted the bot, but I don’t understand how to do it from the word at all.

Tried to do it this way:

from vk_api import VkApi
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

    vk_session = VkApi(token='токен')
    long_poll = VkBotLongPoll(vk_session, 'айди группы')
 
    for event in long_poll.listen():
        if event.type == VkBotEventType.MESSAGE_NEW:
            time.sleep(0.2)
            peer_id = event.obj['peer_id']
            message = event.obj['text'].lower()
            
            if message == 'ку':
                vk.messages.send(
                    user_id=event.user_id,
                    peer_id=peer_id,
                    message='Бонжур!' + full_name,
                    random_id=get_random_id(),
                )

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2020-07-24
Kvili @Quile

def get_name(uid: int) -> str:
    data = vk.method("users.get", {"user_ids": uid})[0]
    return "{} {}".format(data["first_name"], data["last_name"])

full_name = get_name(тут юзер айди)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question