通过Slack API或传入Webhook发送消息时,可以使用文件路径代替url作为`image_url`吗?

问题描述:

假设我有以下代码块,希望通过传入的Webhook发送到Slack

Let's assume I have the following block which I want to send via Incoming Webhook to Slack

   {
        "type": "image",
        "title": {
                   "type": "plain_text",
                   "text": "foo bar"
                 },
        "image_url": "https://api.slack.com/img/blocks/example/beagle.png",
        "alt_text": "foo"
    }

但我不想提供http URL作为image_url,而是提供文件路径,因为我要发送的文件与脚本位于同一文件夹中.

but instead of providing a http url as image_url I would like to provide a file path because the file I want to send is in the same folder as my script.

1)这可能吗?我想不会.

1) Is this possible? I guess no.

2)是否可以通过 files.upload 上传图片没有实际显示的第一步是在任何频道上获取URL,然后将其用作image_url?

2) Is it possible to upload an image via files.upload in a first step WITHOUT actually displaying is on any channel just to get the URL which then can be used as image_url?

1)否.如前所述,无法使用文件路径直接在邮件中包含图像.唯一的方法是为图像文件提供公共URL.

1) No. As I mentioned earlier its not possible to use a file path to directly include an image in a message. The only way is to provide a public URL to an image file.

2)是,您可以使用上载到Slack的 files.upload 您的讯息.而且您不必在任何频道*享它们.

2) Yes, you can use files.upload you uploaded to Slack in your messages. And you do not have to share them in any channel do it.

基本方法是:

  • 使用 files.upload 将图像上传到Slack.而且,如果您不使用该API调用提供任何通道,则该文件将不会在任何通道*享.您将获得包含文件对象.上传的图片文件的文件ID.

  • Upload your image to Slack with files.upload. And if you do not provide any channels with that API call the file will not be shared in any channel. You will get the file object incl. the file ID of the uploaded image file.

调用 files.sharedPublicURL ,以通过提供以下内容将该文件公开文件ID

Call files.sharedPublicURL to make that file public by providing the file ID

您的图像文件现已公开,您可以使用文件对象的permalink_public在邮件中包含该图像.但是,您仍然需要根据从permalink_public获得的URL构造该图像的直接URL.查看这篇文章有关如何获取图片的直接URL.

Your image file is now public and you can use the permalink_public of the file object to include that image in message. However, you still need to construct the direct URL for that image from the URL you get from permalink_public. Check out this post on how to get the direct URL for the image.