0

I'm developing a Slack bot using Glitch and Bolt for Javascript.

I'm opening a Model with two button (Approve and Reject), everything works up to where the user clicks on Approve and the app.action is reached but client is null and I need to send it to the next method...What can be missing?

This is the message I'm sending to a user after selecting everything in a dialog

async function sendSlackMessageToLead(requesterUsername, requesterSlackId, submittedValues, client, logger, leadUsername, leadId){
  try {
    await client.chat.postMessage({
      channel: leadId,
      text: "New PTO Request from " + requesterUsername,
      blocks: [
        {
          type: "header",
          text: {
            type: "plain_text",
            text: "You have requested a new PTO",
          },
        },
        {
          type: "section",
          fields: [
            {
              type: "mrkdwn",
              text:
                "*Type:*\n" +
                  submittedValues.type_block.type.selected_option.text !=
                null
                  ? submittedValues.type_block.type.selected_option.text.text
                  : "N/A",
            },
            {
              type: "mrkdwn",
              text: "*Created by:*\n<@" + requesterUsername + ">",
            },
          ],
        },
        {
          type: "section",
          fields: [
            {
              type: "mrkdwn",
              text:
                "*When:*\n" +
                "From " +
                submittedValues.startdate.datepicker_action_start_date
                  .selected_date +
                " \nTo      " +
                submittedValues.enddate.datepicker_action_end_date
                  .selected_date,
            },
          ],
        },
        {
          type: "section",
          fields: [
            {
              type: "mrkdwn",
              text:
                submittedValues.allDayBlock["checkboxes-action"]
                  .selected_options == ""
                  ? "All Day? No"
                  : "All Day? Yes",
            },
          ],
        },
        {
          type: "section",
          fields: [
            {
              type: "mrkdwn",
              text:
                "*Hours:*\n" +
                submittedValues.starttime.timepicker_action_start_time
                  .selected_time +
                " - " +
                +submittedValues.endtime.timepicker_action_end_time
                  .selected_time,
            },
            {
              type: "mrkdwn",
              text: "*Remaining balance:*\n32.0 hours (4 days)",
            },
          ],
        },
         {
          type: "actions",
          block_id: "approved_by_firstone_block",
          elements: [
            {
              type: "button",
              text: {
                type: "plain_text",
                emoji: true,
                text: "Approve",
              },
              style: "primary",
              value: JSON.stringify(submittedValues),
              action_id: "approved_by_firstone_click"
            },
            {
              type: "button",
              text: {
                type: "plain_text",
                emoji: true,
                text: "Deny",
              },
              style: "danger",
              value: JSON.stringify(submittedValues),
              action_id: "rejected_by_firstone_click"
            },
          ],
        },
      ],
    });
  } catch (error) {
    logger.error("Error while sending message to lead: " + error);
  }
}

Here clientSlack is undefined, the rest of the parameter have values in them

app.action({ action_id: 'approved_by_firstone_click', block_id: 'approved_by_firstone_block' }, async ({ body, clientSlack, ack, logger }) => {
  // Acknowledge the action
  await ack();
  console.log ("approved_by_firstone_click");
  console.log ("body: " + JSON.stringify(body));
  console.log ("ack: " + ack);
  console.log ("clientSlack: " + JSON.stringify(clientSlack));
  console.log ("logger: " + JSON.stringify(logger));
  var submittedValues = JSON.parse(body.actions[0].value);
  
  await sendSlackMessageToNextPerson(body.user.username, submittedValues, clientSlack, logger, "name.surname", "SlackId");
});

Thanks in advance. Guillermo.

polonskyg
  • 4,269
  • 9
  • 41
  • 93

1 Answers1

0

Checking the reference for the listener function, clientSlack isn't passed as an argument to the listener, but client is.

So changing clientSlack to client should work:

app.action({ action_id: 'approved_by_firstone_click', block_id: 'approved_by_firstone_block' }, async ({ body, client, ack, logger }) => {
  // Acknowledge the action
  await ack();
  console.log ("approved_by_firstone_click");
  console.log ("body: " + JSON.stringify(body));
  console.log ("ack: " + ack);
  console.log ("clientSlack: " + JSON.stringify(client));
  console.log ("logger: " + JSON.stringify(logger));
  var submittedValues = JSON.parse(body.actions[0].value);
  
  await sendSlackMessageToNextPerson(body.user.username, submittedValues, client, logger, "name.surname", "SlackId");
});

Pay attention to the fact that your listener function receives an object with properties, so you need to keep their exact name while unpacking the object. It is possible to also assign the property value under a different name, like this:

async ({ body, client: clientSlack, ack, logger }) => {
  console.log(clientSlack); // originally named `client`
  //...
}
jnv
  • 882
  • 10
  • 18