wingbot 3.38.0-alpha.3 → 3.38.0-alpha.4

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.38.0-alpha.3",
3
+ "version": "3.38.0-alpha.4",
4
4
  "description": "Enterprise Messaging Bot Conversation Engine",
5
5
  "main": "index.js",
6
6
  "scripts": {
package/src/Tester.js CHANGED
@@ -360,6 +360,20 @@ class Tester {
360
360
  this.storage.saveState(stateObj);
361
361
  }
362
362
 
363
+ /**
364
+ * Assert, that state contains a subset of provided value
365
+ *
366
+ * @param {object} object
367
+ * @example
368
+ *
369
+ * t.stateContains({ value: true });
370
+ */
371
+ stateContains (object) {
372
+ const { state } = this.getState();
373
+
374
+ assert.deepEqual(state, { ...state, ...object }, 'Conversation state equals');
375
+ }
376
+
363
377
  /**
364
378
  * Makes text request
365
379
  *
@@ -96,11 +96,7 @@ function getSetState (setState, req, res = null, useState = null, configuration
96
96
  .filter((k) => k !== '_');
97
97
 
98
98
  let obj = {};
99
- let state = {
100
- ...req.state,
101
- ...(res ? res.newState : {}),
102
- ...(useState || {})
103
- };
99
+ let state = stateData(req, res, configuration, useState);
104
100
 
105
101
  keys.forEach((k) => {
106
102
  const val = setState[k];
@@ -179,7 +175,7 @@ function getSetState (setState, req, res = null, useState = null, configuration
179
175
  values = [];
180
176
  } else {
181
177
  const useValue = typeof value === 'string'
182
- ? handlebars.compile(value)(stateData(req, res, configuration))
178
+ ? handlebars.compile(value)(state)
183
179
  .split(/(?<!\\),/g)
184
180
  .map((v) => v.replace(/\\,/g, ',').trim())
185
181
  : value;
@@ -205,7 +201,7 @@ function getSetState (setState, req, res = null, useState = null, configuration
205
201
  set = val;
206
202
  }
207
203
  } else if (typeof val === 'string') {
208
- set = handlebars.compile(val)(stateData(req, res, configuration));
204
+ set = handlebars.compile(val)(state);
209
205
  } else if (val === null
210
206
  || SCALAR_TYPES.includes(typeof val)) {
211
207
  set = val;
@@ -69,6 +69,16 @@ function makeExpectedKeyword (
69
69
 
70
70
  /** @typedef {import('../Responder').QuickReply} QuickReply */
71
71
 
72
+ const THIS_REGEX = /\{\{\$this\}\}/g;
73
+
74
+ function hasThis (val) {
75
+ return typeof val === 'string' && val.match(THIS_REGEX);
76
+ }
77
+
78
+ function replaceThis (val, title) {
79
+ return val.replace(THIS_REGEX, title);
80
+ }
81
+
72
82
  /**
73
83
  *
74
84
  * @ignore
@@ -208,6 +218,25 @@ function makeQuickReplies (replies, path = '', translate = (w) => w, quickReplyC
208
218
 
209
219
  const hasData = Object.keys(data).length !== 0;
210
220
  const hasSetState = setState && Object.keys(setState).length !== 0;
221
+ const translatedTitle = translate(title);
222
+
223
+ if (hasSetState) {
224
+ // replace {{this}}
225
+ Object.entries(setState)
226
+ .forEach(([key, val]) => {
227
+ if (typeof val === 'object' && val) {
228
+ Object.entries(val)
229
+ .forEach(([k, v]) => {
230
+ if (k.match(/^_\$/) && hasThis(v)) {
231
+ // eslint-disable-next-line no-param-reassign
232
+ val[k] = replaceThis(v, translatedTitle);
233
+ }
234
+ });
235
+ } else if (hasThis(val)) {
236
+ setState[key] = replaceThis(val, translatedTitle);
237
+ }
238
+ });
239
+ }
211
240
 
212
241
  if (data._senderMeta
213
242
  && data._senderMeta.flag === FLAG_DISAMBIGUATION_SELECTED) {
@@ -229,7 +258,6 @@ function makeQuickReplies (replies, path = '', translate = (w) => w, quickReplyC
229
258
  payload = JSON.stringify(payload);
230
259
  }
231
260
 
232
- const translatedTitle = translate(title);
233
261
  const translatedAiTitle = typeof aiTitle === 'string' ? translate(aiTitle) : aiTitle;
234
262
  const expect = makeExpectedKeyword(
235
263
  absoluteAction,
@@ -11,18 +11,23 @@
11
11
  * @param {Request} req
12
12
  * @param {Responder} res
13
13
  * @param {object} configuration
14
+ * @param {object} [stateOverride]
14
15
  * @returns {object}
15
16
  */
16
- module.exports = function stateData (req, res = null, configuration = null) {
17
+ module.exports = function stateData (req, res = null, configuration = null, stateOverride = {}) {
17
18
  const c = configuration || req.configuration;
18
19
 
20
+ const $this = req.text();
21
+
19
22
  return {
20
23
  c,
21
24
  configuration: c,
22
25
  ...req.state,
23
26
  ...(res ? res.newState : {}),
27
+ ...stateOverride,
28
+ $this,
24
29
  ...req.actionData(),
25
30
  ...(res ? res.data : {}),
26
- $input: req.text()
31
+ $input: $this
27
32
  };
28
33
  };