0

I have developed a telegram bot on Python using Aiogram 2.3 that offers user to play "Truth or Dare" game. Bot works pretty good with one user, but when 2+ users start using bot in the same time, global variables, states, localisation becomes a mess, because it works like there is only one instance. And that is true, because I have created only one 'bot' instance, that is used to work in all handlers.

I expect that that OOP will solve the problem, but currently is quiet confused with the exact solution. I did't manage to find suitable code in the internet, so I wanted to ask if you can guide me to some already invented solution on this matter or show me how can I solve that.

The bot is launched from app.py. Here is the code of it:

from aiogram import executor

from loader import dp

import handlers

if __name__ == '__main__':
    executor.start_polling(dp, skip_updates=True)

Here is loader.py:

from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage

from mainUnit.config import API_TOKEN


bot = Bot(token=API_TOKEN)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)

If you need any additional code, I will be happy to provide it.

Fastnlight
  • 922
  • 2
  • 5
  • 16

1 Answers1

0

You can use a database to store and retrieve user data. Use Telegram user_id as an identifier. Each time a new message arrives find user data with its user id and retrieve other data related to the user. I recommend using the sqlite3 database as soon as it is best to fit for small, lightweight, serverless projects.

Megasrx
  • 116
  • 5