R
R
Rusiantvso2020-07-26 06:12:44
Python
Rusiantvso, 2020-07-26 06:12:44

How to get a person's name through vkapi?

if textl.lower() == 'кто я?': 
            try:
                vk.messages.send(
                    peer_id=peer_id,
                    message=f"[id{from_id}|{имя человека который написал сообщение}]", #ответ бота
                    random_id=get_random_id(),
                )
            except Exception as e:
                print ("Сообщение не отправленно")

I will be very grateful

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ilkhomjon Matazimov, 2020-07-26
@Rusiantvso

Try debugging to find out the full path to the name.
For example, I do this: ["response"][0]["first_name"]
I hope you take something for yourself from this code:

info = requests.get('https://api.vk.com/method/users.get', params={
        'v': v,
        'name_case': 'Nom',
        'access_token': token,
        'fields': 'photo_max_orig,counters'
    }).json()
if "error" in info:
    print("[red]Неправильный токен[/red]")
    sys.exit()

first_name = info["response"][0]["first_name"]
last_name = info["response"][0]["last_name"]

E
Erik Mikoyan, 2020-07-26
@erik_mikoyan

You use the users.get method with the id you need.
Without an additional request to the API in any way.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question