You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已稳定运行3个月的Slack通知脚本突发400 invalid_blocks错误

问题描述

我编写的通知脚本已经稳定运行超过3个月无异常,但上周开始突然抛出如下错误:

Error   
Exception: Request failed for https://hooks.slack.com returned code 400. Truncated server response: invalid_blocks (use muteHttpExceptions option to examine full response)

以下是相关脚本代码段:

for (let i = 0; i in vistasVencimiento; i++){
    if (vistasVencimiento[i][0] instanceof Date == true){
      let differenceInMs = dateToday - vistasVencimiento[i][0];
      let differenceInDaysRaw = differenceInMs / (1000 * 60 * 60 * 24);

      if(differenceInDaysRaw >= 0 && statusColumn[i] !== "Pendiente_de_despacho"){
        // 加入逾期列表
        result.push(sumariantesVistas[i], causasCaratulas[i], '\n');
      }
    }
  };
  console.log(result);
  if (result === undefined || result.length === 0 ){
    let payload ={
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":bell: *Cuales Vistas estan vencidas?* :bell:"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text":"Aparentemente, ninguna"
            }
        },
    ]
  }
      let headers = {
      'Content-type': 'application/json'
    };
    let options = {
      headers : headers,
      method: 'POST',
      payload: JSON.stringify(payload)
    };
  UrlFetchApp.fetch(url, options);

  }else{
    let result_string = result.join('\n');
    let payload ={
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": ":bell: *Cuales Vistas estan vencidas?* :bell:"
            }
        },
        {
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text":"Estas: "
            }
        },
        { 
            "type": "section",
            "text": {
                "type": "mrkdwn",
                "text": result_string
            }
        },      
    ]
  }
      let headers = {
      'Content-type': 'application/json'
    };
    let options = {
      muteHttpExceptions: false,
      headers : headers,
      method: 'POST',
      payload: JSON.stringify(payload)
    };
  UrlFetchApp.fetch(url, options);

  };

已初步定位错误与"text": result_string字段相关:将该字段替换为固定字符串"test"时接口可正常调用,但业务需求是将格式化后的结果字符串推送至Slack工作区频道,目前无法定位问题突发的根因。

补充排查情况

  • 排查1:执行causasCaratulas[i].replace(/"/g, "'")将双引号替换为单引号无法解决问题;其次causasCaratulas包含两列数据,仅加载任意一列时脚本均可正常运行,即使将数组拆分为两个独立列表,同时发送两列数据时依然会抛出上述错误。目前推测错误与同时发送两列数据的逻辑有关,但无法定位具体故障点。
  • 排查2:业务数据结构参考附图,问题触发条件为同时发送案件编号(Case Number)与案件名称(Case Name)字段,疑似与数据中存在的无法识别的特殊符号有关,但暂无法定位具体异常字符。
    数据结构示例
问题根因

报错触发原因是Slack Block Kit的两条强制校验规则,和双引号替换逻辑无关:

  • 单section块mrkdwn文本长度上限为3000字符:单传任意一列数据时文本总长度未达阈值,两列拼接后长度超限就会抛出invalid_blocks错误,这也是脚本稳定运行3个月后突然报错的核心原因——前期数据量未触达长度上限,上周数据累计后突破阈值。
  • Slack mrkdwn语法保留字符未转义:如果案件名称、编号中存在&、<、>三个保留字符,未做转义时会直接被判定为块格式非法。之前推测的特殊字符问题本质就是这三个字符没有处理,替换双引号自然没有效果。
修复方案
  1. 新增Slack mrkdwn转义方法,所有入块的文本先过转义逻辑:
    // Slack mrkdwn保留字符转义
    const escapeSlackText = (text) => {
      return String(text)
        .replace(/&/g, '&amp;')
        .replace(/</g, '&lt;')
        .replace(/>/g, '&gt;');
    }
    
    原数组push逻辑修改为:
    result.push(
      escapeSlackText(sumariantesVistas[i]),
      escapeSlackText(causasCaratulas[i]),
      '\n'
    );
    
  2. 拆分长文本块,避免单块长度超限:拼接完result_string后按2800字符(预留冗余)做切片,拆分为多个section块加入payload,不要把所有内容塞到同一个section里。
  3. 调试阶段将muteHttpExceptions参数设为true,可拿到Slack返回的完整错误详情,无需靠截断响应排查问题。

内容的提问来源于stack exchange,提问作者IFritzler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 15:42:10