Skip to content

Instantly share code, notes, and snippets.

@rominf
Last active August 24, 2018 05:56
Show Gist options
  • Save rominf/054532d6ebfda5045b23df6795140cdb to your computer and use it in GitHub Desktop.
Save rominf/054532d6ebfda5045b23df6795140cdb to your computer and use it in GitHub Desktop.
import asyncio
from aiogram import Bot, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import Dispatcher
from aiogram.types import ParseMode, InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils import executor
from aiogram.utils.markdown import text, bold
API_TOKEN = ''
loop = asyncio.get_event_loop()
bot = Bot(token=API_TOKEN, loop=loop)
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)
# States
AGE = 'process_age'
NAME = 'process_name'
GENDER = 'process_gender'
@dp.message_handler(commands=['start'])
async def cmd_start(message: types.Message):
state = dp.current_state(chat=message.chat.id, user=message.from_user.id)
await state.set_state(NAME)
inline_keyboard = InlineKeyboardMarkup()
for name in ['Alice', 'Bob', 'Mark']:
inline_button = InlineKeyboardButton(text=name, callback_data=name)
inline_keyboard.add(inline_button)
await message.reply("Hi there! What's your name?", reply_markup=inline_keyboard)
@dp.callback_query_handler(state=NAME)
async def process_name(callback_query):
with dp.current_state(chat=callback_query.message.chat.id, user=callback_query.message.from_user.id) as state:
await state.update_data(name=callback_query.data)
await state.set_state(AGE)
inline_keyboard = InlineKeyboardMarkup()
for age in ['12', '15', 'Putin']:
inline_button = InlineKeyboardButton(text=age, callback_data=age)
inline_keyboard.add(inline_button)
await callback_query.message.reply("How old are you?", reply_markup=inline_keyboard)
@dp.callback_query_handler(state=AGE)
async def process_age(callback_query):
with dp.current_state(chat=callback_query.message.chat.id, user=callback_query.message.from_user.id) as state:
await state.set_state(GENDER)
await state.update_data(age=callback_query.data)
inline_keyboard = InlineKeyboardMarkup()
for sex in ['Male', 'Female', 'Other']:
inline_button = InlineKeyboardButton(text=sex, callback_data=sex)
inline_keyboard.add(inline_button)
await callback_query.message.reply("What is your gender?", reply_markup=inline_keyboard)
@dp.callback_query_handler(state=GENDER)
async def process_gender(callback_query):
state = dp.current_state(chat=callback_query.message.chat.id, user=callback_query.message.from_user.id)
data = await state.get_data()
data['gender'] = callback_query.data
await bot.send_message(callback_query.message.chat.id, text(
text('Hi! Nice to meet you,', bold(data['name'])),
text('Age:', data['age']),
text('Gender:', data['gender']),
sep='\n'), parse_mode=ParseMode.MARKDOWN)
await state.finish()
async def shutdown(dispatcher: Dispatcher):
await dispatcher.storage.close()
await dispatcher.storage.wait_closed()
if __name__ == '__main__':
executor.start_polling(dp, loop=loop, skip_updates=True, on_shutdown=shutdown)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment