java bolt for slackMap块json并将其发送到slack

4dbbbstv  于 2021-06-26  发布在  Java
关注(0)|答案(0)|浏览(298)

我试图在slack上看到以下消息:

{
    "blocks": [{
        "type": "actions",
        "elements": [{
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Farmhouse",
                    "emoji": true
                },
                "value": "click_me_123"
            },
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Kin Khao",
                    "emoji": true
                },
                "value": "click_me_123",
                "url": "https://google.com"
            },
            {
                "type": "button",
                "text": {
                    "type": "plain_text",
                    "text": "Ler Ros",
                    "emoji": true
                },
                "value": "click_me_123",
                "url": "https://google.com"
            }
        ]
    }]
}

现在在java方面,我使用gson将它Map到一个对象,然后使用ctx.say()发送它:

List<LayoutBlock> blocks = answers.stream().map(ans -> 
   gson.fromJson(ans.getContent(), LayoutBlock.class)                                 
     ).collect(Collectors.toList());

现在这个Map失败了。如果我尝试使用更简单的json,它可以工作:

{
          "type": "actions",
          "elements": [
            {
              "type": "button",
              "text": {
                "type": "plain_text",
                "text": "Option 1",
                "emoji": true
              },
              "value": "{ \"name\": \"smalltalk.appraisal.thank_you\", \"entities\": { \"reply\": \"test\", \"option\": \"1\" } }"
            },
            {
              "type": "button",
              "text": {
                "type": "plain_text",
                "text": "Option 2",
                "emoji": true
              },
              "value": "{ \"name\": \"smalltalk.appraisal.thank_you\", \"entities\": { \"reply\": \"test\", \"option\": \"2\" } }"
            }
          ]
        }

基本上我看到的是,当使用ctx.say()时,您可以发送字符串或块列表:

default ChatPostMessageResponse say(String text) throws IOException, SlackApiException {
        this.verifyChannelId();
        ChatPostMessageResponse response = this.client().chatPostMessage(ChatPostMessageRequest.builder().text(text).channel(this.getChannelId()).build());
        return response;
    }

    default ChatPostMessageResponse say(List<LayoutBlock> blocks) throws IOException, SlackApiException {
        this.verifyChannelId();
        ChatPostMessageResponse response = this.client().chatPostMessage(ChatPostMessageRequest.builder().blocks(blocks).channel(this.getChannelId()).build());
        return response;
    }

我应该如何将slack block kit builder中显示的json转换成可以使用ctx.say()发送的java对象?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题