I'm trying to use google apps scripts and slack to automate my work. And I wish to enter some text with the Slack dialog to modify my google spreadsheet with google apps scripts. However, with the below code, I can't open a Dialog via Slack-API's Slash command
. Is my code have some problems?
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);
}}
How about this modification?
Modification points :
JSON.stringify()
fordialog
of the objectdialog
.'contentType': 'application/json',
is not required.Modified script :
Note :
ContentService.createTextOutput()
for above, because the status code cannot be customized by Google Apps Script. When the empty body is not returned, the error occurs.References :
In my environment, I confirmed that this modified script works. But if this didn't work, I'm sorry.