java - 在 JAVA 上与 Telegram BOT 进行对话

标签 java python telegram-bot python-telegram-bot

在用 Python 对其进行编程之后,我目前正在开发 Java TelegramBot。 现在我正在努力研究如何使用机器人进行回复。 这是部分源代码:

public void reply (Update update) {

    String text = update.getMessage().getText();
    long chat_id = update.getMessage().getChatId();
    int message_id = update.getMessage().getMessageId();


    if (text.equals("/reply")) {

        SendMessage send = new SendMessage();

        send.setText("Reply to the message that you want to be responsed.")
                .setChatId(chat_id);

        try {
            execute(send);
        } catch (TelegramApiException e) {
            e.printStackTrace();
            System.out.println("Oops");
        }

        if (update.hasMessage() && update.getMessage().hasText() && message_id != update.getMessage().getMessageId()) {

            Message reply = new Message();

            if (reply.hasReplyMarkup()) {
                String response = reply.getText();
                send
                        .setText(response)
                        .setChatId(chat_id);
            }
            try {
                execute(send);
            } catch (TelegramApiException e) {
                e.printStackTrace();
                System.out.println("Oops");
            }
        }
    }
}

问题是,如何在聊天中注册下一条消息?通过简单的消息创建对话。我有一个 json,其中包含与可能的用户输入相匹配的单词。

下面是相同流程的示例,但使用的是 Python。

@bot.message_handler(commands=['conversor'])
def conversor(message):
    chat_id = message.chat.id

    text = ('Puedo convertir las siguientes unidades:\n'
            '1 - m --> cm\n'
            '2 - m --> mm\n'
            '3 - ft --> yardas\n'
            '4 - ft --> in\n'
            '5 - ft --> cm\n'
            '6 - ft --> m\n'

            'Solo respondeme al mensaje el numero de opción, separado del valor a convertir.\n')

    msg = bot.send_message(chat_id, text=text)
    bot.register_next_step_handler(msg, operacion)


def operacion(message):
    chat_id = message.chat.id
    msg = message.text

    answer = str.split(msg)

    option = int(answer[0])
    value = int(answer[1])

    result = functions.bot_conversor(option, value)

    bot.send_message(chat_id, result)

我正在使用这个API:

https://github.com/pengrad/java-telegram-bot-api

谢谢

最佳答案

聊天中的每条消息都有自己的数字 ID。为了轻松处理“ future ”消息,您可以使用 message.chat.id+1,但由于程序在一段时间内不会收到它 - 使用带有必要标志的线程(或者如果它非常简单,那么通常的 sleep 会在预期的时间内接收消息,但其余的代码将不起作用)

关于java - 在 JAVA 上与 Telegram BOT 进行对话,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61544994/

相关文章:

telegram-bot - Telegram Bot 可以向群组发送消息吗?

java - 在 Lucene 中创建索引时出现 AbstractMethodError

java - 流 - ObjectOutputStream 给出 NULL

python - Telegram 机器人 Api/Python : Trying to send voice message via my telegram bot

python - ReplyKeyboardMarkup 对 python 不起作用?

python - 删除 python django 模块中 content_id 和 ip_address 相同的重复项

java - 如何将二维数组的一行或一列传递给java中的函数

java - 没有这样的字段错误: RESOURCE_PREFIX when using Tess4J with Eclipse

python - vscode anacondapython 中 undefined variable url

python - tf.data.Dataset.map() 的输出张量形状有些奇怪