将上传的图像附加到松弛状态

时间:2019-02-10 21:37:38

标签: python slack slack-api

我正在尝试:

  1. 使用files.upload将图像上传到松弛状态
  2. 将上传的图片附加到我发送的邮件中

我遇到的问题是图像没有松弛显示,仅显示标题。

Example of what I am seeing in the channel 我正在采取的步骤涉及到通过API调用函数使用files.upload调用:

self.slack_client.api_call(
                    "files.upload",
                    channel="reviews",
                    filename=os.path.basename(m),
                    title=str(x),
                    initial_comment="sampletest",
                    file=io.BytesIO(fileobj.read())
                )

对于每次上传,我都会根据响应创建一个字典,并将其添加到附件列表中:

[{"title": os.path.basename(m).replace(".", ""), "image_url": resp['file']['permalink']}]

最后我要发布我的消息:

self.slack_client.api_call(
        "chat.postMessage",
        channel="reviews",
        parse="full",
        link_names=1,
        as_user=True,
        text=review_message,
        attachments=attachments
    )

我认为我对该应用拥有所有正确的权限:

enter image description here

下面,我附上了所有相关功能和得到的响应的摘要。关于我在这里做错的任何想法?

片段:

        starterbot_id = self.slack_client.api_call("auth.test")["user_id"]


    x = 0
    attachments = []
    for media_type, media_list in self._review_data['media'].iteritems():
        for m in media_list:
            with open(m, 'rb') as fileobj:

                resp = self.slack_client.api_call(
                    "files.upload",
                    channel="reviews",
                    filename=os.path.basename(m),
                    title=str(x),
                    initial_comment="sampletest",
                    file=io.BytesIO(fileobj.read())
                )
                print resp
                from pprint import pprint
                pprint(resp)
                attachments.append({"title": os.path.basename(m).replace(".", ""), "image_url": resp['file']['permalink']})

    resp = self.slack_client.api_call(
        "chat.postMessage",
        channel="reviews",
        parse="full",
        link_names=1,
        as_user=True,
        text=review_message,
        attachments=attachments
    )
    pprint(attachments)

    pprint(resp)

对files.upload的响应:

{u'file': {u'channels': [],
       u'comments_count': 0,
       u'created': 1549833587,
       u'display_as_bot': False,
       u'editable': False,
       u'external_type': u'',
       u'filetype': u'png',
       u'groups': [],
       u'id': u'FG3ACTKN1',
       u'image_exif_rotation': 1,
       u'ims': [],
       u'is_external': False,
       u'is_public': False,
       u'is_starred': False,
       u'mimetype': u'image/png',
       u'mode': u'hosted',
       u'name': u'03_006_01.png',
       u'original_h': 1536,
       u'original_w': 2048,
       u'permalink': u'https://OMITTEDFORSTACKOVERFLOW.slack.com/files/UG33JNPU2/FG3ACTKN1/03_006_01.png',
       u'permalink_public': u'https://slack-files.com/TEJFE6V4K-FG3ACTKN1-88718bbe43',
       u'pretty_type': u'PNG',
       u'public_url_shared': False,
       u'shares': {},
       u'size': 3265584,
       u'thumb_1024': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_1024.png',
       u'thumb_1024_h': 768,
       u'thumb_1024_w': 1024,
       u'thumb_160': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_160.png',
       u'thumb_360': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_360.png',
       u'thumb_360_h': 270,
       u'thumb_360_w': 360,
       u'thumb_480': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_480.png',
       u'thumb_480_h': 360,
       u'thumb_480_w': 480,
       u'thumb_64': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_64.png',
       u'thumb_720': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_720.png',
       u'thumb_720_h': 540,
       u'thumb_720_w': 720,
       u'thumb_80': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_80.png',
       u'thumb_800': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_800.png',
       u'thumb_800_h': 600,
       u'thumb_800_w': 800,
       u'thumb_960': u'https://files.slack.com/files-tmb/TEJFE6V4K-FG3ACTKN1-0a46015bdb/03_006_01_960.png',
       u'thumb_960_h': 720,
       u'thumb_960_w': 960,
       u'timestamp': 1549833587,
       u'title': u'0',
       u'url_private': u'https://files.slack.com/files-pri/TEJFE6V4K-FG3ACTKN1/03_006_01.png',
       u'url_private_download': u'https://files.slack.com/files-pri/TEJFE6V4K-FG3ACTKN1/download/03_006_01.png',
       u'user': u'UG33JNPU2',
       u'username': u''},
       u'headers': {u'Access-Control-Allow-Origin': u'*',
          u'Cache-Control': u'private, no-cache, no-store, must-revalidate',
          u'Connection': u'keep-alive',
          u'Content-Encoding': u'gzip',
          u'Content-Length': u'602',
          u'Content-Type': u'application/json; charset=utf-8',
          u'Date': u'Sun, 10 Feb 2019 21:19:46 GMT',
          u'Expires': u'Mon, 26 Jul 1997 05:00:00 GMT',
          u'Pragma': u'no-cache',
          u'Referrer-Policy': u'no-referrer',
          u'Server': u'Apache',
          u'Strict-Transport-Security': u'max-age=31536000; includeSubDomains; preload',
          u'Vary': u'Accept-Encoding',
          u'Via': u'1.1 3a89ec81413ada8ae1c2005b5973a09f.cloudfront.net (CloudFront)',
          u'X-Accepted-OAuth-Scopes': u'files:write:user,post',
          u'X-Amz-Cf-Id': u'WKO9KvPiAYN5-NyNnDXk0gv_kAy542NRjh_MGsb9eRnh5Wlc1W10MA==',
          u'X-Cache': u'Miss from cloudfront',
          u'X-Content-Type-Options': u'nosniff',
          u'X-OAuth-Scopes': u'identify,bot:basic',
          u'X-Slack-Req-Id': u'fea559df-5a1a-4a80-a0f5-c51f3d941c1e',
          u'X-Via': u'haproxy-www-h4w9',
          u'X-XSS-Protection': u'0'},
          u'ok': True}

回复帖子:

    {u'channel': u'CG2BKQQ9W',
     u'headers': {u'Access-Control-Allow-Origin': u'*',
          u'Cache-Control': u'private, no-cache, no-store, must-revalidate',
          u'Connection': u'keep-alive',
          u'Content-Encoding': u'gzip',
          u'Content-Length': u'337',
          u'Content-Type': u'application/json; charset=utf-8',
          u'Date': u'Sun, 10 Feb 2019 21:19:54 GMT',
          u'Expires': u'Mon, 26 Jul 1997 05:00:00 GMT',
          u'Pragma': u'no-cache',
          u'Referrer-Policy': u'no-referrer',
          u'Server': u'Apache',
          u'Strict-Transport-Security': u'max-age=31536000; includeSubDomains; preload',
          u'Vary': u'Accept-Encoding',
          u'Via': u'1.1 58b224f0fcba4846d5699ecad6c6829f.cloudfront.net (CloudFront)',
          u'X-Accepted-OAuth-Scopes': u'chat:write:user,client',
          u'X-Amz-Cf-Id': 
          u'bluoWf8yzDLBFYTzRUL0SqSyjHl8Er7IT5xD512d01qr5MgsTwbxMw==',
          u'X-Cache': u'Miss from cloudfront',
          u'X-Content-Type-Options': u'nosniff',
          u'X-OAuth-Scopes': u'identify,bot:basic',
          u'X-Slack-Req-Id': u'48ecae4d-f36e-49c9-9fd1-9a0ca4fc09bd',
          u'X-Via': u'haproxy-www-l4qx',
          u'X-XSS-Protection': u'0'},
          u'message': {u'attachments': [{u'fallback': u'03_006_01png',
                            u'id': 1,
                            u'title': u'03_006_01png'}],
          u'bot_id': u'BG33JNPT4',
          u'text': u'OMITTEDFORSTACKOVERFLOW',
          u'ts': u'1549833594.003000',
          u'type': u'message',
          u'user': u'UG33JNPU2'},
          u'ok': True,
          u'ts': u'1549833594.003000'}

0 个答案:

没有答案