0

I wrote a script in Google's Apps Script that sends data from Google Sheets as a message to Slack formatted in blocks.

Now I want the message to be of variable length depending on how many lines I have in my spreadsheet and I only want to send 1 message to prevent spamming channels with dozens of messages at the same time.

My first instinct was that this should be possible but simply combining 2 (or more) variables that look like they do below does not work. I have also tried splitting the message in even smaller parts to combine that later but that didn't work either.

var message = {
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "This is a plain text section block.",
                "emoji": true
            }
        },
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "This is a plain text section block.",
                "emoji": true
            }
        }
    ]
}

Is there any way to combine multiple blocks into one message or maybe even a way to process the data before making the blocks so that it can be of a variable length?

Pim
  • 1
  • Have you tried concatenating multiple texts into one block using a unique delimiter and then splitting them there later? I'm unfamiliar with slack block kit but if you tried split and combine them later based on your post, I assume the opposite could also be done. – NightEye Jun 07 '21 at 19:13
  • 1
    I could use \n as a delimiter to fit multiple lines in a single textblock. That is a really smart idea that would at least work as a temporary solution. It still doesn't allow me to add multiple blocks of different types together for example. – Pim Jun 07 '21 at 19:46
  • That's what I can think as of now as I am not able to test that personally. but in theory, that should be a temporary solution that should work. – NightEye Jun 07 '21 at 20:13
  • kindly see the updated answer below if it would work for your different types. – NightEye Jun 07 '21 at 20:41

1 Answers1

0

Adding here as an answer since OP approved of the idea.

Basically, appending the text using a unique delimiter in one block then splitting them afterwards. Theoretically, that could work. This is temporary solution as I'm not familiar with the slack block kit.

For different types, another temporary solution would be manipulating the string so you could process multiple types there.

{
    "type": "section",
    "text": {
        "type": "plain_text",
        "text": "plain_text\n
                 This is a plain text section block.\n
                 type2\n
                 text2\n
                 type3\n
                 text3",
        "emoji": true
    }
}

and process them properly on the other side.

Psuedocode:

types = [];
texts = [];
data = text.split("\n");
data.forEach(function (string, index){
    // even indices -> types
    if(index % 2 == 0) 
        types.append(string);
    // odd indices -> texts
    else 
        texts.append(string);
});
// types should now contain all type in array form
// texts should now contain all text in array form
NightEye
  • 10,634
  • 2
  • 5
  • 24