D
D
Daniil Lebedinsky2020-04-18 15:06:48
Python
Daniil Lebedinsky, 2020-04-18 15:06:48

How to track messages from user vk python 3 vk api?

Hello
I can’t figure out how to track a message from a user in a VK bot in Python.
Example:
There is a code:

from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from vk_api.utils import get_random_id
import vk_api
from datetime import datetime

token = "5f4f65......9cabb12"
vk = vk_api.VkApi(token=token)

session_api = vk.get_api()
longpoll = VkLongPoll(vk)

def create_keyboard(response):
    keyboard = VkKeyboard(one_time=True)

    if response.lower() == 'начать':
        keyboard.add_button('Девушку', color=VkKeyboardColor.DEFAULT)
        keyboard.add_button('Парня', color=VkKeyboardColor.DEFAULT)
        keyboard.add_button('Всё равно', color=VkKeyboardColor.DEFAULT)

    keyboard = keyboard.get_keyboard()
    return keyboard

while True:
    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW:
            print('Сообщение пришло в: ' + str(datetime.strftime(datetime.now(), "%H:%M:%S")))
            print('Текст сообщения: ' + str(event.text))
            print('Id пользователя: ' + str(event.user_id))
            response = event.text.lower()
            keyboard = create_keyboard(response)
            user_id = event.user_id

            if event.from_user and not event.from_me:
                if response == 'начать':
                    vk.method('messages.send',
                              {'user_id': event.user_id, 'message': 'Привет! \n'
                                                                    'Я помогу тебе найти пару или просто друзей. \n'
                                                                    'Можно я задам тебе пару вопросов?',
                               'random_id': get_random_id(),
                               'keyboard': keyboard})


                elif response == 'девушку' or response == 'парня' or response == 'всё равно':
                    vk.method('messages.send',
                              {'user_id': event.user_id, 'message': 'Хорошо...\n'
                                                                    'А как тебя зовут?', 'random_id': get_random_id()})

            print('--------------------------------------------')


After choosing Whom to find, the user is asked about his Name.
How to track his entered name?

I would be very grateful for your help :)

PS Don't pay too much attention to the code, I'm just learning ;)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
Timur Pokrovsky, 2020-04-18
@StellandYT

You need to create a user class and declare 3 fields in it: id, name, isNameSelect and create a list of objects of this class.
If a person writes for the first time (that is, a user with this id is not in the list), you need to add him there.
If a person wrote 'girl', 'boy' or 'whatever', you set that person to isNameSelect true. And then you check if the person has isNameSelect true, then set him the name that he wrote

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question