将 botkit 连接到我的服务 restful api

Connect botkit to my service restful api

我在本地安装了 botkit,并且与 slack 配合得很好。现在,我想将机器人与外部 restful api 连接,例如:

HUMAN:您连接了多少客户端? 机器人:机器人在内部对我服务的其余 api 执行查询,然后回答 Bot:有21个客户端连接。

有什么建议吗?

我们做了类似的操作,非常简单。使用某种类型或 HTTP 客户端对您的端点进行 GET。我们使用 request npm。然后你只需要在回调中调用 bot.reply 。为了开始互动,我使用 ambient 来收听机器人被邀请进入的任何频道,但如果您愿意的话,您可以将其设置为 direct_message

var request = require('request');

module.exports = function(controller) {
    controller.hears(['How many clients'], 'ambient', function(bot, message) {

        request('http://api.com/totalUsers', function (err, response, body) {

            console.log('error: ', err); // Handle the error if one occurred
            console.log('statusCode: ', response && response.statusCode); // Check 200 or such
            console.log('This is the count of users: ', body.usersCount);

            bot.reply(message, 'There are ' + body.usersCount + ' clients connected');

        });
    });
};