Python 电报机器人:如何从内联键盘点击中提取信息?

问题描述 投票:0回答:1

我正在尝试用 python 制作一个电报机器人,工作的第一大块是制作一份调查问卷以从用户那里收集特定信息。第一个问题(关于活动类型)将显示为内联按钮,之后将出现简单的问答式问题。 所以它就像: 第 1 阶段 - 选择活动 第 2 阶段 - 提供全名 第 3 阶段 - 提供电话号码 第 4 阶段 - 查看您在一篇文章中提供的信息

我可以使用 FSM 轻松地以问答方式制作整个内容,但在尝试添加按钮时遇到了麻烦。

据我所知,当用户单击按钮时我的问题就开始了,但是:

  1. 我的 reg_state 类中没有添加任何信息,我在其中存储状态并使用该信息从一种状态移动到另一种状态
  2. 下一步尝试从用户发送的消息中获取信息,但在这种情况下,用户单击按钮,我不知道如何在代码中正确反映该信息。

当我运行它时,在第 1 阶段我会看到按钮并单击它们,但随后我还必须输入答案才能继续第 2 阶段。然后它就会按预期工作。 我想要进行更改以允许以下场景:用户单击一个按钮(第 1 阶段),他在该按钮上的答案将保存为答案,然后机器人询问他或她的全名(第 2 阶段)。

我第一次在这里发帖,如果我不够清楚,抱歉,我很乐意详细说明。

这是我到目前为止的代码:

from aiogram import Bot, Dispatcher, types, executor
from aiogram.types import InlineKeyboardMarkup, InlineKeyboardButton
from aiogram.dispatcher.filters import Command
from aiogram.dispatcher.filters.state import StatesGroup, State
from aiogram.dispatcher import FSMContext
from aiogram.contrib.fsm_storage.memory import MemoryStorage
from API_KEY_1196494 import API_KEY

# making a class for a state to move from one state to another later and tie commands to a current state
class reg_state (StatesGroup):
    activity = State()
    full_name = State()
    phone = State()

# button names content
button1_text = 'Activity 1'
button2_text = 'Activity 2'

# buttons
button1 = InlineKeyboardButton (text = button1_text, callback_data='activity1')
button2 = InlineKeyboardButton (text = button2_text, callback_data='activity2')

#keyboard
keyboard_inline = InlineKeyboardMarkup().add(button1).add(button2)

# answers
enter_your_name_details = 'Enter your full name'
enter_your_phone = "Enter your phone number"

bot = Bot(API_KEY)
storage = MemoryStorage()
dp = Dispatcher(bot, storage = storage)


# job initiated, asking to choose activity
@dp.message_handler(commands=['start'])
async def activity_reg (message: types.Message):
    user_name_greeting = message.from_user.first_name
    await message.answer (text = f"{user_name_greeting}, Which activity do you want to choose?", reply_markup=keyboard_inline)
        

# reading button info
async def inline_buttons_handler(call: types.CallbackQuery):
    match call.data:
        case 'activity1':
            await call.message.answer (f"You chose {button1_text}")
            await bot.answer_callback_query(callback_query_id=call.id)
            await reg_state.activity.set()
        case 'activity2':
            await call.message.answer (f"You chose {button2_text}")
            await bot.answer_callback_query(callback_query_id=call.id)
            await reg_state.activity.set()
    await call.answer()

@dp.callback_query_handler(inline_buttons_handler)

    

# grabbing the info about activity provided by the user and asking for name
@dp.message_handler(state=reg_state.activity)
async def grab_activity (message: types.Message, state: FSMContext):
    await state.update_data(activity = message.text)
    await message.answer (enter_your_name_details)
    await reg_state.full_name.set()

# grabbing the info about the user's name and asking for phone
@dp.message_handler(state=reg_state.full_name)
async def grab_full_name (message: types.Message, state: FSMContext):
    await state.update_data(full_name = message.text)
    await message.answer (enter_your_phone)
    await reg_state.phone.set()

# grabbing user's phone info and wrapping it up
@dp.message_handler (state = reg_state.phone)
async def grab_phone (message: types.Message, state: FSMContext):
    await state.update_data (phone = message.text)
    data_full = await state.get_data()
    await message.answer(f"Activity: {data_full['activity']}'\n"
                         f"Full name: {data_full['full_name']}\n"
                         f"Phone: {data_full['phone']}")
    await state.finish()



if __name__ == '__main__':
    executor.start_polling(dp)

我尝试使用 reg_state.activity.set() 命令。首先,我注意到当我将它放在 Activity_reg 函数中时,它根本不会运行,所以我将它移到了回调查询处理程序中。但它仍然没有保存任何东西。 我尝试将其更改为等于单击的结果,但似乎要么我做错了/不知道正确的命令,要么解决方案是其他的。

python telegram inline fsm aiogram
1个回答
0
投票

您无法通过

从内联键盘按钮获取数据
query = callback_query
query.message.text
© www.soinside.com 2019 - 2024. All rights reserved.