【问题标题】:How to answer on replyMarkup using Telegram java TdApi?如何使用 Telegram java TdApi 回答 replyMarkup?
【发布时间】:2019-07-12 02:40:14
【问题描述】:

我正在使用来自https://github.com/tdlib/td/tree/master/example/java 的代码在 java 上创建一个电报客户端 我需要从我的应用程序与电报机器人通信。 Bot 发送带有附加 InlineKeyboardButtons 的消息。如果我点击了按钮,我该如何发送这些消息的答案?

TdApi 有“AnswerInlineQuery”类,但我不明白如何为我的代码调整它以及我应该在哪里使用参数来制作这个答案对象。 我尝试使用带有“replyToMessageId”参数的“sendMessage”函数进行回复。

这是接收到的消息的结构

UpdateNewMessage { message = Message {
    id = 969932800
    senderUserId = 0
    chatId = -1001418532179
    sendingState = null
    isOutgoing = false
    canBeEdited = false
    canBeForwarded = true
    canBeDeletedOnlyForSelf = false
    canBeDeletedForAllUsers = false
    isChannelPost = true
    containsUnreadMention = false
    date = 1562509621
    editDate = 0
    forwardInfo = null
    replyToMessageId = 0
    ttl = 0
    ttlExpiresIn = 0.000000
    viaBotUserId = 0
    authorSignature = ""
    views = 1
    mediaAlbumId = 0
    content = MessageText {
      text = FormattedText {
        text = "опрос"
        entities = Array[0] {
        }
      }
      webPage = null
    }
    replyMarkup = ReplyMarkupInlineKeyboard {
      rows = Array[1] {
        Array[3] {
          InlineKeyboardButton {
            text = "1"
            type = InlineKeyboardButtonTypeCallback {
              data = bytes [15] { 73 65 6E 64 5F 72 65 61 63 74 69 6F 6E 5F 30 }
            }
          }
          InlineKeyboardButton {
            text = "2"
            type = InlineKeyboardButtonTypeCallback {
              data = bytes [15] { 73 65 6E 64 5F 72 65 61 63 74 69 6F 6E 5F 31 }
            }
          }
          InlineKeyboardButton {
            text = "3"
            type = InlineKeyboardButtonTypeCallback {
              data = bytes [15] { 73 65 6E 64 5F 72 65 61 63 74 69 6F 6E 5F 32 }
            }
          }
        }
      }
    }
  }
}

我在这里收到消息

        @Override
        public void onResult(TdApi.Object object) {
            switch (object.getConstructor()) {
                case TdApi.UpdateNewMessage.CONSTRUCTOR:
                    TdApi.UpdateNewMessage updateNewMessage = (TdApi.UpdateNewMessage) object;
                    TdApi.ReplyMarkupInlineKeyboard buttons = (TdApi.ReplyMarkupInlineKeyboard) updateNewMessage.message.replyMarkup;
                    if (buttons.rows[0][0].text.compareTo("1") == 0) {
                        System.out.println("found button");
                        TdApi.InlineKeyboardButton btn = buttons.rows[0][0];
                        TdApi.InlineKeyboardButtonTypeCallback resp = 
    (TdApi.InlineKeyboardButtonTypeCallback) btn.type;
                    }
                    // TdApi.MessageText messageText = 
    (TdApi.MessageText)updateNewMessage.message.content;
                    // System.out.println(messageText.text.text);
                    break;

我应该使用哪个函数和什么参数来发送回复?

【问题讨论】:

    标签: java api telegram


    【解决方案1】:

    GetCallbackQueryAnswer 方法可以向传递CallbackQueryPayloadData 的机器人发送回调查询,并将按钮中的数据作为有效负载。 我对这个方法的使用:

    private static void replyMessage( long chatId, long messageId, byte[] data) {
        TdApi.CallbackQueryPayloadData payloadData = new TdApi.CallbackQueryPayloadData(data);
        TdApi.GetCallbackQueryAnswer answer = new TdApi.GetCallbackQueryAnswer(chatId,messageId, payloadData);
        client.send(answer, defaultHandler);
    }
    

    其中data 是来自InlineKeyboardButtonTypeCallback 的字节

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-27
      • 2019-04-27
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多