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

NodeError: The "last argument" argument must be of type function #533

Closed
FrancescoPolitano opened this issue Apr 15, 2018 · 4 comments
Closed
Labels
question M-T: User needs support to use the project

Comments

@FrancescoPolitano
Copy link

FrancescoPolitano commented Apr 15, 2018

Hi, I'm stuck with this problem while trying to send a message after an event call using the API.

const SlackClient = require('@slack/client').WebClient;
const createSlackEventAdapter = require('@slack/events-api').createSlackEventAdapter;

// *** Initialize event adapter using verification token from environment variables ***
const slackEvents = createSlackEventAdapter(process.env.SLACK_VERIFICATION_TOKEN, {
  includeBody: true
});```



// *** Greeting any user that says "hi" ***
slackEvents.on('message', (message, body) => {
  // Only deal with messages that have no subtype (plain messages) and contain 'hi'
  if (!message.subtype && message.text.indexOf('hi') >= 0) {
    // Initialize a client
    const slack = getClientByTeamId(body.team_id);
    // Handle initialization failure
    if (!slack) {
      return console.error('No authorization found for this team. Did you install this app again after restarting?');
    }
    // Respond to the message back in the same channel
    slack.chat.postMessage(message.channel, `Hello <@${message.user}>! :tada:`)
      .catch(console.error);
  }
});

When I try to call the postMessage function Node crashes and i get this:

NodeError: The "last argument" argument must be of type function

I'll really appreciate some hints.

@aoberoi
Copy link
Contributor

aoberoi commented Apr 15, 2018

@FrancescoPolitano thanks for bringing this to our attention. it looks like you are using the example from the @slack/events-api documentation, which has not been updated to use v4 of this package. i'll make a note of this on the issue tracker for that package.

in the meantime, the fix is to change the postMessage line to the following:

// Respond to the message back in the same channel
slack.chat.postMessage({ channel: message.channel, text: `Hello <@${message.user}>! :tada:` })
  .catch(console.error);

@aoberoi aoberoi added the question M-T: User needs support to use the project label Apr 15, 2018
@aoberoi aoberoi closed this as completed Apr 15, 2018
@FrancescoPolitano
Copy link
Author

FrancescoPolitano commented Apr 16, 2018

Thank you! I have another problem, postmessage give me an error for circular structures on WebClient.ts line 528, but I don't know how to resolve circular dependency on your "value" structure
...
if (Buffer.isBuffer(value) || isStream(value)) { containsBinaryData = true; } else if (typeof value !== 'string' && typeof value !== 'number' && typeof value !== 'boolean') { // if value is anything other than string, number, boolean, binary data, a Stream, or a Buffer, then encode it // as a JSON string. serializedValue = JSON.stringify(value); }
...

@aoberoi
Copy link
Contributor

aoberoi commented Apr 16, 2018

@FrancescoPolitano the value that line is referring to is part of the options that you have passed in, so it seems like you need to look closer at what you've passed in.

for example, an options parameter like the following would cause the same kind of error:

const attachment = {
  text: 'My attachment',
  fields: [
    {
      title: 'Something',
      value: attachment,  // ERROR: this is a circular reference, JSON.stringify() will fail
    },
  ],
};
web.chat.postMessage({
  channel: 'C12345',
  text: `Hello, world',
  attachments: [attachment],
}).then(console.error);

There's no good way to turn a circular reference into a JSON object to send to Slack. It's likely a bug in your code. Take a closer look and try to flatten the structure as much as possible.

@FrancescoPolitano
Copy link
Author

Thank you for your help and for your good code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question M-T: User needs support to use the project
Projects
None yet
Development

No branches or pull requests

2 participants