【问题标题】:Is there a way to wake up the application when a user sends a message and it's idle on heroku?当用户发送消息并且它在heroku上空闲时,有没有办法唤醒应用程序?
【发布时间】:2016-09-27 20:40:17
【问题描述】:

我有一个在 heroku 上免费运行的机器人,我正在寻找一种方法来在 Slack 中收到来自用户的消息时唤醒应用程序。

我的 Procfile 中有一个网络工作者:

web: npm start

我还设置了一个网络服务器和 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();

机器人正常启动,闲置 30~ 分钟后进入空闲状态

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

现在,如果我在 slack 中向机器人发送消息,它将不再响应并且应用程序不会唤醒除非我向网络服务器发送请求。

我不想阻止机器人空闲,因为它会消耗我的 dyno 时间,有没有办法在用户通过 slack 向机器人发送消息时唤醒应用程序?

【问题讨论】:

    标签: heroku slack-api botkit


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2014-03-29
      • 1970-01-01
      • 2019-08-20
      • 1970-01-01
      • 2011-03-28
      • 1970-01-01
      • 1970-01-01
      • 2021-07-06
      • 2010-09-06
      相关资源
      最近更新 更多