Reputation: 81
I am learning Slack, currently I am trying to fetch the profile pic of users and post that in a Slack general Channel.
I tried users.profile.get
api and I am able to get some JSON Response but unclear in posting the image to Channel.
Response received:
{
"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": ""
}
}
I've seen files.upload
api but not sure how to pass the content parameter.
Below is the code used to Send a message to 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();}
After running below code the get the response from 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);
Pasting the Response Received:
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":"[email protected]",
"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}}
I am getting the response now, but please check the encoded URI i am able to send the message but cant post the image. i can manually the url and image is visible now.
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();
Logs
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
Upvotes: 1
Views: 4020
Reputation: 32827
files.upload
would require you to first download the image to your local server and then you can upload it again to Slack.
It's a very powerful API method, but in your case it may be unnecessary since the profile image is already hosted on the Slack cloud.
Instead I would suggest to just include the image URL in the attachment of a normal message post. Since those URLs are public those will work fine.
Example message in JSON for sending with chat.postMessage
etc.:
{
"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"
}
]
}
In case you wonder about the extra backlashes in the URLs from your example. The string is JSON encoded and needs to be decoded first (which I did for the URL).
Here is an outline on how to send messages to users with their profile image to a channel:
users.list
and match based on email addresses).users.get.profile
for each user to get their image URLsMake sure to respect the rate limits for the API calls, e.g. max. 1 message post per second.
I would maybe also add a mention to each user in the message, which you can do by adding <@U1234567>
(with the correct user ID) to the text.
Upvotes: 1