Skip to content

Instantly share code, notes, and snippets.

@Tishka17
Last active February 20, 2022 18:01
Show Gist options
  • Select an option

  • Save Tishka17/ab60590ec20b0c475d9cd237aff7a2e4 to your computer and use it in GitHub Desktop.

Select an option

Save Tishka17/ab60590ec20b0c475d9cd237aff7a2e4 to your computer and use it in GitHub Desktop.
import asyncio
import logging
from typing import Dict
from aiogram import Bot, Dispatcher
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher.filters.state import StatesGroup, State
from aiogram.types import Message
from aiogram_dialog import Dialog, DialogManager, DialogRegistry, Window
from aiogram_dialog.widgets.kbd import Radio, Cancel
from aiogram_dialog.widgets.text import Format, Text, Case, Const
API_TOKEN = "PLACE YOUR TOKEN HERE"
class DialogSG(StatesGroup):
rate = State()
class Star(Radio):
def __init__(self, checked_text: Text, unchecked_text: Text, id: str, count: int = 5):
super().__init__(
checked_text, unchecked_text, id,
lambda x: x, [str(i) for i in range(count)]
)
def rating(self, manager: DialogManager):
return int(self.get_checked(manager) or -1)
def _is_text_checked(self, data: Dict, case: Case, manager: DialogManager) -> bool:
return self.rating(manager) >= data["pos0"]
dialog = Dialog(
Window(
Format("Вам понравился фильм?"),
Star(
Const("⭐️"), Const("✩"),
id="w_rating",
),
Cancel(),
state=DialogSG.rate,
),
)
async def start(m: Message, dialog_manager: DialogManager):
await dialog_manager.start(DialogSG.greeting, mode=StartMode.RESET_STACK)
async def main():
# real main
logging.basicConfig(level=logging.INFO)
storage = MemoryStorage()
bot = Bot(token=API_TOKEN)
dp = Dispatcher(bot, storage=storage)
dp.register_message_handler(start, text="/start", state="*")
registry = DialogRegistry(dp)
registry.register(dialog)
await dp.start_polling()
if __name__ == '__main__':
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment