Google apps script 使用Google应用程序脚本并更新松弛状态

Google apps script 使用Google应用程序脚本并更新松弛状态,google-apps-script,slack-api,Google Apps Script,Slack Api,我刚刚开始学习SlackAPI,我想用GoogleApps脚本来设置。以下是我得到的代码(不知何故,我的profile变量有问题: 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 myUser

我刚刚开始学习SlackAPI,我想用GoogleApps脚本来设置。以下是我得到的代码(不知何故,我的profile变量有问题:

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);
}
但是我得到的错误消息是
{ok=false,error=invalid\u form\u data}

我已通过此代码成功更改了状态(仅更改了“方法”和“负载”变量):


因此,如果有人能帮我指出我是如何错误地传递
配置文件的,那就太好了!

你的脚本几乎是正确的。我修改了一点。请检查它。通过这个脚本,我确认“状态文本”可以更改。请包括用户。配置文件:写入范围并再次检索访问令牌

修改脚本:

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);
}

您的脚本几乎正确。我修改了一点。请检查它。通过此脚本,我已确认“状态\文本”可以更改。请包括用户。配置文件:写入作用域并再次检索访问令牌

修改脚本:

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);
}

我也是一名.NET开发人员,因此对于那些想要C#解决方案的人,我将最终解决方案转换为.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);
}
如果你不想要依赖的东西,那么你可以用这个代替:

JObject profile = new JObject()
{
    {"status_text", "" },
    {"status_emoji", "" }
};


我也是一名.NET开发人员,因此对于那些想要C#解决方案的人,我将最终解决方案转换为.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);
}
如果你不想要依赖的东西,那么你可以用这个代替:

JObject profile = new JObject()
{
    {"status_text", "" },
    {"status_emoji", "" }
};


啊-这就是我所缺少的!给未来看到这一点的人一个提示:
var-profile={“status\u-text:“test”};
将允许您将消息设置为
test
,但为了将其设置回空白,必须使用
var-profile={“status\u-text”:““status\u-emoji”:“};
(您不能只使用
var-profile={”状态文本:“};
-它不工作)啊-这就是我所缺少的!给将来看到这一点的人一个提示:
var profile={”状态文本:“test”};
将允许您将消息设置为
test
,但为了将其设置回空白,必须使用
var profile={“状态文本”:“,“状态表情”:“};
(不能只使用
var profile={“status\u text”:“};
-它不起作用)