J
J
Jalal Nasirov2021-06-20 07:36:52
Python
Jalal Nasirov, 2021-06-20 07:36:52

How to make the bot work for each user separately?

import vk_api, json, vk, sqlite3, statistics, json, datetime
from vk_api.longpoll import VkLongPoll, VkEventType
from vk_api.keyboard import VkKeyboard, VkKeyboardColor
from random import choice


db = sqlite3.connect(':memory:') 

sql = db.cursor()

sql.execute('''CREATE TABLE IF NOT EXISTS Users(

id TEXT,

nik TEXT,

age TEXT);

''')
db.commit()

vk_token = 'token'

while True:
  try:
    vk_session = vk_api.VkApi(token = vk_token)
    give = vk_session.get_api()
    longpoll = VkLongPoll(vk_session)
  except:
    pass
  finally:
    break

def event1():
    for event in longpoll.listen():
        if event.type == VkEventType.MESSAGE_NEW:
            if event.to_me:
                
                message = event.text
                
                id = event.user_id
                
                sql.execute("SELECT * FROM users WHERE id = ?", (id,))
                user = sql.fetchone()
                
                id_id = id

                if user == None:# если нету в бд
                    
                    vk_session.method('messages.send', {'peer_id' : id, 'message' : '⚠️ Вы ещё не зарегистрированы\n\nНапишите свой никнейм:', 'random_id' : 0})
                    
                    for event in longpoll.listen():
                        if event.type == VkEventType.MESSAGE_NEW and event.to_me:
                            message = event.text
                            id = event.user_id
                            nik = message
                            vk_session.method('messages.send', {'peer_id' : id, 'message' : '➡️ Напишите свой возраст:', 'random_id' : 0})
                            break

                    for event in longpoll.listen():
                        if event.type == VkEventType.MESSAGE_NEW and event.to_me:
                            message = event.text
                            id = event.user_id
                            age = message
                            user = (id, nik, age)
                            sql.execute("INSERT INTO Users VALUES(?, ?, ?);", user)
                            db.commit()
                            vk_session.method('messages.send', {'peer_id' : id, 'message' : '✅ Регистрация завершена.\nВаша анкета\n'+user[1]+', '+user[2], 'random_id' : 0})
                            break

                elif user != None:# если есть в бд
                    vk_session.method('messages.send', {'peer_id' : id, 'message' : '✅ Вы зарегистрированы', 'random_id' : 0})

event1()

If a user who is not in the database writes a message, he will have to register. When the code expects him to enter text for his nickname/age, another user can write to the bot and he will end up in the same place of registration, although he may already be registered. How to make it so that when the bot is waiting for the user to enter any data, but it works fine for other users?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question