Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dmitrykolyadin/ydb-aiogram-storage
Storage implementation for aiogram FSM that utilizes Yandex Database (YDB) as the storage backend
https://github.com/dmitrykolyadin/ydb-aiogram-storage
Last synced: 13 days ago
JSON representation
Storage implementation for aiogram FSM that utilizes Yandex Database (YDB) as the storage backend
- Host: GitHub
- URL: https://github.com/dmitrykolyadin/ydb-aiogram-storage
- Owner: DmitryKolyadin
- License: mit
- Created: 2024-02-28T13:11:42.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2024-02-28T13:21:50.000Z (11 months ago)
- Last Synced: 2024-11-25T07:42:41.642Z (2 months ago)
- Language: Python
- Size: 41 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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:
```bash
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:
```python
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, Stateimport 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](LICENSE) file for details.