Python Telegram Bot 如何等待用户回答问题 Return 它

Python Telegram Bot how to wait for user answer to a question And Return It

上下文:

我正在使用PyTelegramBotAPi or Python Telegram Bot

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

当用户开始对话时,我需要向他发送第一张图片和一个问题,如果他在图片中看到了什么,该功能需要等待用户输入以及 return 他是否看到或没有。

在那之后,我需要一直循环发送图片,等待答案和运行对分算法。

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

我尝试使用等待响应的回复标记或带有处理程序的内联键盘,但我被卡住了,因为我的代码 运行ning 没有等待用户输入。

代码:

@bot.message_handler(func=lambda msg: msg in ['Yes', 'No'])
@bot.message_handler(commands=['start', 'help'])
def main(message):
    """
    This is my main function
    """
    chat_id = message.chat.id
    try:
        reply_answer = message.reply_to_message.text
    except AttributeError:
        reply_answer = '0'
    # TODO : should wait for the answer asynchnonossly
    def tester(n, reply_answer):
        """
        Displays the current candidate to the user and asks them to
        check if they see wildfire damages.
        """
        print('call......')
        bisector.index = n
        bot.send_photo(
            chat_id=chat_id,
            photo=bisector.image.save_image(),
            caption=f"Did you see it Yes or No {bisector.date}",
            reply_markup=types.ForceReply(selective=True))
        # I SHOUL WAIT FOR THE INPUT HERE AND RETURN THE USER INPUT
        return eval(reply_answer)
    culprit = bisect(bisector.count, lambda x: x, partial(tester, reply_answer=reply_answer) )
    bisector.index = culprit
    bot.send_message(chat_id, f"Found! First apparition = {bisector.date}")


bot.polling(none_stop=True)

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

def bisect(n, mapper, tester):
    """
    Runs a bisection.

    - `n` is the number of elements to be bisected
    - `mapper` is a callable that will transform an integer from "0" to "n"
      into a value that can be tested
    - `tester` returns true if the value is within the "right" range
    """

    if n < 1:
        raise ValueError('Cannot bissect an empty array')

    left = 0
    right = n - 1

    while left + 1 < right:
        mid = int((left + right) / 2)

        val = mapper(mid)
        tester_values = tester(val) # Here is where I am using the ouput from Telegram bot
        if tester_values:
            right = mid
        else:
            left = mid

    return mapper(right)

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

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

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

(id, first_name, last_name, username, menu)

什么是菜单?

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

因此,如果该用户不存在,您可以将其添加到您的用户 table,并将 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")   
    ...

希望你明白了。

我找到了答案:

  • 诀窍是使用 next_step_handlermessage_handler_function 来处理以 starthelp

    [= 开头的命令29=]
  • 然后按照@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}")