howdyai / botkit

Botkit is an open source developer tool for building chat bots, apps and custom integrations for major messaging platforms.
MIT License
11.48k stars 2.28k forks source link

Get user info from outgoing message #921

Closed thepavangollapalli-zz closed 7 years ago

thepavangollapalli-zz commented 7 years ago

I'm working with the Send and Receive middleware on a Slack chatbot and trying to store the user id of an outgoing message, but wasn't able to find any good ways to do it. I've found methods that use the api like

bot.api.users.profile.get({ user: msg.user }, function(err, response) {
  console.log(response);
});

or

bot.api.users.info({user: msg.user}, (error, response) => {
  let {name, real_name} = response.user;
  console.log(name, real_name);
  user = real_name;
});

but msg.user only exists for incoming messages in the Receive middleware and so those solutions do not work for the Send middleware. I was able to get the user id by editing the slack worker so it manually returns the destination user id, but any non-hacky solutions would be great!

shashvattrip commented 7 years ago

@thepavangollapalli did you figure this out? I need to mutate my outbound messages and user info is absolutely critical part of this process.

jonchurch commented 7 years ago

What user information specifically are you unable to find? Can you share some code?

User id is not explicitly needed to deliver a message, but is usualy hanging out on the message depending on how that message is sent.

You are looking to augment the Send middleware, and want access to the slack userid in that middleware, correct? On Thu, Aug 10, 2017 at 8:02 AM slearner notifications@github.com wrote:

@thepavangollapalli https://github.com/thepavangollapalli did you figure this out? I need to mutate my outbound messages and user info is absolutely critical part of this process.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/howdyai/botkit/issues/921#issuecomment-321531873, or mute the thread https://github.com/notifications/unsubscribe-auth/AMUR2wwBGNEOFZhzVqnsNucUNPJVeAvYks5sWvFYgaJpZM4OReee .

thepavangollapalli-zz commented 7 years ago

Go to node_modules/botkit/lib/Slackbot_worker.js and find the function bot.replyPublic. The functions following replyPublic all have a src object passed in that contains userid and several other things you can attach to the outgoing message object, simply by adding something like msg.user = src.user after the line msg.channel = src.channel that is in each function.

thepavangollapalli-zz commented 7 years ago

Update: I was able to get the information I needed by using the Heard middleware instead without modifying the slackbot-worker file!