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 :
- Use a string to "url" of "UrlFetchApp.fetch(url, params)".
- Use
JSON.stringify()
for dialog
of the object dialog
.
'contentType': 'application/json',
is not required.
Modified script :
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
}
Note :
When there are no exceptions within the dialog submission, your app must respond with 200 OK with an empty body. This will complete the dialog.
- When it uses dialog, it returns the empty body using
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.
- This modified script supposes that your setting for using Slack dialog has already been done.
- If you modified your script, please redeploy Web Apps as a new version. By this, the script of the latest version is reflected to Web Apps.
References :
In my environment, I confirmed that this modified script works. But if this didn't work, I'm sorry.