【问题标题】:Save user input after certain message telegram bot在某些消息电报机器人之后保存用户输入
【发布时间】:2023-03-02 21:55:02
【问题描述】:

我正在 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 的想法,但仍然无法实现。我该如何解决这个问题?有任何想法吗?谢谢。

【问题讨论】:

    标签: python telegram-bot user-interaction python-telegram-bot


    【解决方案1】:

    它不是 python 甚至编程相关的问题。它更像是设计问题。但无论如何。 解决方案是为用户保留会话。例如用户发送给你:

    我们的电脑坏了。

    首先,您为该用户创建一个会话(身份应该是用户 ID),然后向他/她发送适当的消息。当用户首先发送下一条消息时,您会查看用户状态并查看他/她是否有会话。如果他/她有会话,您将继续第二步。我开发了一个这样的机器人并使用字典来存储用户会话。但这让一切变得有点复杂。

    【讨论】:

      【解决方案2】:

      您应该将数据保存在缓存或数据库中。

      【讨论】:

        【解决方案3】:

        您可以使用 Forcereply。 收到带有此对象的消息后,Telegram 客户端将向用户显示回复界面(就像用户选择了机器人的消息并点击“回复”一样)。如果您想在不牺牲隐私模式的情况下创建用户友好的分步界面,这将非常有用。 https://core.telegram.org/bots/api#forcereply

        【讨论】:

          【解决方案4】:

          这对你有帮助 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() 
          

          【讨论】:

            【解决方案5】:

            我知道如何解决这个问题。花了 3 年时间才解决。

            只需查看我的代码并制作您自己的代码。谢谢

            @bot.callback_query_handler(func=lambda call: True)
            def callback_inline(call): #your call
                UserID = str(call.message.chat.id)
            
                if call.data == 'PhotoTEXT': 
                   @bot.message_handler(content_types=['text'])
                   def SetTEXTonPHOTO(message):  # THIS FUNCTION
                       sent = bot.send_message(message.chat.id,'send me text')
                       bot.register_next_step_handler(sent, TextONphoto)
            
                       def TextONphoto(message): #Thsi function add text on photo
                          im = Image.open('UserData/Files/photos/not_converted/'+UserID+'.jpg')
                          idraw = ImageDraw.Draw(im)
                          text = message.text
                          font = ImageFont.truetype("arial.ttf", size=18)
                          idraw.text((10, 10), text, font=font)  
                          im.save('UserData/Files/photos/converted/'+UserID+'.jpg')
                          im.show()
            
                SetTEXTonPHOTO(call.message) #just run your function
                with open('UserData/Files/photos/converted/'+UserID+'.jpg', 'rb') as file:
                     bot.send_document(UserID,file)
            

            我想我帮助了你的朋友

            【讨论】:

            • 欢迎来到堆栈溢出,欢迎您回答您的第一个问题。非常感谢!如果您更具体地了解您的代码如何帮助用户解决问题,它将对用户有所帮助。
            猜你喜欢
            • 2021-09-09
            • 2020-04-14
            • 1970-01-01
            • 1970-01-01
            • 2017-03-19
            • 2017-07-17
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多