Unable to Send Private Message To New Member
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?
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.