M
M
MaxConDimWat2021-01-13 13:18:15
Python
MaxConDimWat, 2021-01-13 13:18:15

How to get username VK.API Python?

I tried to create a bot that can address the user by first and last name. This code throws an error. If there is an easier way to get the username, I'd be happy to know it)

def get_name(from_id):
  info = getting_api.users.get(user_ids = from_id)[0]
  full_name = info.get('first_name') + ' ' + info['last_name']
  return full_name

getting_api = authorize.get_api()

for event in longpoll.listen():
  if event.type == VkEventType.MESSAGE_NEW and event.to_me and event.text:
    msg = event.text
                from_id = event.message.get('from_id')
    name = get_name(from_id)
                 
                if msg == 'test':
                       write_message(sender, name)

Error: AttributeError: 'str' object has no attribute 'get'
(Error on line: from_id = event.message.get('from_id'))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WolfInChains, 2021-01-13
@MaxConDimWat

name = get_name(event.user_id)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question