GithubHelp home page GithubHelp logo

Comments (6)

mbrevoort avatar mbrevoort commented on May 16, 2024 1

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

from bolt-js.

mbrevoort avatar mbrevoort commented on May 16, 2024

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

from bolt-js.

TK95 avatar TK95 commented on May 16, 2024

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.

from bolt-js.

selfcontained avatar selfcontained commented on May 16, 2024

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.

from bolt-js.

mbrevoort avatar mbrevoort commented on May 16, 2024

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

from bolt-js.

TK95 avatar TK95 commented on May 16, 2024

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

from bolt-js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.