Answer the question
In order to leave comments, you need to log in
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)
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) + "!")
Answer the question
In order to leave comments, you need to log in
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 +)
event.object.message['from_id']
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]
chatsender(id, 'Вот ваш профиль\n' + '@id' + user_id)
def chatsender(id, text):
vk.method('messages.send', {'chat_id': id, 'message': text, 'random_id': 0})
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question