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

如何为Slack消息JSON中的thumb_url设置自定义图片尺寸?

解决Slack消息中thumb_url自定义尺寸的问题

嘿,我明白你遇到的困扰了——Slack的attachments结构里的thumb_url确实没有直接提供设置尺寸的属性,但我们有两个靠谱的办法来搞定这个需求:

方法1:通过图片URL自带尺寸参数调整

很多图片托管服务(包括你用的维基媒体 Commons)支持在URL中指定图片尺寸,只需要修改URL的格式就能生成指定大小的缩略图。

比如你原来的图片URL是:

https://upload.wikimedia.org/wikipedia/commons/7/76/Slack_Icon.png

可以改成带尺寸参数的格式(以设置宽度为200px为例):

https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Slack_Icon.png/200px-Slack_Icon.png

这样Slack加载的时候就会显示你指定尺寸的图片。修改后的JSON代码如下:

{
  "text":"This will be header",
  "attachments": [
    {
      "thumb_url":"https://upload.wikimedia.org/wikipedia/commons/thumb/7/76/Slack_Icon.png/200px-Slack_Icon.png",
      "fields": [
        {
          "title": "Title",
          "value": "123",
          "short": true
        }
      ],
      "color": "#F35A00"
    }
  ]
}

方法2:改用Slack Blocks Kit(推荐)

attachments是Slack的旧版API方案,官方现在更推荐使用Blocks Kit,它的image block支持直接设置图片的宽度和高度属性,灵活性更高。

你可以把原来的attachments结构替换成blocks,示例代码如下:

{
  "text":"This will be header",
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": "This will be header"
      },
      "accessory": {
        "type": "image",
        "image_url": "https://upload.wikimedia.org/wikipedia/commons/7/76/Slack_Icon.png",
        "alt_text": "Slack Icon",
        "image_width": 150,
        "image_height": 150
      }
    },
    {
      "type": "section",
      "fields": [
        {
          "type": "mrkdwn",
          "title": "Title",
          "value": "123"
        }
      ]
    },
    {
      "type": "divider"
    }
  ]
}

这里的image_width和image_height就是用来设置图片尺寸的属性,你可以根据需求调整数值。

需要注意的是,Blocks Kit的结构和attachments有所不同,你可能需要根据自己的实际需求调整布局,但它能提供更丰富的自定义能力。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:37:34