yelda-webchat 1.1.12 → 1.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -72,21 +72,22 @@ We don't advise you to download a local version without using the NPM package :
72
72
 
73
73
  Initialize the lib with the Yelda provided elements
74
74
 
75
- | Name | Type | Default | Description |
76
- | ----------------- | ------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
77
- | assistantSlug | String | | The assistant short name on Yelda **Required** |
78
- | assistantId | String | | The assistant id on Yelda **Required** |
79
- | assistantUrl | String | `https://app.yelda.ai` |
80
- | chatPath | String | `/chat` |
81
- | locale | String | `fr_FR` |
82
- | isAdmin | Boolean | `false` | Set to `true` only to access the conversations analysis |
83
- | shouldBeOpened | Boolean | `false` | Set to `true` to open the chat popin on load. With default `false` value, the webchat is closed and the user only see the assistant image or the start button |
84
- | parentContainerId | String | | DOM element id where you want to add the iframe. If the 'container_id' does not exist in the DOM, the iframe will be automatically inserted into the document body |
85
- | bubbleContainerChildId | String | | Child DOM element id where you want to add bubble element. If the 'bubbleContainerChildId' does not exist, **the chat won't be loaded** |
86
- | isStartBtn | Boolean | `false` | Set to `true` only if need to show 'start' button. ⚠️ This is an admin feature. It can only work if isAdmin is also set to `true` |
87
- | canBeClosed | Boolean | `true` | It is used to toggle the visible state of close button. ⚠️ If parentContainerId is provided and canBeClosed is not provided, canBeClosed is set to `false` |
88
- | isDemo | Boolean | `false` | Set to `true` to flag the conversation as "demo" in conversations list and training inbox and force the webchat to be opened on page load, even if shouldBeOpened is set to false |
89
- | yparam | String | `null` | If the iframe's parent URL contains "yparam" parameter, then yparam value will be passed as the settings. yparam value "yshowbot" will force the webchat to be opened on page load, even if shouldBeOpened is set to false|
75
+ | Name | Type | Default | Description |
76
+ | ----------------- | ------- | ---------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
77
+ | assistantSlug | String | | The assistant short name on Yelda **Required** |
78
+ | assistantId | String | | The assistant id on Yelda **Required** |
79
+ | assistantUrl | String | `https://app.yelda.ai` |
80
+ | chatPath | String | `/chat` |
81
+ | locale | String | `fr_FR` |
82
+ | isAdmin | Boolean | `false` | Set to `true` only to access the conversations analysis |
83
+ | shouldBeOpened | Boolean | `false` | Set to `true` to open the chat popin on load. With default `false` value, the webchat is closed and the user only see the assistant image or the start button |
84
+ | parentContainerId | String | | DOM element id where you want to add the iframe. If the 'container_id' does not exist in the DOM, the iframe will be automatically inserted into the document body |
85
+ | bubbleContainerChildId | String | | Child DOM element id where you want to add bubble element. If the 'bubbleContainerChildId' does not exist, **the chat won't be loaded** |
86
+ | isStartBtn | Boolean | `false` | Set to `true` only if need to show 'start' button. ⚠️ This is an admin feature. It can only work if isAdmin is also set to `true` |
87
+ | canBeClosed | Boolean | `true` | It is used to toggle the visible state of close button. ⚠️ If parentContainerId is provided and canBeClosed is not provided, canBeClosed is set to `false` |
88
+ | isDemo | Boolean | `false` | Set to `true` to flag the conversation as "demo" in conversations list and training inbox and force the webchat to be opened on page load, even if shouldBeOpened is set to false |
89
+ | yparam | String | `null` | If the iframe's parent URL contains "yparam" parameter, then yparam value will be passed as the settings. yparam value "yshowbot" will force the webchat to be opened on page load, even if shouldBeOpened is set to false|
90
+ | env | String | | Force an env value. It is used to get the webchat settings from Redis. |
90
91
 
91
92
  ```javascript
92
93
  yeldaChat.init({
@@ -1610,25 +1610,67 @@ var YeldaChat = function () {
1610
1610
  return isFound;
1611
1611
  }
1612
1612
 
1613
+ /**
1614
+ * Get the app env from the assistant url
1615
+ * @param {String} assistantUrl
1616
+ * @returns {String} env
1617
+ */
1618
+
1619
+ }, {
1620
+ key: 'getAppEnv',
1621
+ value: function getAppEnv(assistantUrl) {
1622
+ if (_config__WEBPACK_IMPORTED_MODULE_14___default.a.REGEX_HOST.STAGING.test(assistantUrl)) {
1623
+ return _config__WEBPACK_IMPORTED_MODULE_14___default.a.APP_ENV_VALUES.STAGING;
1624
+ }
1625
+
1626
+ // By default, use the production env
1627
+ return _config__WEBPACK_IMPORTED_MODULE_14___default.a.APP_ENV_VALUES.PRODUCTION;
1628
+ }
1629
+
1630
+ /**
1631
+ * Get the chat bubble url base based on isFallback
1632
+ * @param {Object} data { assistantUrl, assistantId, settingId }
1633
+ * @param {Boolean} isFallback fallback means yelda endpoint
1634
+ * @returns {String} url base
1635
+ */
1636
+
1637
+ }, {
1638
+ key: 'getChatBubbleUrlBase',
1639
+ value: function getChatBubbleUrlBase(data, isFallback) {
1640
+ if (isFallback) {
1641
+ return data.assistantUrl + '/assistants';
1642
+ }
1643
+ return _config__WEBPACK_IMPORTED_MODULE_14___default.a.CHAT_BUBBLE_EXTERNAL_ENDPOINT;
1644
+ }
1645
+
1613
1646
  /**
1614
1647
  * return getAssistantSettings endpoint URL from data
1615
1648
  * @param {Object} data { assistantUrl, assistantId, settingId }
1649
+ * @param {Boolean} isFallback
1616
1650
  * @return {String} url
1617
1651
  */
1618
1652
 
1619
1653
  }, {
1620
1654
  key: 'getAssistantSettingsUrl',
1621
- value: function getAssistantSettingsUrl(data) {
1655
+ value: function getAssistantSettingsUrl(data, isFallback) {
1656
+ var urlBase = this.getChatBubbleUrlBase(data, isFallback);
1657
+ // The app env is required as query for the external chatbubble endpoint
1658
+ // If the env was not explicitly set in data, guess it from the assistant url
1659
+ var appEnv = data.env || this.getAppEnv(data.assistantUrl);
1660
+ var envQuery = 'env=' + appEnv;
1622
1661
  if (data.settingId) {
1623
- return data.assistantUrl + '/assistants/settings/' + data.settingId + '/chatBubble';
1662
+ return urlBase + '/settings/' + data.settingId + '/chatBubble?' + envQuery;
1624
1663
  }
1625
1664
 
1626
- return data.assistantUrl + '/assistants/' + data.assistantId + '/chatBubble/' + data.locale;
1665
+ return urlBase + '/' + data.assistantId + '/chatBubble/' + data.locale + '?' + envQuery;
1627
1666
  }
1628
1667
 
1629
1668
  /**
1630
1669
  * Update assistantImage with assistant settings from backend if any
1631
1670
  * @param {Object} data { assistantUrl, assistantId, settingId }
1671
+ * @param {Object} options
1672
+ * @param {Object} options.isFallback
1673
+ * @param {Number} options.customTimeout in milliseconds
1632
1674
  * @return {Promise}
1633
1675
  */
1634
1676
 
@@ -1637,19 +1679,59 @@ var YeldaChat = function () {
1637
1679
  value: function getAssistantSettings(data) {
1638
1680
  var _this3 = this;
1639
1681
 
1682
+ var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
1683
+ _ref2$isFallback = _ref2.isFallback,
1684
+ isFallback = _ref2$isFallback === undefined ? false : _ref2$isFallback,
1685
+ _ref2$customTimeout = _ref2.customTimeout,
1686
+ customTimeout = _ref2$customTimeout === undefined ? null : _ref2$customTimeout;
1687
+
1640
1688
  return new babel_runtime_core_js_promise__WEBPACK_IMPORTED_MODULE_2___default.a(function (resolve, reject) {
1641
1689
  try {
1642
1690
  var xhr = new XMLHttpRequest();
1643
- var url = _this3.getAssistantSettingsUrl(data);
1691
+
1692
+ // Get the url base depending on if we are doing the basic request (external chatbubble endpoint)
1693
+ // or the fallback one (chatbubble endpoint in Yelda)
1694
+ var url = _this3.getAssistantSettingsUrl(data, isFallback);
1644
1695
 
1645
1696
  xhr.open('GET', url);
1697
+ if (customTimeout) {
1698
+ xhr.timeout = customTimeout;
1699
+ }
1646
1700
  xhr.send();
1647
1701
 
1648
1702
  // Bind and call are necessary to pass the "this" to the callback function
1649
1703
  xhr.onreadystatechange = function () {
1650
1704
  if (xhr.readyState === 4) {
1651
- var webchatSettings = xhr.responseText ? JSON.parse(xhr.responseText).data : null;
1652
- resolve(webchatSettings);
1705
+ var responseText = xhr.responseText ? JSON.parse(xhr.responseText) : null;
1706
+ // Yelda endpoint returns { data: ...webchatSettings } (to be compatible with old versions of the chat-plugin)
1707
+ // whereas the external endpoint returns { ...webchatSettings }
1708
+ // We need to handle both format
1709
+ var webchatSettings = responseText && (responseText.data || responseText);
1710
+
1711
+ if (webchatSettings) {
1712
+ return resolve(webchatSettings);
1713
+ }
1714
+
1715
+ // We did not received the expected webchatSettings
1716
+ // If we are already in fallback mode, it means there is no webchat settings for the given init information
1717
+ if (isFallback) {
1718
+ return resolve(null);
1719
+ }
1720
+
1721
+ // If the initial request do not return webchatSettings, it might be because they were not set in Redis yet
1722
+ // So let's call the yelda endpoint (fallback) that will find the setting (if it exists) and set it in Redis
1723
+ return resolve(_this3.getAssistantSettings(data, { isFallback: true }));
1724
+ }
1725
+ };
1726
+
1727
+ xhr.ontimeout = function () {
1728
+ /*
1729
+ It the initial request to the external chatbubble endpoint timed out, (maybe the endpoint is down)
1730
+ let's fallback on the yelda endpoint
1731
+ */
1732
+ if (!isFallback) {
1733
+ // Set isFallback to true to be sure to not end up in an infinite loop
1734
+ return resolve(_this3.getAssistantSettings(data, { isFallback: true }));
1653
1735
  }
1654
1736
  };
1655
1737
  } catch (e) {
@@ -1670,7 +1752,7 @@ var YeldaChat = function () {
1670
1752
  var _this4 = this;
1671
1753
 
1672
1754
  return new babel_runtime_core_js_promise__WEBPACK_IMPORTED_MODULE_2___default.a(function () {
1673
- var _ref2 = babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_1___default()( /*#__PURE__*/babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.mark(function _callee2(resolve) {
1755
+ var _ref3 = babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_1___default()( /*#__PURE__*/babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.mark(function _callee2(resolve) {
1674
1756
  return babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.wrap(function _callee2$(_context2) {
1675
1757
  while (1) {
1676
1758
  switch (_context2.prev = _context2.next) {
@@ -1709,7 +1791,7 @@ var YeldaChat = function () {
1709
1791
  // Get assistant settings from backend
1710
1792
  _context2.prev = 12;
1711
1793
  _context2.next = 15;
1712
- return _this4.getAssistantSettings(data);
1794
+ return _this4.getAssistantSettings(data, { customTimeout: _config__WEBPACK_IMPORTED_MODULE_14___default.a.CHAT_BUBBLE_REQUEST_TIMEOUT });
1713
1795
 
1714
1796
  case 15:
1715
1797
  _this4.webchatSettings = _context2.sent;
@@ -1746,8 +1828,8 @@ var YeldaChat = function () {
1746
1828
  }, _callee2, _this4, [[12, 19]]);
1747
1829
  }));
1748
1830
 
1749
- return function (_x7) {
1750
- return _ref2.apply(this, arguments);
1831
+ return function (_x8) {
1832
+ return _ref3.apply(this, arguments);
1751
1833
  };
1752
1834
  }());
1753
1835
  }
@@ -1815,10 +1897,10 @@ var YeldaChat = function () {
1815
1897
 
1816
1898
  }, {
1817
1899
  key: 'isDataOutdated',
1818
- value: function isDataOutdated(data, _ref3) {
1819
- var settingId = _ref3.settingId,
1820
- assistantId = _ref3.assistantId,
1821
- locale = _ref3.locale;
1900
+ value: function isDataOutdated(data, _ref4) {
1901
+ var settingId = _ref4.settingId,
1902
+ assistantId = _ref4.assistantId,
1903
+ locale = _ref4.locale;
1822
1904
 
1823
1905
  if (data.settingId && data.settingId !== settingId) {
1824
1906
  return true;
@@ -1935,7 +2017,7 @@ var YeldaChat = function () {
1935
2017
  var _this5 = this;
1936
2018
 
1937
2019
  return new babel_runtime_core_js_promise__WEBPACK_IMPORTED_MODULE_2___default.a(function () {
1938
- var _ref4 = babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_1___default()( /*#__PURE__*/babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.mark(function _callee4(resolve) {
2020
+ var _ref5 = babel_runtime_helpers_asyncToGenerator__WEBPACK_IMPORTED_MODULE_1___default()( /*#__PURE__*/babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.mark(function _callee4(resolve) {
1939
2021
  return babel_runtime_regenerator__WEBPACK_IMPORTED_MODULE_0___default.a.wrap(function _callee4$(_context4) {
1940
2022
  while (1) {
1941
2023
  switch (_context4.prev = _context4.next) {
@@ -1989,8 +2071,8 @@ var YeldaChat = function () {
1989
2071
  }, _callee4, _this5);
1990
2072
  }));
1991
2073
 
1992
- return function (_x8) {
1993
- return _ref4.apply(this, arguments);
2074
+ return function (_x9) {
2075
+ return _ref5.apply(this, arguments);
1994
2076
  };
1995
2077
  }());
1996
2078
  }
@@ -2626,6 +2708,12 @@ module.exports = {
2626
2708
  ADD_BUBBLE_TEXT: 'addBubbleText',
2627
2709
  ADD_MINIMAL_NOTIFICATION_TEXT: 'addMinimalNotificationText'
2628
2710
  }
2711
+ },
2712
+ CHAT_BUBBLE_EXTERNAL_ENDPOINT: 'https://webchat.yelda.ai/webchat',
2713
+ CHAT_BUBBLE_REQUEST_TIMEOUT: 5000,
2714
+ APP_ENV_VALUES: {
2715
+ STAGING: 'staging',
2716
+ PRODUCTION: 'production'
2629
2717
  }
2630
2718
  };
2631
2719