Skip to main content

FSM Storage for YDB (Yandex Database)

Project description

aiogram YDB Storage

aiogram_ydb_storage is a storage implementation for aiogram FSM that utilizes Yandex Database (YDB) as the storage backend. This allows you to persist state data for state machines used in your Telegram bots.

Installation

You can install aiogram_ydb_storage via pip:

pip install aiogram_ydb_storage

Usage

To use aiogram_ydb_storage, you need to first set up your YDB database and obtain necessary credentials. Then, you can use it in conjunction with aiogram as follows:

import asyncio
from aiogram import Bot, Dispatcher, types
from aiogram.filters import CommandStart
from aiogram.types import Message
from aiogram.fsm.context import FSMContext
from aiogram.fsm.state import StatesGroup, State

import ydb
from aiogram_ydb_storage import YDBStorage

# Configure YDB driver
driver_config = ydb.DriverConfig(
    "grpcs://...",  # YDB endpoint
    "...",          # Database name
    credentials=ydb.credentials_from_env_variables(),  # Use YDB credentials from environment
)

# Initialize YDBStorage
my_storage = YDBStorage(driver_config=driver_config)

# Initialize aiogram Bot and Dispatcher
dp = Dispatcher(storage=my_storage)
bot = Bot("token")

# Define your states
class MyStates(StatesGroup):
    test1 = State()
    test2 = State()

# Handlers
@dp.message(CommandStart())
async def command_start_handler(message: Message, state: FSMContext) -> None:
    await state.set_state(MyStates.test1)
    await message.answer("Set state test1")

@dp.message(MyStates.test1)
async def handler(message: types.Message, state: FSMContext) -> None:
    await message.answer("state test1. Set state test2")
    await state.set_state(MyStates.test2)

@dp.message()
async def echo_handler(message: types.Message, state: FSMContext) -> None:
    await message.answer("not state test1. Set state test1")
    await state.set_state(MyStates.test1)

# Main function
async def main() -> None:
    await dp.start_polling(bot)

# Run the main function
if __name__ == "__main__":
    asyncio.run(main())

Contributing

Contributions are welcome! Please feel free to submit issues or pull requests.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiogram_ydb_storage-0.1.0.tar.gz (4.3 kB view hashes)

Uploaded Source

Built Distribution

aiogram_ydb_storage-0.1.0-py3-none-any.whl (5.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page