Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Message.respond doesn't work for message actions #106

Closed
TK95 opened this issue May 28, 2018 · 6 comments · Fixed by #107 or #108
Closed

Message.respond doesn't work for message actions #106

TK95 opened this issue May 28, 2018 · 6 comments · Fixed by #107 or #108

Comments

@TK95
Copy link
Contributor

TK95 commented May 28, 2018

Greetings.

I've faced the issue related to message actions, looks like Message.respond doesn't do anything when I'm inside in message action handler.

Steps to reproduce:

  1. Register some message action.
// actions.js
const slapp = new Slapp(...);

slapp.messageAction(/my-message-action/, messageActionHandler)
  1. Trigger it.
  2. Inside that message action handler call msg.respond('Some text') and notice that actually nothing is happening (The message hasn't been posted in the channel).
// messageActionHandler.js

function messageActionHandler(msg) {
   return msg.respond('Some text from Message Action!');
}

Additional info:

  1. This bug occurs in the last version of Slapp. Currently, it's 3.2.0.
  2. Everything works just fine for common actions.
  3. If I comment out this if statement then Message.respond starts to work fine and I see necessary messages in the channel.

Any questions let me know.

@mbrevoort
Copy link
Contributor

Hi @TK95, can you try v3.2.1 and confirm the issue is resolved?

This is what I tested with BTW:

  slapp.messageAction('my_action', (msg, message) => {
    msg.respond({
      text: `:tada: your action has been received :wink: -  ${message.text}`,
      response_type: 'ephemeral'
    })
  })

@TK95
Copy link
Contributor Author

TK95 commented May 31, 2018

Hey, @mbrevoort and thanks for looking into it!

I installed v3.2.1 and did tests. Your snippet works just fine!
However, my case is a bit tricky and I still have the same problem. I'm sorry for not being precise.

Please, consider the following code:
Example 1:

const middlewareFn = (msg, next) => {
    return msg.respond({
      text: ':tada: Hello from middleware',
      response_type: 'ephemeral'
    })
  }

  bot.use(middlewareFn)

  bot.messageAction('my_action', (msg) => {
    return msg.respond({
      text: ':tada: Hello from message action handler',
      response_type: 'ephemeral'
    })
  })

The aforementioned code is very similar to my real one (Sorry I can't disclosure it). And this code doesn't work as expected.

If I trigger the message action I expect that "Hello from middleware" will be posted to the channel, but nothing is happening.

Please, see the second code fragment:
Example 2:

const middlewareFn = (msg, next) => {
    return msg.respond({
      text: ':tada: Hello from midllware',
      response_type: 'ephemeral'
    })
  }

 // bot.use(middlwareFn)

  bot.messageAction('my_action', middlewareFn, (msg) => {
    return msg.respond({
      text: ':tada: Hello from message action handler',
      response_type: 'ephemeral'
    })
  })

In this example, I commented out bot.use(middlewareFn) line and placed middlewareFn in bot.messageAction(...). And actually, this code works as expected, I see the message from middlewareFn.

Are there any problems with using middlewares in message actions?

I tried to add a callback argument, like this msg.respond(msg, cb), there is no error.

If you need any other details, please let me know.

@selfcontained
Copy link
Contributor

selfcontained commented May 31, 2018

I think what may be causing the behavior you're seeing is that msg.respond() is being called twice in the processing of the event. I think if you added a callback to the msg.respond inside the messageAction handler, you would see an error, but not w/ the msg.respond call from inside the middleware (if I'm right 😄).

For some context, msg.respond() is designed to be called once, as it ends up sending the response to the slack event request, which is how you can trigger things like ephemeral messages from certain types of events.

You could probably accomplish what you want in a few ways. Slack added an api to postEphemeral - which is a way to create an ephemeral message outside having to respond to an event request. You could make that api call either in your middleware, or from within your messageAction handler. My suggestion would be to have the middleware call the chat.postEphemeral api, which leaves the default behavior for msg.respond() available inside any event handlers.

@mbrevoort mbrevoort reopened this May 31, 2018
@mbrevoort
Copy link
Contributor

Fix coming. With this fix msg.respond will work but your messageAction handler will only be called if you call next() in the middleware.

@mbrevoort
Copy link
Contributor

@TK95, v3.2.2 should fix the problem. Just make sure you call next() in your middleware so the messageAction() handler is matched.

@TK95
Copy link
Contributor Author

TK95 commented Jun 1, 2018

Thanks, @mbrevoort and @selfcontained. Everything works like a charm!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants