How can I get the action button ID when developing the Slack app?

802 views Asked by At

H :)

I am currently developing a slack app using nodejs and typescripts.

And I finished adding the button by creating rich message layouts.

{
    "blocks": [
        {
            "type": "actions",
            "elements": [
                {
                    "type": "button",
                    "text": {
                        "type": "plain_text",
                        "text": "Cancel"
                    },
                    "value": "cancel",
                    "action_id": "actionId-0"
                }
            ]
        }
    ]
}

And I have a request URL too. (Slack will send an HTTP POST request with information to this URL when users interact with a shortcut or interactive component.)

server.post('REQUEST_RUL', async (req, reply) => {
      try {
        tag = req.body.text.split(' ');
        reply
          .code(200)
          .header('Content-Type', 'application/json; charset=utf-8')
          .send({
            response_type: 'in_channel',
            blocks: [
              {
                type: 'image',
                title: {
                  type: 'plain_text',
                  text: `${tag}`
                },
                image_url: `${url}`,
                alt_text: `${tag}`
              },
              {
                type: actions,
                elements: [
                    {
                    type: button,
                    text: {
                        type: plain_text,
                        text: Cancel
                    },
                    value: cancel,
                    action_id: actionId-0
                    }
                 ]
              }
            ]
          });
      } catch (e) {
        reply
          .code(200)
          .header('Content-Type', 'application/json; charset=utf-8')
          .send({
            response_type: 'in_channel',
            message: e.message,
            text: `We couldn't find a match for '${tag}'. Please try another tag.`
          });
      }
    });

So I know that the action button has an id, but how can I get and use this id when I click the button?

The interaction should occur when the user clicks the button.

1

There are 1 answers

0
Sejal Parwal On

simply use: You can first check whether actions exists or not & then use the following to fet actions[0].action_id to fetch the action_id.