我正在尝试使用Google Apps脚本和Slack来自动化我的工作。我希望在“松弛”对话框中输入一些文本,以使用Google Apps脚本修改我的Google电子表格。但是,使用下面的代码,我无法通过Slack-API's Slash command
打开对话框。我的代码有问题吗?
function doPost(e){
var params = e.parameter;
var token = params.token;
var text = params.text;
var trigger_id = params.trigger_id;
var slackUrl = ["https://slack.com/api/dialog.open"];
if (token == "[token from slack]"){
var dialog = {
"token": "[OAuth Token]",
"trigger_id":trigger_id,
"dialog":{
"callback_id": "ryde-46e2b0",
"title": "Request a Ride",
"submit_label": "Request",
"elements": [
{
"type": "text",
"label": "Pickup Location",
"name": "loc_origin"
},
{
"type": "text",
"label": "Dropoff Location",
"name": "loc_destination"
}
]
}
};
var options = {
'method' : 'POST',
'contentType': 'application/json',
'payload' : dialog};
UrlFetchApp.fetch(slackUrl, options);
}
else{
var res = {"text":"failed token verification!"}
return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
}}
答案 0 :(得分:2)
此修改如何?
JSON.stringify()
用于对象dialog
的{{1}}。dialog
不是必需的。'contentType': 'application/json',
function doPost(e) {
var params = e.parameter;
var token = params.token;
var text = params.text;
var trigger_id = params.trigger_id;
var slackUrl = "https://slack.com/api/dialog.open";
if (token == "[token from slack]"){ // Please input this.
var dialog = {
"token": "[OAuth Token]", // Please input this.
"trigger_id": trigger_id,
"dialog": JSON.stringify({
"callback_id": "ryde-46e2b0",
"title": "Request a Ride",
"submit_label": "Request",
"elements": [
{
"type": "text",
"label": "Pickup Location",
"name": "loc_origin"
},
{
"type": "text",
"label": "Dropoff Location",
"name": "loc_destination"
}
]
})
}
var options = {
'method' : 'post',
'payload' : dialog,
};
UrlFetchApp.fetch(slackUrl, options);
}
else{
var res = {"text":"failed token verification!"}
return ContentService.createTextOutput(JSON.stringify(res)).setMimeType(ContentService.MimeType.JSON);
}
return ContentService.createTextOutput(); // Important
}
返回上面的空内容,因为状态代码无法通过Google Apps脚本进行自定义。如果未返回空主体,则会发生错误。在我的环境中,我确认此修改后的脚本有效。但是,如果这不起作用,对不起。