wingbot 3.25.0 → 3.26.0-alpha.3
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 +1 -1
- package/src/BotApp.js +18 -1
- package/src/BotAppSender.js +33 -2
- package/src/CallbackAuditLog.js +0 -5
- package/src/graphApi/validateBotApi.js +1 -1
- package/src/utils/getUpdate.js +1 -1
package/package.json
CHANGED
package/src/BotApp.js
CHANGED
|
@@ -22,6 +22,7 @@ const DEFAULT_API_URL = 'https://orchestrator-api.wingbot.ai';
|
|
|
22
22
|
/** @typedef {import('./Processor').Plugin} Plugin */
|
|
23
23
|
|
|
24
24
|
/** @typedef {import('./CallbackAuditLog')} AuditLog */
|
|
25
|
+
/** @typedef {import('./BotAppSender').TlsOptions} TlsOptions */
|
|
25
26
|
|
|
26
27
|
/**
|
|
27
28
|
* @typedef {object} BotAppOptions
|
|
@@ -32,6 +33,7 @@ const DEFAULT_API_URL = 'https://orchestrator-api.wingbot.ai';
|
|
|
32
33
|
* @prop {ChatLogStorage} [chatLogStorage]
|
|
33
34
|
* @prop {boolean} [preferSynchronousResponse]
|
|
34
35
|
* @prop {AuditLog} [auditLog]
|
|
36
|
+
* @prop {TlsOptions} [tls]
|
|
35
37
|
*
|
|
36
38
|
* @typedef {ProcessorOptions & BotAppOptions} Options
|
|
37
39
|
*/
|
|
@@ -63,6 +65,7 @@ class BotApp {
|
|
|
63
65
|
appId = null,
|
|
64
66
|
preferSynchronousResponse = false,
|
|
65
67
|
auditLog = null,
|
|
68
|
+
tls = null,
|
|
66
69
|
...processorOptions
|
|
67
70
|
} = options;
|
|
68
71
|
|
|
@@ -70,6 +73,7 @@ class BotApp {
|
|
|
70
73
|
this._fetch = fetch; // mock
|
|
71
74
|
this._appId = appId;
|
|
72
75
|
this._auditLog = auditLog;
|
|
76
|
+
this._tls = tls;
|
|
73
77
|
|
|
74
78
|
let { apiUrl } = options;
|
|
75
79
|
|
|
@@ -96,6 +100,18 @@ class BotApp {
|
|
|
96
100
|
this._preferSynchronousResponse = preferSynchronousResponse;
|
|
97
101
|
}
|
|
98
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Get authorization token for wingbot orchestrator
|
|
105
|
+
*
|
|
106
|
+
* @param {string} body
|
|
107
|
+
* @param {string} secret
|
|
108
|
+
* @param {string} appId
|
|
109
|
+
* @returns {Promise<string>}
|
|
110
|
+
*/
|
|
111
|
+
static signBody (body, secret, appId) {
|
|
112
|
+
return BotAppSender.signBody(body, secret, appId);
|
|
113
|
+
}
|
|
114
|
+
|
|
99
115
|
/**
|
|
100
116
|
* Returns processor plugin, which updates thread context automatically
|
|
101
117
|
*
|
|
@@ -211,7 +227,8 @@ class BotApp {
|
|
|
211
227
|
appId,
|
|
212
228
|
secret,
|
|
213
229
|
mid,
|
|
214
|
-
fetch: this._fetch
|
|
230
|
+
fetch: this._fetch,
|
|
231
|
+
tls: this._tls
|
|
215
232
|
};
|
|
216
233
|
|
|
217
234
|
const sender = new BotAppSender(options, senderId, message, this._senderLogger);
|
package/src/BotAppSender.js
CHANGED
|
@@ -6,17 +6,25 @@
|
|
|
6
6
|
const jwt = require('jsonwebtoken');
|
|
7
7
|
const { default: fetch, Headers } = require('node-fetch');
|
|
8
8
|
const crypto = require('crypto');
|
|
9
|
+
const { Agent } = require('https');
|
|
9
10
|
const { promisify } = require('util');
|
|
10
11
|
const ReturnSender = require('./ReturnSender');
|
|
11
12
|
|
|
12
13
|
/** @typedef {import('./ReturnSender').ReturnSenderOptions} ReturnSenderOptions */
|
|
13
14
|
/** @typedef {import('./ReturnSender').ChatLogStorage} ChatLogStorage */
|
|
14
15
|
|
|
16
|
+
/**
|
|
17
|
+
* @typedef {object} TlsOptions
|
|
18
|
+
* @prop {string|Promise<string>} key
|
|
19
|
+
* @prop {string|Promise<string>} cert
|
|
20
|
+
*/
|
|
21
|
+
|
|
15
22
|
/**
|
|
16
23
|
* @typedef {object} BotAppSenderOptions
|
|
17
24
|
* @prop {string} apiUrl
|
|
18
25
|
* @prop {string} pageId
|
|
19
26
|
* @prop {string} appId
|
|
27
|
+
* @prop {TlsOptions} [tls]
|
|
20
28
|
* @prop {string} [mid]
|
|
21
29
|
* @prop {Function} [fetch]
|
|
22
30
|
* @prop {string|Promise<string>} secret
|
|
@@ -47,6 +55,9 @@ class BotAppSender extends ReturnSender {
|
|
|
47
55
|
|
|
48
56
|
this._secret = Promise.resolve(options.secret);
|
|
49
57
|
|
|
58
|
+
this._tls = options.tls;
|
|
59
|
+
this._agent = null;
|
|
60
|
+
|
|
50
61
|
this._fetch = options.fetch || fetch;
|
|
51
62
|
}
|
|
52
63
|
|
|
@@ -65,6 +76,21 @@ class BotAppSender extends ReturnSender {
|
|
|
65
76
|
}, goodSecret);
|
|
66
77
|
}
|
|
67
78
|
|
|
79
|
+
async _getAgent () {
|
|
80
|
+
if (!this._tls) {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
if (!this._agent) {
|
|
84
|
+
const [key, cert] = await Promise.all([
|
|
85
|
+
Promise.resolve(this._tls.key), Promise.resolve(this._tls.cert)
|
|
86
|
+
]);
|
|
87
|
+
|
|
88
|
+
this._agent = new Agent({ cert, key });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
return this._agent;
|
|
92
|
+
}
|
|
93
|
+
|
|
68
94
|
async _send (payload) {
|
|
69
95
|
// attach sender
|
|
70
96
|
if (typeof payload.sender === 'undefined') {
|
|
@@ -78,14 +104,19 @@ class BotAppSender extends ReturnSender {
|
|
|
78
104
|
|
|
79
105
|
const body = JSON.stringify(payload);
|
|
80
106
|
|
|
81
|
-
const token = await
|
|
107
|
+
const [token, agent] = await Promise.all([
|
|
108
|
+
BotAppSender.signBody(body, this._secret, this._appId),
|
|
109
|
+
this._getAgent()
|
|
110
|
+
]);
|
|
82
111
|
|
|
83
112
|
const headers = new Headers();
|
|
84
113
|
|
|
85
114
|
headers.set('Authorization', token);
|
|
86
115
|
headers.set('Content-Type', 'application/json');
|
|
87
116
|
|
|
88
|
-
const response = await this._fetch(this._apiUrl, {
|
|
117
|
+
const response = await this._fetch(this._apiUrl, {
|
|
118
|
+
headers, body, agent, method: 'POST'
|
|
119
|
+
})
|
|
89
120
|
.then((r) => r.json());
|
|
90
121
|
|
|
91
122
|
const { request, errors = null } = response;
|
package/src/CallbackAuditLog.js
CHANGED
|
@@ -99,7 +99,7 @@ function validateBotApi (botFactory, postBackTest = null, textTest = null, acl =
|
|
|
99
99
|
|
|
100
100
|
const bot = botFactory();
|
|
101
101
|
|
|
102
|
-
await ctx.audit('validateBot'
|
|
102
|
+
await ctx.audit('validateBot');
|
|
103
103
|
return validate(bot, validationRequestBody, postBackTest, textTest);
|
|
104
104
|
}
|
|
105
105
|
};
|
package/src/utils/getUpdate.js
CHANGED
|
@@ -179,7 +179,7 @@ function getSetState (setState = {}, req, res = null, useState = null) {
|
|
|
179
179
|
const useValue = typeof value === 'string'
|
|
180
180
|
? handlebars.compile(value)(stateData(req, res))
|
|
181
181
|
.split(/(?<!\\),/g)
|
|
182
|
-
.map((v) => v.trim())
|
|
182
|
+
.map((v) => v.replace(/\\,/g, ',').trim())
|
|
183
183
|
: value;
|
|
184
184
|
values = toArray(useValue);
|
|
185
185
|
}
|