我正在学习 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.upload
api 但不确定如何传递内容参数。
以下是用于向 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