Java bolt for slack mapping block json and sending it to slack

Viewed 326

I am trying to see the following message on 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"
            }
        ]
    }]
}

Now on the java side I am using gson to map this to an object and then send it using ctx.say():

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

Now this mapping fails. If I try to use a simpler json it works:

{
          "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\" } }"
            }
          ]
        }

Basically what I'm seeing is that when using ctx.say(), you can either send a String or a List of Blocks:

  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;
    }

How am I supposed to convert the json shown in the slack block kit builder into a java object that can actually be sent using ctx.say()?

0 Answers
Related