Reputation: 2283
I'm just starting to learn the Slack API and I'm wanting to use Google Apps Script to set the custom status. Here's the code I've got (somehow there's something wrong with my profile variable:
function setStatus() {
//https://api.slack.com/docs/presence-and-status
var token = MYTOKEN; //https://api.slack.com/apps
var apiEndpoint = "https://slack.com/api/";
var myUserID = MYUSERID;
var method = "users.profile.set";
var profile = {"status_text": "test"};
var payload = {"token": token, "user" : myUserID, "profile" : profile};
Logger.log(payload);
var completeUrl = apiEndpoint + method;
var jsonData = UrlFetchApp.fetch(completeUrl, {"method" : "post", "payload" : JSON.stringify(payload)});
var object = JSON.parse(jsonData.getContentText());
Logger.log(object);
}
But the error message I'm getting is {ok=false, error=invalid_form_data}
I have successfully changed the presence via this code (just the "method" and "payload" variables have changed):
function setPresence() {
//https://api.slack.com/docs/presence-and-status
var token = MYTOKEN; //https://api.slack.com/apps
var apiEndpoint = "https://slack.com/api/";
var myUserID = MYUSERID;
var method = "users.setPresence";
var payload = {"token": token, "user" : myUserID, "presence" : "auto"}; //Presence can either be "auto" or "away"
Logger.log(payload);
var completeUrl = apiEndpoint + method;
var jsonData = UrlFetchApp.fetch(completeUrl, {"method" : "post", "payload" : JSON.stringify(payload)});
var object = JSON.parse(jsonData.getContentText());
Logger.log(object);
}
So if someone can help point out to me how I'm passing the profile
in wrong, that'd be great!
Upvotes: 0
Views: 1017
Reputation: 2283
I am also a .NET developer so for those people who want a C# solution, I have converted the final solution into .NET:
string apiEndpoint = "https://slack.com/api/";
string method = "users.profile.set";
string completeUrl = apiEndpoint + method;
string token = MYTOKEN; //Put your token here
string userID = MYUSERID; //Put your user id here
JObject profile = new JObject()
{
{"status_text", "" },
{"status_emoji", "" }
};
NameValueCollection payload = new NameValueCollection()
{
{"token", token },
{"user", userID },
{"profile", profile.ToString() }
};
using (WebClient client = new WebClient())
{
var response = client.UploadValues(completeUrl, "POST", payload);
string responseText = (new UTF8Encoding()).GetString(response);
Console.WriteLine(responseText);
}
And if you don't want something that depends on Newtonsoft.Json Then you can substitute this:
JObject profile = new JObject()
{
{"status_text", "" },
{"status_emoji", "" }
};
with
string profile = "{" +
"\"status_text\":\"\"," + /* e.g. "\"status_text\":\"test\","*/
"\"status_emoji\":\"\"" +
"}";
Upvotes: 0
Reputation: 201378
Your script is almost correct. I modified a little. Please check it. By this script, I have confirmed that "status_text" can be changed. And please include users.profile:write to the scope and retrieve access token again.
Modified script :
function setStatus() {
//https://api.slack.com/docs/presence-and-status
var token = MYTOKEN; //https://api.slack.com/apps
var apiEndpoint = "https://slack.com/api/";
var myUserID = MYUSERID;
var method = "users.profile.set";
var profile = {"status_text": "test"};
var payload = {token: token, user: myUserID, profile: JSON.stringify(profile)};
Logger.log(payload);
var completeUrl = apiEndpoint + method;
var jsonData = UrlFetchApp.fetch(completeUrl, {method: "post", payload: payload});
var object = JSON.parse(jsonData.getContentText());
Logger.log(object);
}
Upvotes: 3