4
votes

I'm trying to open a dialog in slack through a google app script after the user presses a button but i'm getting the following error message:

{"ok":false,"error":"invalid_auth","warning":"missing_charset","response_metadata":{"warnings":["missing_charset"]}}

This is my code:

function openDialog (range, triggerId, token) {
    var url = 'https://slack.com/api/dialog.open';
    var dialog = {
        trigger_id: triggerId,
        title: 'Submit a helpdesk ticket',
        callback_id: 'submit-ticket',
        submit_label: 'Submit',
        elements: [
            {
                label: 'Title',
                type: 'text',
                name: 'title',
                value: 'teste',
                hint: '30 second summary of the problem',
            },
            {
                label: 'Description',
                type: 'textarea',
                name: 'description',
                optional: true,
            },
            {
                label: 'Urgency',
                type: 'select',
                name: 'urgency',
                options: [
                    { label: 'Low', value: 'Low' },
                    { label: 'Medium', value: 'Medium' },
                    { label: 'High', value: 'High' },
                ],
            },
        ],
    };

    var options = {
        'method' : 'post',                  
        'contentType': 'application/json', 
        'headers': {
            'Authorization': 'Bearer ' +  token,
        },
        'payload' : JSON.stringify(dialog),
    };

    var urlFetch = UrlFetchApp.fetch(url, options);
    var message = ContentService.createTextOutput(urlFetch).setMimeType(ContentService.MimeType.JSON);

    return message; 
}

Can anyone spot what I'm missing?

Thanks


Here's the doPost() function triggering the openDialog() function it reads a payload from a button in slack with callback_id = "gasolina" and value = "update" :

if (payload.callback_id == "gasolina") {
  var selectedOption = actions.value;
  var operation = payload.callback_id;
  var triggerId = payload.trigger_id;
  var token = payload.token;
  var inputRow = actions.name;
  if (selectedOption == 'update') {
    var keyword = 'no+money';
    var gastoExtra = '';
    var operation = payload.callback_id;
    var gastoExtraRange = actions.name;
    return openDialog (gasRange, triggerId, token); 
1
Based on the error response I'd say something is wrong with the token you passed in the authorization header. Maybe its malformed. - TheAddonDepot
Thanks! Do you have any idea if there's a problem in the code that's not sending the token? I tested the imported values for the token and trigger_id and they are coming correctly. It seems it is not being "posted". - Mário Guidoux

1 Answers

0
votes

I think that although your script is almost correct, a little modification is required. How about this modification for your script? From your question, I'm not sure about your current settings for using dialog.open of Slack. So this modified script supposes that the settings is correct.

Modification points :

  • Reason of missing_charset is due to using JSON.stringify() to the payload.
  • From the document of dialog.open, the payload is token, dialog and trigger_id.
  • token doesn't use for the header.
  • application/json doesn't use for contentType.

When these points are reflected to your script, the modified script is as follows.

Modified script :

function openDialog (range, triggerId, token) {
  var url = 'https://slack.com/api/dialog.open';
  var dialog = {
    title: 'Submit a helpdesk ticket',
    callback_id: 'submit-ticket',
    submit_label: 'Submit',
    elements: [
      {
        label: 'Title',
        type: 'text',
        name: 'title',
        value: 'teste',
        hint: '30 second summary of the problem',
      },
      {
        label: 'Description',
        type: 'textarea',
        name: 'description',
        optional: true,
      },
      {
        label: 'Urgency',
        type: 'select',
        name: 'urgency',
        options: [
          { label: 'Low', value: 'Low' },
          { label: 'Medium', value: 'Medium' },
          { label: 'High', value: 'High' },
        ],
      },
    ],
  };
  var options = {
    method: 'post',
    payload: {
      token: token,
      dialog: JSON.stringify(dialog),
      "trigger_id": triggerId,
    },
  };
  var urlFetch = UrlFetchApp.fetch(url, options);
  var message = ContentService.createTextOutput(urlFetch).setMimeType(ContentService.MimeType.JSON);
  return message; 
}

Note :

  • In my environment, I'm using dialog.open with the request like above script. But if this didn't work, please check the error messages and modify your settings.