【问题标题】:How to share uploaded image in a channel?如何在频道中分享上传的图片?
【发布时间】:2019-04-01 12:50:58
【问题描述】:

我使用 https://slack.com/api/files.upload 将图像上传到 slack 未在请求中指定 channels 字段。 我可以在网络界面中看到图像。将来某个时间点在某个频道中分享上传的图片的 api 调用应该是什么?

我尝试上传一张图片,来自 slack 的回复是:

 {
    "ok": true,
    "file": {
        "id": "FHJ9QTX1V",
        "created": 1554115093,
        "timestamp": 1554115093,
        "name": "scaled_IMG-20190324-WA0002.jpg",
        "title": "scaled IMG-20190324-WA0002",
        "mimetype": "image/jpeg",
        "filetype": "jpg",
        "pretty_type": "JPEG",
        "user": "UGRR6FCF7",
        "editable": false,
        "size": 217356,
        "mode": "hosted",
        "is_external": false,
        "external_type": "",
        "is_public": false,
        "public_url_shared": false,
        "display_as_bot": false,
        "username": "",
        "url_private": "https://files.slack.com/files-pri/TGQU3SCHF-FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
        "url_private_download": "https://files.slack.com/files-pri/TGQU3SCHF-FHJ9QTX1V/download/scaled_img-20190324-wa0002.jpg",
        "thumb_64": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_64.jpg",
        "thumb_80": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_80.jpg",
        "thumb_360": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_360.jpg",
        "thumb_360_w": 360,
        "thumb_360_h": 270,
        "thumb_480": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_480.jpg",
        "thumb_480_w": 480,
        "thumb_480_h": 360,
        "thumb_160": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_160.jpg",
        "thumb_720": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_720.jpg",
        "thumb_720_w": 720,
        "thumb_720_h": 540,
        "thumb_800": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_800.jpg",
        "thumb_800_w": 800,
        "thumb_800_h": 600,
        "thumb_960": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_960.jpg",
        "thumb_960_w": 960,
        "thumb_960_h": 720,
        "thumb_1024": "https://files.slack.com/files-tmb/TGQU3SCHF-FHJ9QTX1V-fa34003fce/scaled_img-20190324-wa0002_1024.jpg",
        "thumb_1024_w": 1024,
        "thumb_1024_h": 768,
        "image_exif_rotation": 1,
        "original_w": 1040,
        "original_h": 780,
        "permalink": "https://autolainen.slack.com/files/UGRR6FCF7/FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
        "permalink_public": "https://slack-files.com/TGQU3SCHF-FHJ9QTX1V-3366c52c9c",
        "comments_count": 0,
        "is_starred": false,
        "shares": {},
        "channels": [],
        "groups": [],
        "ims": [],
        "has_rich_preview": false
    }
}

然后尝试使用/api/chat.postMessage分享图片:

{
    "channel": "CH68ZSHFA",
    "text": "test",
    "blocks": [
        {
            "type": "section",
            "text": {
                "type": "plain_text",
                "text": "test"
            }
        },
        {
            "type": "image",
            "image_url": "https://autolainen.slack.com/files/UGRR6FCF7/FHJ9QTX1V/scaled_img-20190324-wa0002.jpg",
            "alt_text": "attachment"
        }
    ],
    "as_user": false,
    "username": "Client name"
}

我使用了来自文件描述的url_privateurl_private_downloadpermalinkpermalink_public 字段的 url,但得到了相同的响应:

{
    "ok": false,
    "error": "invalid_blocks",
    "response_metadata": {
        "messages": [
            "[ERROR] downloading image failed [json-pointer:/1/image_url]"
        ]
    }
}

【问题讨论】:

    标签: slack-api


    【解决方案1】:

    不幸的是,没有官方 API 方法可以在升级后在您的工作区上共享文件。因此,如果您在最初的 files.upload 请求中未包含频道,则以后无法通过官方方式共享该文件。

    但是有一个名为files.share 的非官方 API 方法,它具有该功能。它可以完美运行,但您需要一个旧令牌才能使用它,因此这可能不是您的解决方案。

    在 Slack 上使用图像的另一种方法是将其包含在消息中(正如您在代码示例中所尝试的那样)。从技术上讲,这与在 Slack 上共享文件不同,因为它仅适用于图像并且为用户提供有限的功能。

    它会起作用,但前提是您的图像 URL 是公开的,因为 chat.postMessage 仅适用于图像的公共 URL,并且 Slack 上的文件默认是私有的(这意味着您需要在任何请求中提供授权才能访问该来自 Slack 外部的文件)。

    要获取文件的公共 URL,您可以在上传文件后调用 API 方法 files.sharedPublicURL。您将获得一个公共 URL 作为响应,然后您可以使用该 URL 将该图像包含在您的消息中。

    【讨论】:

    • 感谢您提供这么好的答案!我在想我没有正确阅读 Slack api 文档。我是否正确理解,如果我使用files.sharedPublicURL 方法,互联网上的任何人都可以通过公共 url 访问该文件?如果是这样,那对我来说不是一个选择。
    猜你喜欢
    • 2017-02-20
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多