UNPKG

botpress-janis

Version:

Monitor bots and take over live from Slack

437 lines (364 loc) 14 kB
module.exports = /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = "/Users/mnatha/Dropbox/janis/botpress-janis"; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { module.exports = __webpack_require__(1); /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { 'use strict'; var _botpressVersionManager = __webpack_require__(2); var _botpressVersionManager2 = _interopRequireDefault(_botpressVersionManager); var _lodash = __webpack_require__(3); var _lodash2 = _interopRequireDefault(_lodash); var _bluebird = __webpack_require__(4); var _bluebird2 = _interopRequireDefault(_bluebird); var _janis = __webpack_require__(5); var _janis2 = _interopRequireDefault(_janis); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new _bluebird2.default(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return _bluebird2.default.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } var janis = null; var outgoingMiddleware = function outgoingMiddleware(event, next) { if (janis === null) { return next(); } var message = Object.assign({}, event.raw); // Clone the raw message if (event.platform === "slack") { message.channel = event.raw.channelId; } else if (event.platform === "facebook") { message.channel = event.raw.to; } if (event.text) { message.text = event.text; } else { message.text = ""; } janis.hopOut(event.platform, message); return next(); }; var incomingMiddleware = function incomingMiddleware(event, next) { if (janis === null) { return next(); } janis.hopIn(event.platform, event.raw, event.user).then(function (isPaused) { event.raw.paused = isPaused; if (!isPaused) { return next(); } }); }; var postProcessingIncomingMiddleware = function postProcessingIncomingMiddleware(event, next) { if (janis === null) { return next(); } if (!event.raw.paused) { janis.logUnkownIntent(event.platform, event.raw); } return next(); }; module.exports = { config: { clientKey: { type: 'string', default: '', env: 'WORDHOP_CLIENT_KEY' }, apiKey: { type: 'string', default: '', env: 'WORDHOP_API_KEY' } }, init: function init(bp) { (0, _botpressVersionManager2.default)(bp, __dirname); bp.middlewares.register({ name: 'janis.hopOut', type: 'outgoing', order: 100, handler: outgoingMiddleware, module: 'botpress-janis', description: 'Sends out messages.' }); bp.middlewares.register({ name: 'janis.hopIn', type: 'incoming', order: 21, handler: incomingMiddleware, module: 'botpress-janis', description: 'Receive messages.' + ' This middleware should be placed at the beginning as it sets whether or not a bot is paused.' }); bp.middlewares.register({ name: 'janis.logUnkownIntent', type: 'incoming', order: 100, handler: postProcessingIncomingMiddleware, module: 'botpress-janis', description: 'Sends alerts to agents when your bot does not understand the user intent.' + ' This middleware should be placed anywhere after the `hear` handler.' }); }, ready: function () { var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee4(bp, configurator) { var _this = this; var config, router, setConfigAndRestart; return regeneratorRuntime.wrap(function _callee4$(_context4) { while (1) { switch (_context4.prev = _context4.next) { case 0: _context4.next = 2; return configurator.loadAll(); case 2: config = _context4.sent; router = bp.getRouter('botpress-janis'); setConfigAndRestart = function () { var _ref2 = _asyncToGenerator(regeneratorRuntime.mark(function _callee(newConfigs) { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _context.next = 2; return configurator.saveAll(newConfigs); case 2: if (newConfigs.apiKey && newConfigs.clientKey) { if (janis === null) { janis = new _janis2.default(bp, newConfigs); } janis.setConfig(newConfigs); } case 3: case 'end': return _context.stop(); } } }, _callee, _this); })); return function setConfigAndRestart(_x3) { return _ref2.apply(this, arguments); }; }(); router.get('/config', function () { var _ref3 = _asyncToGenerator(regeneratorRuntime.mark(function _callee2(req, res) { return regeneratorRuntime.wrap(function _callee2$(_context2) { while (1) { switch (_context2.prev = _context2.next) { case 0: _context2.t0 = res; _context2.next = 3; return configurator.loadAll(); case 3: _context2.t1 = _context2.sent; _context2.t0.json.call(_context2.t0, _context2.t1); case 5: case 'end': return _context2.stop(); } } }, _callee2, _this); })); return function (_x4, _x5) { return _ref3.apply(this, arguments); }; }()); router.post('/config', function () { var _ref4 = _asyncToGenerator(regeneratorRuntime.mark(function _callee3(req, res) { return regeneratorRuntime.wrap(function _callee3$(_context3) { while (1) { switch (_context3.prev = _context3.next) { case 0: setConfigAndRestart(req.body); _context3.t0 = res; _context3.next = 4; return configurator.loadAll(); case 4: _context3.t1 = _context3.sent; _context3.t0.json.call(_context3.t0, _context3.t1); case 6: case 'end': return _context3.stop(); } } }, _callee3, _this); })); return function (_x6, _x7) { return _ref4.apply(this, arguments); }; }()); bp.events.on('assistanceRequested', function (event) { if (janis === null) { return; } if (event.raw.paused) { return; } janis.assistanceRequested(event.platform, event.raw); }); if (config.apiKey && config.clientKey) { janis = new _janis2.default(bp, config); } case 9: case 'end': return _context4.stop(); } } }, _callee4, this); })); function ready(_x, _x2) { return _ref.apply(this, arguments); } return ready; }() }; /***/ }), /* 2 */ /***/ (function(module, exports) { module.exports = require("botpress-version-manager"); /***/ }), /* 3 */ /***/ (function(module, exports) { module.exports = require("lodash"); /***/ }), /* 4 */ /***/ (function(module, exports) { module.exports = require("bluebird"); /***/ }), /* 5 */ /***/ (function(module, exports, __webpack_require__) { 'use strict'; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Janis * * This file contains one class Janis, which in charge of communication between * botpress and janis. * */ var Janis = function () { function Janis(bp, config) { _classCallCheck(this, Janis); if (!bp || !config) { throw new Error('You need to specify botpress and config'); } this.setConfig(config); this.app = bp.getRouter('botpress-janis', { 'bodyParser.json': false, 'auth': function auth(req) { return !/\/webhook/i.test(req.originalUrl); } }); this.janis = __webpack_require__(6)(config.apiKey, config.clientKey); this.janis.on('chat response', function (msg) { var platform = "facebook"; var platformModule; if (/[a-zA-Z]+/.test(msg.channel)) { platform = "slack"; platformModule = bp.slack; } else { platformModule = bp.messenger; } var message = {}; if (msg.attachments) { if (platformModule.createText) { message = platformModule.createAttachments(msg.channel, msg.attachments); } } else if (platform == "facebook" && platformModule.createAttachment && msg.attachment) { if (msg.attachment.type == "template") { message = platformModule.createTemplate(msg.channel, msg.attachment.payload); } else if (msg.attachment.payload) { message = platformModule.createAttachment(msg.channel, msg.attachment.type, msg.attachment.payload.url); } } else if (msg.text.length > 0 && platformModule.createText) { message = platformModule.createText(msg.channel, msg.text); } message.raw.slack_user = msg.slack_user; message.raw.ts = msg.ts; bp.events.emit('janis.chat_response', msg); bp.middlewares.sendOutgoing(message); }); } _createClass(Janis, [{ key: 'setConfig', value: function setConfig(config) { this.config = config; } }, { key: 'getConfig', value: function getConfig() { return this.config; } }, { key: 'hopIn', value: function hopIn(platform, message, user) { this.janis.setPlatform(platform); if (user) { message.user_profile = user; } return this.janis.hopIn(message); } }, { key: 'hopOut', value: function hopOut(platform, message) { this.janis.setPlatform(platform); return this.janis.hopOut(message); } }, { key: 'logUnkownIntent', value: function logUnkownIntent(platform, message) { this.janis.setPlatform(platform); return this.janis.logUnkownIntent(message); } }, { key: 'assistanceRequested', value: function assistanceRequested(platform, message) { this.janis.setPlatform(platform); return this.janis.assistanceRequested(message); } }, { key: 'module', value: function module(factory) { return factory.apply(this, [this]); } }]); return Janis; }(); module.exports = Janis; /***/ }), /* 6 */ /***/ (function(module, exports) { module.exports = require("janis"); /***/ }) /******/ ]); //# sourceMappingURL=node.bundle.js.map