在 Bot 中获取用户的输入

Getting input from users in Bot

我对使用 python 构建电报机器人有疑问。 如何在我的 python-telegram-bot 中获得用户的输入?

例如,一个字典机器人,我如何从用户那里得到一个词?

我推荐 pyTelegramBotAPI,而不是 python-telegram-bot。 对我来说更容易。

你应该使用这个:

@bot.message_handler(func=lambda message: True)
def echo_message(message):
  cid = message.chat.id
  mid = message.message_id 
  message_text = message.text 
  user_id = message.from_user.id 
  user_name = message.from_user.first_name 

如果需要,您可以将此信息保存在 JSON 中。

我建议看看examples on GitHub

使用 python-telegram-bot v12.0.0b1 你可以这样做:

def do_something(user_input):
    answer = "You have wrote me " + user_input
    return answer

def reply(update, context):
    user_input = update.message.text
    update.message.reply_text(do_something(user_input))

def main():
    updater = Updater("TOKEN", use_context=True)
    dp = updater.dispatcher
    dp.add_handler(MessageHandler(Filters.text, reply))
    updater.start_polling()
    updater.idle()