slack-bot-api icon indicating copy to clipboard operation
slack-bot-api copied to clipboard

Unable to Send Private Message To New Member

Open datatypevoid opened this issue 9 years ago • 1 comments

I am attempting to write a basic Slack bot the sends new users a spiel when they join the Slack group.

My basic implementation is something like this, plus the boilerplate:

MyBot.prototype._isTeamJoin = function(message) {
  return message.type === 'team_join';
};

MyBot.prototype._onMessage = function(message) {
   if(this._isTeamJoin(message)) {
     var username = message.user.name;
     this._greetNewUser(username);
   }
};

MyBot.prototype._greetNewUser = function(username) {
  var greeting = 'Welcome!';  
  this.postMessageToUser(username, greeting, { as_user: true });
};

This message is never received by the new user. If you instead hard code it to send the message to a user that existed when the bot was fired up, it works without a hitch. Why is this? I have tried using the .getUsers() function and setting this.users to the updated data, but that doesn't seem to work either. What is the correct way to go about this?

datatypevoid avatar Jul 16 '16 01:07 datatypevoid

I seem to be able to fix this by pushing the message.user object to this.users before attempting to send the message. Still seems a bit hacky to me to do it that way.

datatypevoid avatar Jul 16 '16 01:07 datatypevoid