0

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?

4

1 回答 1

6

不幸的是,使用 RTM API,需要打开 RTM 连接才能接收消息。这似乎是重言式,但如果测功机正在休眠,则 RTM 连接将关闭,服务器看不到任何消息。

您的解决方案可以是切换到事件 API(完全或只是启用唤醒您的应用程序。但是,如果您的应用程序中同时使用了 RTM API 和事件 API,请确保您不会做出两次反应到同一条消息)。消息事件的回调将唤醒您的应用。

但是,对第一条消息的响应会有很大的延迟。如果那不可接受,那么您总是可以支付 7 美元/月 :-p

于 2016-10-01T07:51:16.013 回答