2

I have a Slack command which displays a button. When I click on this button I need to display a modal. For this, after clicking it I do this:

const dialog = {
  callback_id: "submit-ticket",
  elements: [
    {
      hint: "30 second summary of the problem",
      label: "Title",
      name: "title",
      type: "text",
      value: "teste"
    },
    {
      label: "Description",
      name: "description",
      optional: true,
      type: "textarea"
    },
    {
      label: "Urgency",
      name: "urgency",
      options: [
        { label: "Low", value: "Low" },
        { label: "Medium", value: "Medium" },
        { label: "High", value: "High" }
      ],
      type: "select"
    }
  ],
  submit_label: "Submit",
  title: "Submit a helpdesk ticket"
};

const modalInfo = {
    dialog: JSON.stringify(dialog),
    token, // this is correct
    trigger_id: actionJSONPayload.trigger_id
  };


  // This is what I get confused with...

  // Method 1
  slack.dialog.open(modalInfo).catch(err => {
    console.log("ERROR: ", err);
  });
  // end method 1

  // Method 2
  sendMessageToSlackResponseURL(actionJSONPayload.response_url, modalInfo);

...

function sendMessageToSlackResponseURL(responseURL: any, JSONmessage: any) {
  const postOptions = {
    headers: {
      "Content-type": "application/json"
    },
    json: JSONmessage,
    method: "POST",
    uri: responseURL
  };
  request(postOptions, (error: any, response: any, body: any) => {
    if (error) {
      console.log("----Error: ", error);
    }
  });
}
// end method 2

I get always Error: invalid_trigger using method1 when this trigger is something that my button is generating automatically.

Method 2 doesn't throw any error but doesn't open any modal/dialog either.

The official documentation is not quite clear and I don't know if I need to call dialog.open or views.open. Either way, the last one is not available from Slack package

This is also the button I'm displaying before anything:

const message = {
        attachments: [
          {
            actions: [
              {
                name: "send_sms",
                style: "danger",
                text: "Yes",
                type: "button",
                value: "yes"
              },
              {
                name: "no",
                text: "No",
                type: "button",
                value: "no"
              }
            ],
            attachment_type: "default",
            callback_id: "alert",
            color: "#3AA3E3",
            fallback: "We could not load the options. Try later",
            text: "Do you want to alert by SMS about P1 error/fix?"
          }
        ],
        text: "P1 SMSs"
      };
Dani
  • 3,128
  • 2
  • 43
  • 91
  • This answer describes how to combine interactive menus and dialogs: https://stackoverflow.com/questions/53242419/how-to-combine-slash-commands-buttons-and-dialogs-for-a-menu Does that solve your issue? – Erik Kalkoken Oct 22 '19 at 14:42
  • so that means that I cannot make a call to the API using that library, that I should do it using the url from the payload? – Dani Oct 22 '19 at 14:45
  • no. You call dialog.open to open the Dialog. The response_url is needed to update the initial message after the dialog is submitted. – Erik Kalkoken Oct 22 '19 at 14:47
  • but when I call `dialog.open` is when it fails – Dani Oct 22 '19 at 15:00
  • I updated my question with the 2 methods I tried – Dani Oct 22 '19 at 15:29

1 Answers1

1

Copy a modal from here

const headers = {
  headers: {
    "Content-type": "application/json; charset=utf-8",
    "Authorization": "Bearer " + token
  }
};

const modalInfo = {
        "token": token,
        "trigger_id": reqBody.trigger_id,
        "view": slack.modal
      };

      axios
        .post("https://slack.com/api/views.open", modalInfo, headers)
        .then(response => {
          const data = response.data;
          if (!data.ok) {
            return data.error;
          }
        })
        .catch(error => {
          console.log("-Error: ", error);
        });

But most importantly, when we do some changes to our app we need to reinstall it and also when we do it, the token changes and this is something I couldn't find on the documentation

Dani
  • 3,128
  • 2
  • 43
  • 91