Search code examples
javascriptjsonslack-apislack

Slack API (JSON Data)


I'm trying to use this method provided via the Slack API to grab the "name" attribute of the "user" object and the code I'm using to do that is as follows:

controller.hears(['users'], 'direct_message, direct_mention, mention', function(bot, message){
bot.api.users.info({}), function(err, response) {
bot.reply(message, response);
var slack_username = message["user"][2];
console.log(">>>>> " + slack_username);
});
});

I'm not sure what I'm doing wrong/missing and any help would be greatly appreciated! Thanks in advance!

EDIT:

Expected Output: "dkulas"

Terminal Output" "U"

debug: Got response null {"OK":false,"error":"user_not_found"}

degub: SAY { ok: false, error: 'user_not_found', channel" 'D0UV5S7MZ' }


Solution

  • The Slack API method users.info requires you to provide the Slack User ID in the query. The format is U12345678. (also see here for the usage documentation)

    If you want to get the user info for a user by name, use the API method users.list to get the list of all users including name and user ID and then search that list for the right match. users.list provides all information about a user, so you don't need to call user.info.

    See here for the documentation on this method.