Python Telegram Bot如何等待用户回答问题并返回

时间:2020-03-16 10:48:35

标签: python telegram telegram-bot python-telegram-bot

上下文:

我正在使用PyTelegramBotAPiPython Telegram Bot

当用户开始对话时,我有一个正在运行的代码。

当用户开始对话时,我需要向他发送第一张图片,并询问他是否看到图片中的内容,该功能需要等待用户输入并返回他是否看到它。

在那之后,我将需要继续循环发送图片,等待答案并对其运行二等分算法。

到目前为止,我已经尝试过:

我尝试使用等待响应的应答标记或带有处理程序的嵌入式键盘,但由于我的代码正在运行而不等待用户输入而被卡住。

代码:

subject: "%{document_type} pendiente" # double-quoted
subject: '%{document_type} pendiente' # single-quoted
subject: >-                           # folded block scalar
  %{document_type} pendiente
subject: |-                           # literal block scalar
  %{document_type} pendiente

我在用户输入上运行的算法是这样的:

'

我希望我能清楚地解释问题,随时提出任何澄清。 如果您知道可以为我指出正确方向的解决方案,请告诉我。

我尝试了类似的问题,但没有得到答案。

2 个答案:

答案 0 :(得分:6)

您应该将用户信息保存在数据库中。基本字段为:

(id, first_name, last_name, username, menu)

什么是菜单?

菜单保留用户的当前状态。当用户向您的漫游器发送消息时,您可以检查数据库以了解用户的当前状态。

因此,如果该用户不存在,则将其添加到menu设置为MainMenuWelcomeMenu或您的情况为PictureMenu的用户表中。

现在,您将拥有一个用于更新功能的侦听器,让我们假设每个菜单都有。

@bot.message_handler(commands=['start', 'help'])

因此,当用户发送start时,您将要检查功能内的用户菜单字段。

@bot.message_handler(commands=['start', 'help'])
def main(message):
    user = fetch_user_from_db(chat_id)
    if user.menu == "PictureMenu":
        if message.photo is Not None:
            photo = message.photo[0].file_id
            photo_file = download_photo_from_telegram(photo)
            do_other_things()
            user.menu = "Picture2Menu";
            user.save();
        else:
            send_message("Please send a photo")
    if user.menu == "Picture2Menu":
        if message.photo is Not None:
            photo = message.photo[0].file_id
            photo_file = download_photo_from_telegram(photo)
            do_other_things()
            user.menu = "Picture3Menu";
            user.save();
        else:
            send_message("Please send a photo")   
    ...

我希望你明白了。

答案 1 :(得分:0)

我找到了答案:

  • 诀窍是使用next_step_handlermessage_handler_function处理以starthelp开头的命令

  • 然后按照@ALi在其答案中的建议,我将把用户输入的答案以及他回答的问题ID保存在词典中,该词典的键是问题,而id是答案。

  • 一旦用户回答了所有问题,我就可以在其答案上运行算法

这是代码中的样子:

user_dict = {}


# Handle '/start' and '/help'
@bot.message_handler(commands=['help', 'start'])
def send_welcome(message):
    # initialise the the bisector and 
    bisector = LandsatBisector(LON, LAT)
    indice = 0
    message = send_current_candidate(bot, message, bisector, indice)
    bot.register_next_step_handler(
        message, partial(
            process_step, indice, bisector))


def process_step(indice, bisector, message):
    # this run a while loop and will that send picture and will stop when the count is reached
    response = message.text
    user = User.create_get_user(message, bisector=bisector)
    if indice < bisector.count - 1:
        indice += 1
        try:
            # get or create
            user.responses[bisector.date] = response # save the response
            message = send_current_candidate(bot, message, bisector, indice)
            bot.register_next_step_handler(
                message, partial(
                    process_step, indice, bisector))
        except Exception as e:
            print(e)
            bot.reply_to(message, 'oooops')
    else:
        culprit = bisect(bisector.count,
                         lambda x: x,
                         partial(
                             tester_function,
                             responses=list(user.responses.values())))
        bisector.index = culprit
        bot.reply_to(message, f"Found! First apparition = {bisector.date}")