A
A
asssdas2020-09-14 18:11:22
Python
asssdas, 2020-09-14 18:11:22

How to make a VK bot in Python “work” separately for each user?

The bot collects data and then calculates them according to the formula, and if someone else writes at the time of collection, the data changes.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander, 2020-09-14
@NeiroNx

Well, think about how the bot will understand what someone else wrote? How to identify a user: by nickname or by id. Then figure out how to store data separately, maybe start a database or stupidly in RAM - in dictionaries. There are many solutions.

K
kirillinyakin, 2020-09-14
@kirillinyakin

Well, depending on which library you use, but in general, in general, you need to remember the steps for each user and then look at which step the user

A
afterters, 2020-09-14
@efters

The general solution is to create a data structure about the user with the entered numbers and state, each time a message is received, check the state of the user, it is better to store states as Enum
If you are using pyTelegramBotApi, then you can use bot.register_next_step_handler , official example: https://github.com /eternnoir/pyTelegramBotAPI/blob...

An example of a simple implementation with a state system

from enum import Enum

class UserStates(Enum):
  PARAM_1 = 0
  PARAM_2 = 1

class UserInfo:
  state = UserStates.PARAM_1
  param_1 = 0
  param_2 = 0

users = {}

# обработчик текстового сообщения от пользователя
# userId - любой спосо идентификации пользователя (TG User ID / Chat ID / etc)

if (users.has_key(userId)):
  if (users[user_id].state == UserStates.PARAM_1):
    # нам отправили первое число
    users[user_id].param_1 = # int (сообщение)
    # зпросить второе число
    users[user_id].state = UserStates.PARAM_2

  elif (users[user_id].state == UserStates.PARAM_2):
    # отправили 2е число
    users[user_id].param_2 = # int (сообщение)
    #результат
    result = users[user_id].param_1 + users[user_id].param_2
    # готовы снова принять первый параметр для нового вычисления
    users[user_id].state = UserStates.PARAM_1
else:
  users[user_id] = UserInfo()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question