Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I post a welcome message to a newly joined user via a Slack bot?

I am trying to leverage Slack's Real-time messaging (RTM) API to post a welcome message to newly joined users of my team from a "Greeter Bot".

The event I'm listening for is team_join.

I have confirmed that the event is firing, and that messages can be sent to previously joined users out upon receiving that event. However, when trying to notify the newly joined user, nothing comes through. I've played around with adding a delay (up to 45secs) after receiving the event before notifying the user, but still no dice.

Here my index.js file:

var SlackBot = require('slackbots');

var bot = new SlackBot({
  token: process.env.SLACK_TOKEN || '',
  name: process.env.BOT_NAME || 'greeterbot'
});

bot.on('message', function(data) {
  var self = this;
  if ('team_join' === data.type) {
    setTimeout(function() {
      var message = 'hello.';
      // this works. 'some crusty old user' gets a DM message from my greeterbot.
      self.postMessageToUser('some crusty old user', message, { as_user: true });
      // FAIL. what am i doing wrong?!
      self.postMessageToUser(data.user.name, message, { as_user: true });
    }, 45000);
    console.log("'" + data.user.name + "' has joined the team."); // works. the user name is present in the log.
  }
});
like image 209
stopfstedt Avatar asked Jan 20 '26 19:01

stopfstedt


2 Answers

I figured it out. There is a caching issue with the underlying bot library that I'm using.

For reference, here is a link to a PR that aims to address this issue. https://github.com/mishk0/slack-bot-api/pull/25

Switching libs now.

like image 199
stopfstedt Avatar answered Jan 23 '26 10:01

stopfstedt


Our Slack team, SKGTech.io uses Janitr that we wrote. Janitr is a Slack bot that welcomes new users based on your preferences.

like image 33
kbariotis Avatar answered Jan 23 '26 10:01

kbariotis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!