首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Aiogram正在等待用户回复

Aiogram正在等待用户回复
EN

Stack Overflow用户
提问于 2021-11-04 21:26:22
回答 1查看 329关注 0票数 0

如何在图片上写下用户回复的等待?它需要像这样:

代码语言:javascript
复制
#!python3
@dp.message_handler(commands["start"]):
async def start(message: types.Message):
    bot.reply("Send me your name")
    name = #Here need func that will await message
EN

回答 1

Stack Overflow用户

发布于 2021-12-01 13:12:38

你应该使用FSM,这是a的一个内置特性。

您的案例示例:

代码语言:javascript
复制
from aiogram import Bot, Dispatcher, types
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from aiogram.dispatcher import FSMContext
from aiogram.dispatcher.filters.state import State, StatesGroup


bot = Bot(token='BOT TOKEN HERE')

# Don't forget to use storage, otherwise answers won't be saved.
# You can find all supported storages here:
# https://github.com/aiogram/aiogram/tree/dev-2.x/aiogram/contrib/fsm_storage
storage = MemoryStorage()
dp = Dispatcher(bot, storage=storage)


class Form(StatesGroup):
    name = State()


@dp.message_handler(commands=['start'])
async def start(message: types.Message):
    """Conversation entrypoint"""

    # Set state
    await Form.name.set()
    await message.reply("Send me your name")


# You can use state='*' if you want to handle all states
@dp.message_handler(state='*', commands=['cancel'])
async def cancel_handler(message: types.Message, state: FSMContext):
    """Allow user to cancel action via /cancel command"""

    current_state = await state.get_state()
    if current_state is None:
        # User is not in any state, ignoring
        return

    # Cancel state and inform user about it
    await state.finish()
    await message.reply('Cancelled.')


@dp.message_handler(state=Form.name)
async def process_name(message: types.Message, state: FSMContext):
    """Process user name"""

    # Finish our conversation
    await state.finish()
    await message.reply(f"Hello, {message.text}")  # <-- Here we get the name

它看起来是这样的:

aiogram FSM的Official documentation相当糟糕,但是有an example可以帮助你发现FSM所拥有的几乎所有东西。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69846020

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档