6

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); 
2
  • Based on the error response I'd say something is wrong with the token you passed in the authorization header. Maybe its malformed. Jun 2, 2018 at 17:13
  • 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". Jun 2, 2018 at 21:11

2 Answers 2

0

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.
18
  • 1
    Thank you for your help. However, i keep receiving an error. This time, it reads: {"ok":false,"error":"invalid_auth"} Jun 3, 2018 at 11:18
  • @Mário Guidoux I'm really sorry for the inconvenience. It was found that your error was changed. From the document of dialog.open, invalid_auth means that Some aspect of authentication cannot be validated. Either the provided token is invalid or the request originates from an IP address disallowed from making the request.. Could you please confirm about it again.
    – Tanaike
    Jun 3, 2018 at 11:45
  • @Mário Guidoux Also I have already confirmed that this modified script works fine under the correct settings. But as I said, I'm not sure about your settings for using dialog.open from your question. Can I ask you about the detail information for your settings? If you can do, please add the detail flow you did by updating your question. By this, I can think of the solution of your error. I would like to solve your issue.
    – Tanaike
    Jun 3, 2018 at 11:45
  • thank you for your continued support! Find below the doPost() function that triggers openDialog(): if (payload.callback_id == "gasolina") { var selectedOption = actions.value; var operation = payload.callback_id; var triggerId = payload.trigger_id; var token = payload.token; var timeStampRange = 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); } Jun 3, 2018 at 14:20
  • 1
    @MárioGuidoux I am having the invalid_auth issue. Were you able to fix it? There doesnt seem to be any helpful documentation on this :(
    – Mans
    Jan 12, 2019 at 4:31
0

if you request by application/json.
You use to header.authorization.

https://github.com/slackapi/python-slack-sdk/issues/302#issuecomment-825321015

API

Authorization in Headers

Content-Type: application/json
Authorization: 'Bearer ' + token
{
    "text": "hello",
    "as_user": true,
    "channel": "U##########"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.