Откройте модал с помощью команды Slack - PullRequest
0 голосов
/ 22 октября 2019

У меня есть команда Slack, которая отображает кнопку. Когда я нажимаю на эту кнопку, мне нужно отобразить модал. Для этого после нажатия я делаю это:

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

Я всегда получаю Error: invalid_trigger, используя method1, когда этот триггер является чем-то, что моя кнопка генерирует автоматически.

Метод 2 невыдает любую ошибку, но также не открывает модальные / диалоговые окна.

Официальная документация не совсем ясна, и я не знаю, нужно ли мне вызывать dialog.open или views.open. В любом случае, последний не доступен из Slack пакета

Это также кнопка, которую я показываю раньше всего:

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"
      };

1 Ответ

0 голосов
/ 25 октября 2019

Скопируйте модальное с здесь

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);
        });

Но самое главное, когда мы вносим некоторые изменения в наше приложение, нам нужно переустановить его, а также когда мысделайте это, токен изменится и я не смог найти это в документации

...