Coder Social home page Coder Social logo

bloodielie / state_manager Goto Github PK

View Code? Open in Web Editor NEW
5.0 1.0 0.0 304 KB

bots fsm for people

License: Apache License 2.0

Python 100.00%
asyncio vkwave aiogram fsm python python3 pydantic python-types async vk vk-api telegram-bot telegram-bot-api bot

state_manager's Introduction

StateManager - fsm for people

Convenient FSM implementation for telegram/vk

Russian version


Installation

Supports python3.7+

Library only

pip install state-manager

Library and vkwave

pip install state-manager[vk]

Library and aiogram

pip install state-manager[telegram]

Full installation

pip install state-manager[full]

Examples

VkWave

from vkwave.bots import SimpleLongPollBot
import logging
from state_manager.routes.vkwave.state import VkWaveMainStateRouter
from state_manager.models.state_managers.vkwave import VkWaveStateManager

logging.basicConfig(level=logging.INFO)

bot = SimpleLongPollBot(tokens="your token", group_id=123123,)
main_state = VkWaveMainStateRouter(bot)

@main_state.message_handler()
async def home(event: bot.SimpleBotEvent, state_manager: VkWaveStateManager):
    await event.answer("go to home2")
    await state_manager.set_next_state("home2")

@main_state.message_handler()
async def home2(event: bot.SimpleBotEvent, state_manager: VkWaveStateManager):
    await event.answer("go to home")
    await state_manager.back_to_pre_state()

main_state.install()
bot.run_forever(ignore_errors=True)

Aiogram

import logging
from aiogram import Bot, Dispatcher, executor, types
from state_manager.models.state_managers.aiogram import AiogramStateManager
from state_manager.routes.aiogram.state import AiogramMainStateRouter

logging.basicConfig(level=logging.INFO)

bot = Bot(token='your token')
dp = Dispatcher(bot)
main_state = AiogramMainStateRouter(dp)
main_state.install()

@main_state.message_handler()
async def home(msg: types.Message, state_manager: AiogramStateManager):
    await msg.answer("go to home2")
    await state_manager.set_next_state("home2")

@main_state.message_handler()
async def home2(msg: types.Message, state_manager: AiogramStateManager):
    await msg.answer("go to home")
    await state_manager.set_next_state("home")

executor.start_polling(dp, skip_updates=True)

more examples

Storage

The library currently supports:

  • RedisStorage
  • MemoryStorage

If you do not pass arguments to install, Redis Storage is used, which takes the settings from env.

Settings:

  • storage_dsn, default: "redis://localhost:6379"
  • storage_ssl, default: None
  • storage_db: default: None
  • pool_size: default: 10
  • storage_timeout: default: 5

Filters

The library supports out of the box:

  • text_filter
  • text_contains_filter
  • regex_filter

You can also write your filters. aiogram, vkwave

TODO

  • Middleware
  • SyncEvent
  • VkBottle support

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.