如何将多个 Slack 块合并为一个? 伪代码:

问题描述

我在 Google 的 Apps Script 中编写了一个脚本,该脚本将来自 Google 表格的数据作为消息发送到 Slack,并以块格式进行格式化。

现在我希望消息长度可变,具体取决于我的电子表格中的行数,我只想发送 1 条消息以防止垃圾邮件频道同时包含数十条消息。

我的第一直觉是这应该是可能的,但简单地组合 2 个(或更多)看起来像下面的变量是行不通的。我还尝试将消息拆分成更小的部分,以便稍后将其合并,但这也不起作用。

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

有没有办法将多个块组合成一条消息,或者甚至可以在制作块之前处理数据,使其具有可变长度?

解决方法

因为 OP 批准了这个想法,所以在此处添加作为答案。

基本上,在一个块中使用唯一的分隔符附加文本,然后将它们拆分。理论上,这可行。这是临时解决方案,因为我不熟悉松弛块套件。

对于不同的类型,另一种临时解决方案是操作字符串,以便您可以在那里处理多种类型。

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

并在另一侧正确处理它们。

伪代码:

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