wingbot 3.45.0 → 3.45.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wingbot",
3
- "version": "3.45.0",
3
+ "version": "3.45.1",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/BotApp.js CHANGED
@@ -207,7 +207,7 @@ class BotApp {
207
207
  }, analyticsStorage);
208
208
 
209
209
  this._eventHandlers.push(onEvent);
210
- this.processor.on('interaction', onInteraction);
210
+ this.processor.onInteraction(onInteraction);
211
211
  return this;
212
212
  }
213
213
 
package/src/Processor.js CHANGED
@@ -58,6 +58,12 @@ const { mergeState, isUserInteraction } = require('./utils/stateVariables');
58
58
  * @prop {TrackingObject} tracking
59
59
  */
60
60
 
61
+ /**
62
+ * @callback IInteractionHandler
63
+ * @param {InteractionEvent} params
64
+ * @returns {Promise|void}
65
+ */
66
+
61
67
  /**
62
68
  * Interaction event fired after every interaction
63
69
  *
@@ -183,6 +189,20 @@ class Processor extends EventEmitter {
183
189
  */
184
190
  this._plugins = [];
185
191
  this._middlewares = [];
192
+
193
+ /** @type {IInteractionHandler[]} */
194
+ this._onInteractionHandlers = [];
195
+ }
196
+
197
+ /**
198
+ * Register asynchronous interaction handler function
199
+ *
200
+ * @param {IInteractionHandler} handler
201
+ * @returns {this}
202
+ */
203
+ onInteraction (handler) {
204
+ this._onInteractionHandlers.push(handler);
205
+ return this;
186
206
  }
187
207
 
188
208
  /**
@@ -400,16 +420,23 @@ class Processor extends EventEmitter {
400
420
  tracking: messageSender.tracking
401
421
  };
402
422
 
403
- return new Promise((resolve) => {
404
- process.nextTick(() => {
405
- try {
406
- this.emit('interaction', event);
407
- } catch (e) {
408
- this.options.log.error('Firing Processor interaction event failed', e);
409
- }
410
- resolve();
411
- });
412
- });
423
+ return Promise.allSettled([
424
+ ...this._onInteractionHandlers
425
+ .map((handler) => Promise.resolve(handler(event))
426
+ .catch((e) => {
427
+ this.options.log.error('Executing Processor interaction event failed', e);
428
+ })),
429
+ new Promise((resolve) => {
430
+ process.nextTick(() => {
431
+ try {
432
+ this.emit('interaction', event);
433
+ } catch (e) {
434
+ this.options.log.error('Firing Processor interaction event failed', e);
435
+ }
436
+ resolve();
437
+ });
438
+ })
439
+ ]);
413
440
  }
414
441
 
415
442
  /**
@@ -752,12 +779,18 @@ class Processor extends EventEmitter {
752
779
  }
753
780
  }
754
781
 
755
- static _createSessionId (pageId, senderId, timestamp = Date.now()) {
756
- const senderHash = crypto.createHash('shake256', { outputLength: 6 })
757
- .update(`${senderId}|${pageId}`)
782
+ static _shakeShort (str, outputLength) {
783
+ const senderHash = crypto.createHash('shake256', { outputLength })
784
+ .update(str)
758
785
  .digest('hex');
759
786
 
760
- const senderShort = parseInt(senderHash, 16).toString(36);
787
+ return senderHash.match(/[a-f0-9]{1,13}/g)
788
+ .map((v) => parseInt(v, 16).toString(36))
789
+ .join('');
790
+ }
791
+
792
+ static _createSessionId (pageId, senderId, timestamp = Date.now()) {
793
+ const senderShort = Processor._shakeShort(`${senderId}|${pageId}}`, 9);
761
794
 
762
795
  const rand = Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)
763
796
  .toString(36);
@@ -768,9 +801,18 @@ class Processor extends EventEmitter {
768
801
  const ts = Math.floor(MAX_TS - timestamp)
769
802
  .toString(36);
770
803
 
804
+ // console.log({
805
+ // base: `${ts}.${senderShort}`.length,
806
+ // ts,
807
+ // senderShort,
808
+ // randTS,
809
+ // rand,
810
+ // randL: rand.length
811
+ // });
812
+
771
813
  return `${ts}.${senderShort}`
772
- .padEnd(21, randTS)
773
- .padEnd(28, rand);
814
+ .padEnd(26, randTS)
815
+ .padEnd(32, rand);
774
816
  }
775
817
 
776
818
  /**
@@ -915,6 +957,6 @@ class Processor extends EventEmitter {
915
957
 
916
958
  }
917
959
 
918
- Processor._createSessionId('p', 's');
960
+ // console.log(Processor._createSessionId('p', 's'));
919
961
 
920
962
  module.exports = Processor;
@@ -7,6 +7,7 @@ const { replaceDiacritics } = require('webalize');
7
7
  const Ai = require('../Ai');
8
8
 
9
9
  /** @typedef {import('../Processor').InteractionEvent} InteractionEvent */
10
+ /** @typedef {import('../Processor').IInteractionHandler} IInteractionHandler */
10
11
  /** @typedef {import('../Request')} Request */
11
12
 
12
13
  /**
@@ -104,12 +105,6 @@ const Ai = require('../Ai');
104
105
  * @prop {UserExtractor} [userExtractor] - text anonymization function
105
106
  */
106
107
 
107
- /**
108
- * @callback IInteractionHandler
109
- * @param {InteractionEvent} params
110
- * @returns {Promise}
111
- */
112
-
113
108
  /**
114
109
  * @typedef {object} Handlers
115
110
  * @prop {IInteractionHandler} onInteraction
@@ -37,7 +37,7 @@ function encodePayload ({ content, contentType, className }) {
37
37
  return {
38
38
  content: Buffer.from(content).toString('base64'),
39
39
  content_type: contentType,
40
- class_name: className
40
+ ...(className && { class_name: className })
41
41
  };
42
42
  }
43
43