【问题标题】:How to execute definite handler-function in aiogram (Telegram API)?如何在 aiogram (Telegram API) 中执行明确的处理函数?
【发布时间】:2021-11-07 22:34:29
【问题描述】:

我在 AIOgram 中有一个 Telegram-bot。我在这个机器人中的目标是运行它,如果用户写了秘密短语 - 机器人必须发送秘密消息。

我在 main.py 文件中的代码:

from aiogram.dispatcher.filters import Text
from aiogram.dispatcher import FSMContext

from database import Database
from states import *
from aiogram import Bot, Dispatcher, executor, types

bot = Bot(token="my_token")
dp = Dispatcher(bot)

@dp.message_handler(commands="start")
async def start(message: types.Message):
    keyboard1 = types.InlineKeyboardMarkup()
    links = ["one", "two", "three"]

    for row in links:
        button = types.InlineKeyboardButton(text=row[0])
        keyboard1.add(button)

    await message.answer("Chose the phrase", reply_markup=keyboard1)
    # options - is the next handler function
    dp.register_message_handler(options, state="*")

async def options(message: types.Message):
    if message.text == "Secret phrase":
        keyboard = types.ReplyKeyboardMarkup(one_time_keyboard=True)
        keyboard.add(types.KeyboardButton(text="Secret 1"),
                     types.KeyboardButton(text="Secret 2"),
                     types.KeyboardButton(text="Secret 3"),
                     types.KeyboardButton(text="Main menu"))
        await message.answer("Chose the phrase", reply_markup=keyboard)
        dp.register_message_handler(workingWithLinks, state="*")
    else:
        await message.answer("This command is error, for phrases update call command /update")

async def workingWithLinks(message: types.Message):
    if message.text == "Secret 1":
        await message.answer("This is secret number 1")
        await SecretOne.step_one.set()
    elif message.text == "Secret 2":
        await SecretTwo.step_one.set()
        await message.answer("This is secret 2")
    elif message.text == "Secret 3":
        await SecretThree.step_one.set()
        await message.answer("This is secret 3")
    else:
        await message.answer("This command is error, for phrases update call command /update")

def register_handlers_common(dp: Dispatcher):
    dp.register_message_handler(start, commands="start", state="*")
    dp.register_message_handler(start, commands="update", state="*")
    
if __name__ == "__main__":
    register_handlers_common(dp)
    executor.start_polling(dp, skip_updates=True)

我在 states.py 文件中的代码:

from aiogram.dispatcher.filters.state import State, StatesGroup

class SecretOne(StatesGroup):
    step_one = State()
    step_two = State()
    step_three = State()

class SecretTwo(StatesGroup):
    step_one = State()
    step_two = State()
    step_three = State()

class SecretThree(StatesGroup):
    step_one = State()
    step_two = State()
    step_three = State()

接下来是我的机器人的场景:我单击命令 /start,机器人向我发送消息“选择短语”,其中包含三个内联按钮 - “一”、“二”和“三”。如果用户不单击此按钮之一并键入“秘密短语”-程序链接用户到“选项”处理程序功能。这是字符串,其中链接是:

dp.register_message_handler(options, state="*")

“选项”是处理函数。此链接有效。但是,如果在“选项”中我选择了短语“秘密 1”-workingWithLinks 处理函数上的链接不起作用。

workingWithLinks 处理函数上的链接:

dp.register_message_handler(workingWithLinks, state="*")

我还尝试用this tutorial 中的状态链接下一个处理函数,但这也不起作用。

如何链接 workingWithLinks 处理函数?

【问题讨论】:

    标签: python python-asyncio state-machine py-telegram-bot-api aiogram


    【解决方案1】:

    不要在消息处理程序中注册消息处理程序!

    dp.register_message_handler(options, state="*")
    

    此字符串将在每次消息调用时将处理程序添加到堆栈中。

    回答

    如果你想从用户那里得到一些东西 - 使用状态。 看FSM example

    【讨论】:

      猜你喜欢
      • 2021-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-16
      • 2012-12-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多