Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. from aiogram.contrib.fsm_storage.memory import MemoryStorage
  2. from aiogram import Bot, Dispatcher, types
  3. from aiogram.types import ChatType
  4. from aiogram.dispatcher.filters.state import State, StatesGroup
  5. from aiogram.dispatcher import FSMContext
  6. from aiogram.utils import executor
  7.  
  8. bot = Bot(token=BOT_TOKEN)
  9.  
  10. storage = MemoryStorage()
  11. dp = Dispatcher(bot, storage=storage)
  12.  
  13. class States(StatesGroup):
  14. first = State()
  15. second = State()
  16.  
  17. @dp.message_handler(lambda msg: ChatType.is_group_or_super_group(msg), commands=['start'], state='*')
  18. async def command_start_handler_group(message: types.Message):
  19. markup = types.InlineKeyboardMarkup(row_width=2)
  20. markup.insert(types.InlineKeyboardButton(text='Button 1', callback_data='button1'))
  21. markup.insert(types.InlineKeyboardButton(text='Button 2', callback_data='button2'))
  22. await bot.send_message(message.chat.id, text='Hi there!', reply_markup=markup)
  23.  
  24.  
  25. @dp.callback_query_handler(text='button1', state='*')
  26. async def callback_button1_handler(cb: types.CallbackQuery):
  27. await States.first.set()
  28. new_text = cb.message.text + '\n' + cb.from_user.full_name
  29. await cb.message.edit_text(text=new_text, reply_markup=cb.message.reply_markup)
  30.  
  31.  
  32. @dp.callback_query_handler(text='button2', state=States.first)
  33. async def callback_button2_handler(cb: types.CallbackQuery, state: FSMContext):
  34. print('Get to callback_button2_handler()')
  35. new_text = cb.message.text.replace('\n' + cb.from_user.full_name, '')
  36. await cb.message.edit_text(text=new_text, reply_markup=cb.message.reply_markup)
  37. await state.finish()
  38.  
  39.  
  40. if __name__ == '__main__':
  41. executor.start_polling(dp, skip_updates=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement