UNPKG

hubot-ringcentral

Version:

Hubot adapter to use with RingCentral Team Messaging

281 lines (235 loc) 10.3 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] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = 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; /******/ /******/ // identity function for calling harmony imports with the correct context /******/ __webpack_require__.i = function(value) { return value; }; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { /******/ configurable: false, /******/ enumerable: true, /******/ get: getter /******/ }); /******/ } /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 7); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (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; }; }(); var _fs = __webpack_require__(2); var _fs2 = _interopRequireDefault(_fs); var _ringcentralJsConcise = __webpack_require__(5); var _ringcentralJsConcise2 = _interopRequireDefault(_ringcentralJsConcise); var _pubnubEs = __webpack_require__(6); var _pubnubEs2 = _interopRequireDefault(_pubnubEs); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(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 Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } var hubot = null; try { hubot = __webpack_require__(3); } catch (_) { var prequire = __webpack_require__(4); hubot = prequire('hubot'); } var _hubot = hubot, Adapter = _hubot.Adapter, TextMessage = _hubot.TextMessage, User = _hubot.User; var RingCentralAdapter = function (_Adapter) { _inherits(RingCentralAdapter, _Adapter); function RingCentralAdapter(robot) { var _this2 = this; _classCallCheck(this, RingCentralAdapter); var _this = _possibleConstructorReturn(this, (RingCentralAdapter.__proto__ || Object.getPrototypeOf(RingCentralAdapter)).call(this, robot)); _this.rc = new _ringcentralJsConcise2.default(process.env.RINGCENTRAL_CLIENT_ID, process.env.RINGCENTRAL_CLIENT_SECRET, process.env.RINGCENTRAL_SERVER || _ringcentralJsConcise2.default.PRODUCTION_SERVER); if (_fs2.default.existsSync('./token.json')) { var data = _fs2.default.readFileSync('./token.json', 'utf8'); _this.rc.token(JSON.parse(data)); _this.robot.logger.info('Token restored from file'); _this.subscribe(); } else { _this.robot.logger.error('No saved token detected. You need to add the bot to RingCentral first.'); } _this.robot.router.post('/oauth', function (req, res) { _this.rc.token(req.body); _fs2.default.writeFileSync('./token.json', JSON.stringify(_this.rc.token(), null, 2)); _this.subscribe(); res.send(''); }); _this.robot.router.get('/oauth', function () { var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(req, res) { return regeneratorRuntime.wrap(function _callee$(_context) { while (1) { switch (_context.prev = _context.next) { case 0: _this.robot.logger.info(req.query.code); _context.next = 3; return _this.rc.authorize({ code: req.query.code, redirectUri: process.env.RINGCENTRAL_BOT_SERVER + '/oauth' }); case 3: _fs2.default.writeFileSync('./token.json', JSON.stringify(_this.rc.token(), null, 2)); _this.subscribe(); res.send(''); case 6: case 'end': return _context.stop(); } } }, _callee, _this2); })); return function (_x, _x2) { return _ref.apply(this, arguments); }; }()); return _this; } _createClass(RingCentralAdapter, [{ key: 'subscribe', value: function subscribe() { var _this3 = this; var pubnub = new _pubnubEs2.default(this.rc, ['/restapi/v1.0/glip/posts'], function (message) { _this3.robot.logger.info(JSON.stringify(message, null, 2)); var post = message.body; if (post.eventType === 'PostAdded' && post.text && post.text !== '') { var user = new User(post.creatorId, { room: post.groupId, reply_to: post.groupId, name: 'User ' + post.creatorId + ' from Group ' + post.groupId }); var hubotMessage = new TextMessage(user, post.text, 'MSG-' + post.id); _this3.robot.receive(hubotMessage); } }); pubnub.subscribe(); this.robot.logger.info('Subscription created'); } }, { key: 'send', value: function send(envelope) { var textToSend = arguments.length <= 1 ? undefined : arguments[1]; var attachmentsToSend = arguments.length <= 2 ? undefined : arguments[2]; var dataToSend = { groupId: envelope.user.reply_to }; if (textToSend !== undefined) { dataToSend.text = textToSend; } if (attachmentsToSend !== undefined) { dataToSend.attachments = attachmentsToSend; } this.robot.logger.info('send ' + JSON.stringify(envelope, null, 4) + '\n\n object:' + JSON.stringify(dataToSend, null, 4)); this.rc.post('/restapi/v1.0/glip/posts', dataToSend); } }, { key: 'reply', value: function reply(envelope) { var string = arguments.length <= 1 ? undefined : arguments[1]; this.robot.logger.info('reply ' + JSON.stringify(envelope, null, 4) + '\n\n' + string); this.rc.post('/restapi/v1.0/glip/posts', { groupId: envelope.user.reply_to, text: string }); } }, { key: 'run', value: function run() { this.robot.logger.info('Run'); this.emit('connected'); } }]); return RingCentralAdapter; }(Adapter); exports.use = function (robot) { return new RingCentralAdapter(robot); }; /***/ }), /* 1 */ /***/ (function(module, exports) { module.exports = require("babel-polyfill"); /***/ }), /* 2 */ /***/ (function(module, exports) { module.exports = require("fs"); /***/ }), /* 3 */ /***/ (function(module, exports) { module.exports = require("hubot"); /***/ }), /* 4 */ /***/ (function(module, exports) { module.exports = require("parent-require"); /***/ }), /* 5 */ /***/ (function(module, exports) { module.exports = require("ringcentral-js-concise"); /***/ }), /* 6 */ /***/ (function(module, exports) { module.exports = require("ringcentral-js-concise/src/pubnub.es5.js"); /***/ }), /* 7 */ /***/ (function(module, exports, __webpack_require__) { __webpack_require__(1); module.exports = __webpack_require__(0); /***/ }) /******/ ]);