A
A
Alexander Zakharov2021-08-04 04:07:37
Python
Alexander Zakharov, 2021-08-04 04:07:37

How to get user id using vk_api & longpoll in VK conversation, in Python?

Hello.

Yap: Python
Vk_api
Longpoll

In general, we need a function that can get the user id in the conversation (preferably by message).
I am creating a bot on VK and the difficulty is that now it only accepts, that is, if someone writes a "profile" to the bot, then my profile will be displayed. because from_id always matters with my specific id. Here's how to get this id, I've been thinking about it for the second day already ... and I climbed into the documentation, and looked for similar questions ... I think you understood my idea ...

I got something like this:

def get_id(user_id):
    if reseived_message == "id":
        vk.users.get('message.send', {'users_ids': user_id})
        print(user_id)


reseived_message - a variable with the user's message:
here is the part of the code where the function was located:

for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW and event.from_chat and event.message.get('text') !="":

        reseived_message = event.message.get('text'.lower())
        sender = event.chat_id

        if 'ку' in str(reseived_message) or 'привет' in str(reseived_message):
            write_message(sender, "Добрый день" + " " + take_name(from_id=573881719) + "!")


Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
RINAMI, 2021-08-04
@CleRleQ

Oh my god, what did I just see, gentlemen, remember once and for all: VkLongPoll- ONLY for working in LAN, BotsLongPoll - in conversation. Also, if you want to make a profile for each user, then you need a database, (depending on what you want to write in the profile)
An example of the implementation of what you want on BotsLongPoll:

import time
get_name(from_id):  # Имя Фамилия пользователя
    if from_id > 0:
        sender_info = getting_api.users.get(user_ids=from_id)[0]
        full_name = sender_info.get('first_name') + ' ' + sender_info['last_name']
        return full_name
        pass
def profilesender(id, text)
      vk.method('messages.send', {'chat_id': id, 'message': text, 'random_id': 0})
def chatsender(id, text):
      vk.method('messages.send', {'chat_id': id, 'message': text, 'random_id': 0})
for event in longpoll.listen():
    if event.type == VkBotEventType.MESSAGE_NEW:
       if event.from_chat:
          user_id = event.object.message['from_id']
          mes = event.object.message['text']
          if mes == 'профиль':
             vremya = '⏰Текущее время беседы:⏰\n'  + str(time.asctime()) + '\n'
             chat = event.chat_id + '\n' #можете указать ид вашего чата
             user_name = get_name(from_id)
             chatsender(id, 'Вот ваш профиль\n' + '@id' + user_id)
             profilesender(id, 'Имя:' + user_name + '\n' + vremya + chat +)

PS I don’t know what you want to add to the user profile, but this was an example, in general, you need to create a class for this, but if you want to know the user ID, you don’t need to create a function, just write:
event.object.message['from_id']

A
Alexander Zakharov, 2021-08-04
@CleRleQ

Good afternoon. Thank you for your reply. But I still have a couple of questions:
1) what does - [0]

sender_info = getting_api.users.get(user_ids=from_id)[0]

2) and did I understand correctly that:
Here we display the id:
chatsender(id, 'Вот ваш профиль\n' + '@id' + user_id)

and here we get:
def chatsender(id, text):
      vk.method('messages.send', {'chat_id': id, 'message': text, 'random_id': 0})

??
3) and why couldn't we use one function? just called it with different parameters:
chatsender(id, 'Here is your profile\n' + '@id' + user_id)
chatsender(Code with name and time)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question