Bolt-js: How do I send ephemeral message/ack when an event is triggered?

Created on 4 Jul 2020  路  4Comments  路  Source: slackapi/bolt-js

Description

Describe your issue here.

What type of issue is this? (place an x in one of the [ ])

  • [ ] bug
  • [ ] enhancement (feature request)
  • [x ] question
  • [ ] documentation related
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [ x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [ x] I've read and agree to the Code of Conduct.
  • [ x] I've searched for any related issues and avoided creating a duplicate issue.

Reproducible in:

package version: 2.1.1

node version:v13.3.0

OS version(s):

  • ProductName: Mac OS X
  • ProductVersion: 10.15.5
  • BuildVersion: 19F101

Steps to reproduce:

I am trying to send an ephemeral message on member_joined_channel event trigger. When I try to use ack(), it throws an error TypeError: ack is not a function

Below is my code:

app.event('member_joined_channel', async ({ event, say, ack }) => {
    try {
      await ack({
        text: 'blah'
      });
    } catch(e) {
      console.log(e);
    }
});

Expected result:

I want all the messages to be ephemeral and none of the messages should get posted on the channel. I can do the above in actions/commands but it's not working for the events. Basically, all I want is, be it a command, event or a simple message, all the replies from the bot to the users need to be ephemeral. The below code works though

app.event('member_joined_channel', async ({ event, say, ack }) => {
  app.client.chat.postEphemeral({
      token: process.env.SLACK_BOT_TOKEN,
      channel: event.channel,
      user: event.user,
      text: `Ephemeral Message Test`
    });
});

Actual result:

`TypeError: ack is not a function`
question

Most helpful comment

You cannot send a reply message as an HTTP response to Events API requests from Slack. So, ack() method is not available in app.event listeners (Bolt does the acknowledgment of requests for you under the hood). The only way you can post an ephemeral message in response to Events API is, as you did, utilizing chat.postEphemeral API method.

All 4 comments

You cannot send a reply message as an HTTP response to Events API requests from Slack. So, ack() method is not available in app.event listeners (Bolt does the acknowledgment of requests for you under the hood). The only way you can post an ephemeral message in response to Events API is, as you did, utilizing chat.postEphemeral API method.

@seratch that is helpful. One last question, what is the difference between ack() and respond()? can I use them interchangeably? The reason am asking is, I can send ephemeral messages using respond() too, so can I change chat. postEphemeral to respond() instead?

As mentioned here, respond() is a utility method for easily utilizing response_url. To learn about response_url, refer to its document.

can I change chat.postEphemeral to respond() instead?

No, you can't because response_url doesn't exist in Events API payloads. Please use chat.postEphemeral in app.event listeners.

Perfect. Thank you

Was this page helpful?
0 / 5 - 0 ratings