Search code examples
herokuslack-apibotkit

Is there a way to wake up the application when a user sends a message and it's idle on heroku?


I'm have a bot running on heroku within a free tier, and I'm looking for a way to wake the application when a message is received from the user in Slack.

I have a web worker in my Procfile:

web: npm start

I also setup a webserver and botkit:

var app = express();
var port = process.env.PORT || 3000;

app.listen(port, function (err) {
  if (err) throw err;

  console.log('Bot up!');
});

var controller = Botkit.slackbot({
  debug: false
});

var bot = controller.spawn({
  token: botConfig.SLACK_BOT_KEY
}).startRTM();

The bot goes up as normal, and goes idle after 30~ minutes of inactivity

2016-09-27T18:55:18.013318+00:00 app[web.1]: info: ** API CALL: https://slack.com/api/rtm.start
2016-09-27T18:55:18.027341+00:00 app[web.1]: Bot up!
2016-09-27T18:55:18.253156+00:00 app[web.1]: notice: ** BOT ID: bot ...attempting to connect to RTM!
2016-09-27T18:55:18.298822+00:00 app[web.1]: notice: RTM websocket opened
2016-09-27T18:55:18.346493+00:00 heroku[web.1]: State changed from starting to up
2016-09-27T19:25:42.535535+00:00 heroku[web.1]: Idling
2016-09-27T19:25:42.536182+00:00 heroku[web.1]: State changed from up to down
2016-09-27T19:25:46.877746+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2016-09-27T19:25:48.014988+00:00 heroku[web.1]: Process exited with status 143

Now, if I send a message to the bot in slack, it won't respond anymore and the application won't wake up unless I send a request to the webserver.

I don't want to prevent the bot from idling as it would consume my dyno hours, is there a way I can wake up the app when a user sends a message to the bot through slack?


Solution

  • Unfortunately, using RTM API, the RTM connection needs to be opened to receive a message. It seems like a tautology, but if the dyno is sleeping, the RTM connection is closed, and no message is seen by the server.

    Your solution can be to switch to the Events API (either entirely, or just to enable waking-up your app. However, it you have both the RTM API and the Events API used in your app, make sure you don't react twice to the same message). The callback from the message event will wake up your app.

    There will be a significant lag for the response to the first message, though. If that's not acceptable, then you could always pay 7$ / months :-p