1

我正在学习 Slack,目前我正在尝试获取用户的个人资料图片并将其发布在 Slack 通用频道中。

我尝试users.profile.get了 api,并且能够获得一些 JSON 响应,但不清楚将图像发布到频道。

收到的回复:

{
    "ok": true,
    "profile": {
        "title": "",
        "phone": "",
        "skype": "",
        "real_name": "azeez.gaa",
        "real_name_normalized": "azeez.gaa",
        "display_name": "",
        "display_name_normalized": "",
        "fields": null,
        "status_text": "",
        "status_emoji": "",
        "status_expiration": 0,
        "avatar_hash": "gd6ada77a40d",
        "email": "XXXXXXXXXXX",
        "image_24": "https:\/\/secure.gravatar.com\/avatar\/d6ada88a40de8504c6b6068db88266ad.jpg?s=24&d=https%3A%2F%2Fslack.com%2Fdev-cdn%2Fv%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-24.png",
        "image_32": "https:\/\/secure.gravatar.com\/avatar\/d6ada88a40de8504c6b6068db88266ad.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-32.png",
        "image_48": "https:\/\/secure.gravatar.com\/avatar\/d6ada88a40de8504c6b6068db88266ad.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-48.png",
        "image_72": "https:\/\/secure.gravatar.com\/avatar\/d6ada88a40de8504c6b6068db88266ad.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-72.png",
        "image_192": "https:\/\/secure.gravatar.com\/avatar\/d6ada88a40de8504c6b6068db88266ad.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-192.png",
        "image_512": "https:\/\/secure.gravatar.com\/avatar\/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png",
        "status_text_canonical": ""
    }
}

我见过files.uploadapi 但不确定如何传递内容参数。

以下是用于向 Slack 发送消息的代码:

for (i = 0; i < arrayList.length; i++)
{
var rm = new sn_ws.RESTMessageV2();
rm.setHttpMethod('POST');
rm.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
rm.setEndpoint('https://slack.com/api/chat.postMessage');
var bodyString = '';
bodyString += 'token=' + encodeURIComponent('TOKEN');
bodyString += '&channel=' + encodeURIComponent('CKZLVKNFM');
bodyString += '&text=' + encodeURIComponent('HBD ' + arrayList[i]);
rm.setRequestBody(bodyString);
var response1 = rm.execute();
var responsebody = response1.getBody();
var statuscode = response1.getStatusCode();
response1.getAllHeaders();}

运行以下代码后,从 SLACK 获取响应:

var rm = new sn_ws.RESTMessageV2();
     rm.setLogLevel('all');
    rm.setHttpMethod('POST');
    rm.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    rm.setEndpoint('https://slack.com/api/users.info');
    //encodeURIComponent
    var bodyString = '';
    bodyString += 'token=' + encodeURIComponent('xoxb-691106198192-692978264951-MxWbFRMgjb8xo6B9I3yHEu3i');
    bodyString += '&user=' + encodeURIComponent('UL4NBK3BK');
    //bodyString += 
    //bodyString += '&text=' + encodeURIComponent('HBD ' + arrayList[i] + ', ');
    rm.setRequestBody(bodyString);
    var response1 = rm.execute();
    var responsebody = response1.getBody();
    var statuscode = response1.getStatusCode();
    response1.getAllHeaders();
gs.info("Response: " +responsebody);
//var js = JSON.parse(responsebody);
//var jsf = JSON.stringify(responsebody);

var jss = JSON.stringify(JSON.parse(responsebody));
gs.info('Response Parsing: ' +jss);
gs.info('Response Parsing 11' +jss.user.profile.real_name);

粘贴收到的响应:

Response Parsing: 

{"ok":true,
"user":{
"id":"UL4NBK3BK","team_id":"TLB345U5N","name":"azeez.gaa","deleted":false,"color":"9f69e7","real_name":"azeez.gaa","tz":"Asia/Kolkata","tz_label":"India Standard Time","tz_offset":19800,
"profile":{"title":"","phone":"","skype":"","real_name":"azeez.gaa","real_name_normalized":"azeez.gaa","display_name":"","display_name_normalized":"","status_text":"","status_emoji":"","status_expiration":0,"avatar_hash":"gd6ada88a40d","email":"azeez.gaa@gmail.com",
"image_24":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=24&d=https%3A%2F%2Fslack.com%2Fdev-cdn%2Fv%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-24.png",
"image_32":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=32&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-32.png",
"image_48":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=48&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-48.png",
"image_72":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=72&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-72.png",
"image_192":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=192&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-192.png",
"image_512":"https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png","status_text_canonical":"","team":"TLB345U5N"},
"is_admin":true,"is_owner":true,"is_primary_owner":true,"is_restricted":false,"is_ultra_restricted":false,"is_bot":false,"is_app_user":false,"updated":1562821220}}

我现在收到了回复,但请检查我能够发送消息但无法发布图像的编码 URI。我现在可以手动显示 url 和图像。

var rm1 = new sn_ws.RESTMessageV2();
    //rm.setLogLevel('all');
    rm1.setHttpMethod('POST');
    rm1.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    rm1.setEndpoint('https://slack.com/api/chat.postMessage');
    //encodeURIComponent
    var bodyString1 = '';
    bodyString1 += 'token=' + encodeURIComponent('xoxb-691106198192-692978264951-MxWbFRMgjb8xo6B9I3yHEu3i');
    bodyString1 += '&channel=' + encodeURIComponent('CKZLVKNFM');
bodyString1 += '&image_url=' +encodeURIComponent(grimage.user.profile.image_512);
    bodyString1 += '&text=' + encodeURIComponent('Congrats hhhh' );
    rm1.setRequestBody(bodyString1);

    var response11 = rm1.execute();
    var responsebody1 = response11.getBody();
    var statuscode1 = response11.getStatusCode();
    response11.getAllHeaders();

日志

token=xoxb-692206198192-692978264951-MxWbFRMgjb8xo6B9I3yHEu3i&channel=CKZLVKNFM&image_url=https%3A%2F%2Fsecure.gravatar.com%2Favatar%2Fd6ada88a40de8504c6b6068db88266ad.jpg%3Fs%3D512%26d%3Dhttps%253A%252F%252Fa.slack-edge.com%252F27b6e%252Fimg%252Favatars%252Fsmiley_blobs%252Fava_0016-512.png&text=Congrats%20hhhh
4

1 回答 1

1

files.upload将要求您首先将图像下载到本地服务器,然后您可以再次将其上传到 Slack。

这是一个非常强大的 API 方法,但在您的情况下,它可能是不必要的,因为配置文件图像已经托管在 Slack 云上。

相反,我建议只在普通消息帖子的附件中包含图像 URL。由于这些 URL 是公开的,因此它们可以正常工作。

用于发送chat.postMessage等的 JSON 中的示例消息:

{
    "channel": "test",
    "attachments":
    [
        {
            "text": "Profile pic",
            "image_url": "https://secure.gravatar.com/avatar/d6ada88a40de8504c6b6068db88266ad.jpg?s=512&d=https%3A%2F%2Fa.slack-edge.com%2F27b6e%2Fimg%2Favatars%2Fsmiley_blobs%2Fava_0016-512.png"
        }

    ]
}

如果您想知道示例中 URL 中的额外反冲。该字符串是 JSON 编码的,需要先解码(我为 URL 做了解码)。

以下是有关如何向频道发送带有个人资料图片的用户的消息的概述:

  1. 使用他们的 Slack 用户 ID 获取您想要向其发送消息的用户列表(如果您没有 Slack 用户 ID 调用users.list并根据电子邮件地址进行匹配)。
  2. 要求users.get.profile每个用户获取他们的图像 URL
  3. 向每个用户发送消息。将图像 URL 添加为附件(参见我的示例)。

确保遵守 API 调用的速率限制,例如最大值。每秒 1 条消息。

我可能还会在消息中添加对每个用户的提及,您可以通过<@U1234567>在文本中添加(使用正确的用户 ID)来做到这一点。

于 2019-07-17T10:45:26.567 回答