V
V
ValeraShprot2020-04-24 21:24:14
Python
ValeraShprot, 2020-04-24 21:24:14

How to add the user's first and last name to the logs?

There is a code:

import vk_api.vk_api
import random
import requests
import re
from vk_api.bot_longpoll import VkBotLongPoll, VkBotEventType

# Настройки
group_id = id # Цифровой ID группы
admins = [1, 2, 3] 
mytoken = "token"

vk = vk_api.VkApi(token = mytoken)
vkapi = vk.get_api()
lp = VkBotLongPoll(vk, group_id, wait = 5)

for event in lp.listen():
  if event.type == VkBotEventType.MESSAGE_NEW:
    username = event.object.from_id
    msg = event.object.text
    print("Пользователь: " + str(username) + "\nСообщение: " + msg)

The bottom line is that from the logs:
User: id, message: text
Make:
User: Name, Surname, message, text.
That is, from id to make the user's first and last name

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dev12345, 2020-04-26
@Dev12345

users.get method, code:

_user = vkapi.users.get(user_ids=event.object.user_id)
print(f'Имя: {_user[0]["first_name"]}, Фамилия: {_user[0]["last_name"]}')

T
Timur Pokrovsky, 2020-04-25
@Makaroshka007

Users.get method
https://vk.com/dev/users.get

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question