Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get username/userID in slack bot

https://github.com/DeronLee/starbot.git

I created a slack bot and it worked fine. But when somebody sends message to the bot, I'm not able to tell who sent it.

I tried msg.user msg.username, but all of them are undefined.

I just want my output to look like this

abc: @starbot hello
starbot: hello. abc 

finally. I got it.

    slack.users.info({
    token: config('SLACK_TOKEN'),
    user: msg.user
  }, (err, data) => {
    if (err) throw err
    var text = makeMessage.makeMessage(msg.text, data.user.name);
    sendMessage.send(msg, text, slack);
like image 488
Deron Lee Avatar asked Mar 22 '16 05:03

Deron Lee


People also ask

How do I get a bot user?

A bot user is added to a workspace by installing the app that the bot is associated with. Once you do, you'll get a bot token that is imbued with the bot scope. This token can be used with a subset of Web API methods that we'll discuss later.

How do I find my Slack token ID?

Visit the App Directory at my.slack.com/apps/manage on your desktop. Click Edit configuration next to the integration. On the configuration page, click Regenerate below the current token. This is the new token that you can use anywhere you need it.

Are Slack user IDs unique?

User IDs are now globally unique. Unless you are on an Enterprise Grid plan, the same user on two unrelated workspaces will have different user IDs. See the Sign in with Slack docs for even more information on these responses.


1 Answers

slack.users.info({
  token: config('SLACK_TOKEN'),
  user: msg.user
}, (err, data) => {
  if (err) throw err

  var text = makeMessage.makeMessage(msg.text, data.user.name);
  sendMessage.send(msg, text, slack);
like image 146
Deron Lee Avatar answered Sep 18 '22 04:09

Deron Lee