在某些消息电报机器人之后保存用户输入

新手上路,请多包涵

我正在 python 上构建一些电报机器人(使用这个框架 pyTelegramBotAPI )。我遇到了用户输入的问题。在某些机器人的消息之后,我需要保存用户输入(可以是任何文本)。例如:

机器人:- 请描述您的问题。

用户:-我们的电脑坏了。

然后我需要将此文本“我们的计算机不工作”保存到某个变量并转到下一步。这是我的代码:

 #!/usr/bin/env python
# -*- coding: utf-8 -*-

import telebot
import constants
from telebot import types

bot = telebot.TeleBot(constants.token)

@bot.message_handler(commands=['start'])
def handle_start(message):
    keyboard = types.InlineKeyboardMarkup()
    callback_button = types.InlineKeyboardButton(text="Help me!", callback_data="start")
    keyboard.add(callback_button)
    bot.send_message(message.chat.id, "Welcome I am helper bot!", reply_markup=keyboard)

@bot.inline_handler(lambda query: len(query.query) > 0)
def query_text(query):
    kb = types.InlineKeyboardMarkup()
    kb.add(types.InlineKeyboardButton(text="Help me!", callback_data="start"))
    results = []
    single_msg = types.InlineQueryResultArticle(
        id="1", title="Press me",
        input_message_content=types.InputTextMessageContent(message_text="Welcome I am helper bot!"),
        reply_markup=kb
    )
    results.append(single_msg)
    bot.answer_inline_query(query.id, results)

@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
    if call.message:
        if call.data == "start":
            bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text="Please describe your problem.")
            #here I need wait for user text response, save it and go to the next step

我有在语句中使用 message_id 的想法,但仍然无法实现。我该如何解决这个问题?有任何想法吗?谢谢你。

原文由 user3151148 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 606
1 个回答

这将帮助你 https://github.com/eternnoir/pyTelegramBotAPI/blob/master/examples/step_example.py

 import telebot
import constants
from telebot import types

bot = telebot.TeleBot(constants.token)

@bot.message_handler(commands=['start'])
def start(message):
  sent = bot.send_message(message.chat.id, 'Please describe your problem.')
  bot.register_next_step_handler(sent, hello)

def hello(message):
    open('problem.txt', 'w').write(message.chat.id + ' | ' + message.text + '||')
    bot.send_message(message.chat.id, 'Thank you!')
    bot.send_message(ADMIN_ID, message.chat.id + ' | ' + message.text)

bot.polling()

原文由 Nematillo Ochilov 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题