【问题标题】:Slack + Botkit: Automatic reconnect after RTM closureSlack + Botkit:RTM 关闭后自动重新连接
【发布时间】:2017-02-15 01:59:57
【问题描述】:

我将Howdy.ai's Botkit 用于一个简单的机器人应用程序,并让它在 VPS 上的 node.js 上运行。基本上,我从here 定制了一个 Slack 应用程序的示例,现在我正在努力保持机器人活着 - 在一段未定义的时间之后,到 Slack API 的 RTM 通道关闭,我不能找到重新连接的正确方法。到目前为止我试过了

controller.on('rtm_close',function(bot) {
   console.log('** The RTM api just closed. Trying reconnect...');
   // Try a reconnect
   bot.startRTM(function(err) {
      if (!err) {
          trackBot(bot);
      } else {
          console.log('** The RTM api couldn\'t be reopened. It\'s closed now.'); 
      }
   });
});

trackBot 函数控制日志记录:

function trackBot(bot) {
   _bots[bot.config.token] = bot;
}

似乎我错过了整个方法的工作原理。非常感谢任何帮助!

【问题讨论】:

    标签: slack-api slack botkit


    【解决方案1】:

    要启用重新连接,您需要将retry 配置值设置为true

      // Launch bot
      bot = controller.spawn({
        retry: true,
        token: 'xxx'
      })
    

    https://github.com/howdyai/botkit/blob/master/readme-slack.md#slack-controller

    【讨论】:

    • 谢谢,帮我搞定了!
    【解决方案2】:

    您是否尝试过使用 forever 模块? https://www.npmjs.com/package/forever

    然后运行它 forever stop bot.js; forever start bot.js && forever logs bot.js -f

    希望对你有帮助

    【讨论】:

    • 实际上,我已经永远在运行,但我认为我配置错误。随着我深入挖掘,这个问题似乎更多地与 Slack 的 API 本身有关——它不时关闭 RTM 通道。但这在我的代码中不被视为程序关闭,因此永远不会重新启动。由于重新连接也不起作用,我不得不在重新连接失败后使用process.exit() 语句强制关闭程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-27
    • 2017-12-04
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    相关资源
    最近更新 更多