slackapi / node-slack-interactive-messages

Slack Buttons, Menus, and Dialogs made simpler for Node
MIT License
133 stars 41 forks source link

500 Error when interacting with messages #68

Closed brianholle closed 6 years ago

brianholle commented 6 years ago

Description

I am currently getting a "500_Service_Error" in slack whenever I respond to an interactive message.

I have checked my slack node service, and there does not seem to be any error. My interactive messages are being received by the service and my service is then updating the original message and sending that response back to my slack channel. All of this works as expected, however, I am getting an additional message that states:

"Darn - that didn't work (error message: 500_service_error). Manage the app at {app name}.

What could possibly cause this problem?

Requirements

brianholleibm commented 6 years ago

@Slack/interactive-messages version: 0.4.0 @Slack/client version: 4.3.1

Updating to the newest breaks everything. 404 Errors when sending push's to server via interacting with message

aoberoi commented 6 years ago

Hi @brianholleibm, sorry about your difficulties.

Can you provide an example of the code in your service (feel free to simplify)? I’d like to see if you’re doing anything we didn’t anticipate.

Could you also run your application with the following environment variable set? This will enable more verbose logging, and when you interact with a message we should get a better idea of what went wrong.

DEBUG=@slack/interactive-messages:*

aoberoi commented 6 years ago

Also, when updating to the latest, you need to be careful to not initialize with a Verification Token, and instead use the Request Signing Secret.

brianholle commented 6 years ago

Here is an example of my server-side code:

// Import dependencies
const { createMessageAdapter}  = require('@slack/interactive-messages');
const { WebClient }  = require('@slack/client');
const slack_token = "SlAcKtOkEn";
const slack_oAuth = "SlAcKoAuThlEl";

// Create the adapter using the app's verification token, read from environment variable
const slackInteractions = createMessageAdapter(slack_token);

const web = new WebClient(slack_oAuth);
const port = 8080;

// Start the built-in HTTP server
slackInteractions.start(port).then(() => {
  console.log(`server listening on port ${port}`);
});

slackInteractions.action({type: 'button', callbackId: 'ig_pl_bot'}, (payload, respond) => {
  try{
    console.log(`The user ${payload.user.name} in team ${payload.team.domain} pressed a button`);
    const reply = payload.original_message;
    var text = 'Some Value';
    // Before the work completes, return a message object that is the same as the original but with
    // the interactive elements removed.
    reply.text = text;
    reply.attachments[0].title = "Post  " + (reply.attachments[0].title).slice((reply.attachments[0].title).lastIndexOf("By"));
    delete reply.attachments[0].actions;
    respond(reply);
  } catch (error) {
    console.error("SlAcKeRrOr " + error);
  }
});

@aoberoi what do you mean by setting debug? How do I do that? Also, thanks for the info about updating the token to the request signing secret. I will keep that in mind when updating. For now, i will try to get it all working with this version of slack (unless that is a poor idea?).

aoberoi commented 6 years ago

thanks for supplying the example @brianholle. there's a few things i suggest:

  1. update your app to use the latest version of @slack/interactive-messages. there were some bugs in older versions that have been fixed since. at this point, i'm not sure if you are being affected by any of those bugs, but eliminating that possibility will likely help us find the cause of any remaining issues. this means you should replace the value of slack_token above with the request signing secret (as i previously mentioned).

  2. in the example you've shown, you don't need to call respond(reply), since you have the value of reply ready to return without waiting for any asynchronous code to complete. so i suggest you simply return reply; instead. if this example isn't like your real code, and in your real code you actually want to wait for something asynchronous to complete, then you should still return reply (with the actions removed as you did in the example) but you should also call respond() with a JSON object representing the message. i think the reason for the error is that you are calling respond() before the function returns. this means that you haven't yet acknowledged the button click (this happens when the function returns), but you are asking Slack to update the message (this happens when you call respond()). this order of requests doesn't make sense.

aoberoi commented 6 years ago

if this is a common cause for issues, we can likely make the respond() function wait until the next tick to do its work. that would likely have prevented this issue for you (but would have resulted in a less-good way of accomplishing the same thing). i'll create a new issue for anyone else who is interested in this to "+1". it would also be a decent first contribution.