@aws-amplify/interactions 4.0.49 → 4.0.50
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/CHANGELOG.md +11 -0
- package/dist/aws-amplify-interactions.js +177 -88
- package/dist/aws-amplify-interactions.js.map +1 -1
- package/dist/aws-amplify-interactions.min.js +1 -1
- package/dist/aws-amplify-interactions.min.js.map +1 -1
- package/lib/Interactions.d.ts +2 -2
- package/lib/Interactions.js +45 -25
- package/lib/Interactions.js.map +1 -1
- package/lib/Providers/AWSLexProvider.d.ts +5 -3
- package/lib/Providers/AWSLexProvider.js +95 -61
- package/lib/Providers/AWSLexProvider.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/types/Provider.d.ts +2 -1
- package/lib/types/Providers/AWSLexProvider.d.ts +10 -0
- package/lib/types/Providers/AWSLexProvider.js +3 -0
- package/lib/types/Providers/AWSLexProvider.js.map +1 -0
- package/lib/types/index.d.ts +1 -0
- package/lib-esm/Interactions.d.ts +2 -2
- package/lib-esm/Interactions.js +45 -25
- package/lib-esm/Interactions.js.map +1 -1
- package/lib-esm/Providers/AWSLexProvider.d.ts +5 -3
- package/lib-esm/Providers/AWSLexProvider.js +95 -61
- package/lib-esm/Providers/AWSLexProvider.js.map +1 -1
- package/lib-esm/index.d.ts +1 -0
- package/lib-esm/index.js +1 -0
- package/lib-esm/index.js.map +1 -1
- package/lib-esm/types/Provider.d.ts +2 -1
- package/lib-esm/types/Providers/AWSLexProvider.d.ts +10 -0
- package/lib-esm/types/Providers/AWSLexProvider.js +1 -0
- package/lib-esm/types/Providers/AWSLexProvider.js.map +1 -0
- package/lib-esm/types/index.d.ts +1 -0
- package/package.json +3 -3
- package/src/Interactions.ts +53 -29
- package/src/Providers/AWSLexProvider.ts +48 -11
- package/src/index.ts +1 -0
- package/src/types/Provider.ts +1 -1
- package/src/types/Providers/AWSLexProvider.ts +11 -0
- package/src/types/index.ts +1 -0
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { AbstractInteractionsProvider } from './InteractionsProvider';
|
|
2
|
-
import { InteractionsOptions, InteractionsResponse, InteractionsMessage } from '../types';
|
|
2
|
+
import { InteractionsOptions, AWSLexProviderOptions, InteractionsResponse, InteractionsMessage } from '../types';
|
|
3
|
+
import { PostTextCommandOutput, PostContentCommandOutput } from '@aws-sdk/client-lex-runtime-service';
|
|
3
4
|
export declare class AWSLexProvider extends AbstractInteractionsProvider {
|
|
4
5
|
private lexRuntimeServiceClient;
|
|
5
6
|
private _botsCompleteCallback;
|
|
6
7
|
constructor(options?: InteractionsOptions);
|
|
7
8
|
getProviderName(): string;
|
|
8
|
-
|
|
9
|
+
configure(config?: AWSLexProviderOptions): AWSLexProviderOptions;
|
|
10
|
+
reportBotStatus(data: PostTextCommandOutput | PostContentCommandOutput, botname: string): void;
|
|
9
11
|
sendMessage(botname: string, message: string | InteractionsMessage): Promise<InteractionsResponse>;
|
|
10
|
-
onComplete(botname: string, callback: any): void;
|
|
12
|
+
onComplete(botname: string, callback: (err: any, confirmation: any) => void): void;
|
|
11
13
|
}
|
|
@@ -1,15 +1,3 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
3
|
-
*
|
|
4
|
-
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
5
|
-
* the License. A copy of the License is located at
|
|
6
|
-
*
|
|
7
|
-
* http://aws.amazon.com/apache2.0/
|
|
8
|
-
*
|
|
9
|
-
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
10
|
-
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
11
|
-
* and limitations under the License.
|
|
12
|
-
*/
|
|
13
1
|
var __extends = (this && this.__extends) || (function () {
|
|
14
2
|
var extendStatics = function (d, b) {
|
|
15
3
|
extendStatics = Object.setPrototypeOf ||
|
|
@@ -70,6 +58,18 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
70
58
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
71
59
|
}
|
|
72
60
|
};
|
|
61
|
+
/*
|
|
62
|
+
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
63
|
+
*
|
|
64
|
+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
|
|
65
|
+
* the License. A copy of the License is located at
|
|
66
|
+
*
|
|
67
|
+
* http://aws.amazon.com/apache2.0/
|
|
68
|
+
*
|
|
69
|
+
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
70
|
+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
71
|
+
* and limitations under the License.
|
|
72
|
+
*/
|
|
73
73
|
import { AbstractInteractionsProvider } from './InteractionsProvider';
|
|
74
74
|
import { LexRuntimeServiceClient, PostTextCommand, PostContentCommand, } from '@aws-sdk/client-lex-runtime-service';
|
|
75
75
|
import { ConsoleLogger as Logger, Credentials, getAmplifyUserAgent, } from '@aws-amplify/core';
|
|
@@ -86,6 +86,18 @@ var AWSLexProvider = /** @class */ (function (_super) {
|
|
|
86
86
|
AWSLexProvider.prototype.getProviderName = function () {
|
|
87
87
|
return 'AWSLexProvider';
|
|
88
88
|
};
|
|
89
|
+
AWSLexProvider.prototype.configure = function (config) {
|
|
90
|
+
if (config === void 0) { config = {}; }
|
|
91
|
+
var propertiesToTest = ['name', 'alias', 'region'];
|
|
92
|
+
Object.keys(config).forEach(function (botKey) {
|
|
93
|
+
var botConfig = config[botKey];
|
|
94
|
+
// is bot config correct
|
|
95
|
+
if (!propertiesToTest.every(function (x) { return x in botConfig; })) {
|
|
96
|
+
throw new Error('invalid bot configuration');
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
return _super.prototype.configure.call(this, config);
|
|
100
|
+
};
|
|
89
101
|
AWSLexProvider.prototype.reportBotStatus = function (data, botname) {
|
|
90
102
|
var _this = this;
|
|
91
103
|
// Check if state is fulfilled to resolve onFullfilment promise
|
|
@@ -114,25 +126,31 @@ var AWSLexProvider = /** @class */ (function (_super) {
|
|
|
114
126
|
};
|
|
115
127
|
AWSLexProvider.prototype.sendMessage = function (botname, message) {
|
|
116
128
|
return __awaiter(this, void 0, void 0, function () {
|
|
117
|
-
var credentials, params, postTextCommand, data, err_1, content, messageType, postContentCommand, data, audioArray, err_2;
|
|
118
|
-
return __generator(this, function (
|
|
119
|
-
switch (
|
|
129
|
+
var credentials, error_1, params, postTextCommand, data, err_1, content, messageType, _a, postContentCommand, data, audioArray, _b, err_2;
|
|
130
|
+
return __generator(this, function (_c) {
|
|
131
|
+
switch (_c.label) {
|
|
120
132
|
case 0:
|
|
133
|
+
// check if bot exists
|
|
121
134
|
if (!this._config[botname]) {
|
|
122
135
|
return [2 /*return*/, Promise.reject('Bot ' + botname + ' does not exist')];
|
|
123
136
|
}
|
|
124
|
-
|
|
137
|
+
_c.label = 1;
|
|
125
138
|
case 1:
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
139
|
+
_c.trys.push([1, 3, , 4]);
|
|
140
|
+
return [4 /*yield*/, Credentials.get()];
|
|
141
|
+
case 2:
|
|
142
|
+
credentials = _c.sent();
|
|
143
|
+
return [3 /*break*/, 4];
|
|
144
|
+
case 3:
|
|
145
|
+
error_1 = _c.sent();
|
|
146
|
+
return [2 /*return*/, Promise.reject('No credentials')];
|
|
147
|
+
case 4:
|
|
130
148
|
this.lexRuntimeServiceClient = new LexRuntimeServiceClient({
|
|
131
149
|
region: this._config[botname].region,
|
|
132
150
|
credentials: credentials,
|
|
133
151
|
customUserAgent: getAmplifyUserAgent(),
|
|
134
152
|
});
|
|
135
|
-
if (!(typeof message === 'string')) return [3 /*break*/,
|
|
153
|
+
if (!(typeof message === 'string')) return [3 /*break*/, 9];
|
|
136
154
|
params = {
|
|
137
155
|
botAlias: this._config[botname].alias,
|
|
138
156
|
botName: botname,
|
|
@@ -140,65 +158,81 @@ var AWSLexProvider = /** @class */ (function (_super) {
|
|
|
140
158
|
userId: credentials.identityId,
|
|
141
159
|
};
|
|
142
160
|
logger.debug('postText to lex', message);
|
|
143
|
-
|
|
144
|
-
case
|
|
145
|
-
|
|
161
|
+
_c.label = 5;
|
|
162
|
+
case 5:
|
|
163
|
+
_c.trys.push([5, 7, , 8]);
|
|
146
164
|
postTextCommand = new PostTextCommand(params);
|
|
147
165
|
return [4 /*yield*/, this.lexRuntimeServiceClient.send(postTextCommand)];
|
|
148
|
-
case
|
|
149
|
-
data =
|
|
166
|
+
case 6:
|
|
167
|
+
data = _c.sent();
|
|
150
168
|
this.reportBotStatus(data, botname);
|
|
151
169
|
return [2 /*return*/, data];
|
|
152
|
-
case
|
|
153
|
-
err_1 =
|
|
170
|
+
case 7:
|
|
171
|
+
err_1 = _c.sent();
|
|
154
172
|
return [2 /*return*/, Promise.reject(err_1)];
|
|
155
|
-
case
|
|
156
|
-
case
|
|
173
|
+
case 8: return [3 /*break*/, 19];
|
|
174
|
+
case 9:
|
|
157
175
|
content = message.content, messageType = message.options.messageType;
|
|
158
|
-
if (messageType === 'voice')
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
176
|
+
if (!(messageType === 'voice')) return [3 /*break*/, 11];
|
|
177
|
+
if (!(content instanceof Blob || content instanceof ReadableStream))
|
|
178
|
+
return [2 /*return*/, Promise.reject('invalid content type')];
|
|
179
|
+
_a = {
|
|
180
|
+
botAlias: this._config[botname].alias,
|
|
181
|
+
botName: botname,
|
|
182
|
+
contentType: 'audio/x-l16; sample-rate=16000; channel-count=1'
|
|
183
|
+
};
|
|
184
|
+
return [4 /*yield*/, convert(content)];
|
|
185
|
+
case 10:
|
|
186
|
+
params = (_a.inputStream = _c.sent(),
|
|
187
|
+
_a.userId = credentials.identityId,
|
|
188
|
+
_a.accept = 'audio/mpeg',
|
|
189
|
+
_a);
|
|
190
|
+
return [3 /*break*/, 12];
|
|
191
|
+
case 11:
|
|
192
|
+
if (typeof content !== 'string')
|
|
193
|
+
return [2 /*return*/, Promise.reject('invalid content type')];
|
|
194
|
+
params = {
|
|
195
|
+
botAlias: this._config[botname].alias,
|
|
196
|
+
botName: botname,
|
|
197
|
+
contentType: 'text/plain; charset=utf-8',
|
|
198
|
+
inputStream: content,
|
|
199
|
+
userId: credentials.identityId,
|
|
200
|
+
accept: 'audio/mpeg',
|
|
201
|
+
};
|
|
202
|
+
_c.label = 12;
|
|
203
|
+
case 12:
|
|
178
204
|
logger.debug('postContent to lex', message);
|
|
179
|
-
|
|
180
|
-
case
|
|
181
|
-
|
|
205
|
+
_c.label = 13;
|
|
206
|
+
case 13:
|
|
207
|
+
_c.trys.push([13, 18, , 19]);
|
|
182
208
|
postContentCommand = new PostContentCommand(params);
|
|
183
209
|
return [4 /*yield*/, this.lexRuntimeServiceClient.send(postContentCommand)];
|
|
184
|
-
case
|
|
185
|
-
data =
|
|
210
|
+
case 14:
|
|
211
|
+
data = _c.sent();
|
|
212
|
+
if (!data.audioStream) return [3 /*break*/, 16];
|
|
186
213
|
return [4 /*yield*/, convert(data.audioStream)];
|
|
187
|
-
case
|
|
188
|
-
|
|
214
|
+
case 15:
|
|
215
|
+
_b = _c.sent();
|
|
216
|
+
return [3 /*break*/, 17];
|
|
217
|
+
case 16:
|
|
218
|
+
_b = undefined;
|
|
219
|
+
_c.label = 17;
|
|
220
|
+
case 17:
|
|
221
|
+
audioArray = _b;
|
|
189
222
|
this.reportBotStatus(data, botname);
|
|
190
223
|
return [2 /*return*/, __assign(__assign({}, data), { audioStream: audioArray })];
|
|
191
|
-
case
|
|
192
|
-
err_2 =
|
|
224
|
+
case 18:
|
|
225
|
+
err_2 = _c.sent();
|
|
193
226
|
return [2 /*return*/, Promise.reject(err_2)];
|
|
194
|
-
case
|
|
227
|
+
case 19: return [2 /*return*/];
|
|
195
228
|
}
|
|
196
229
|
});
|
|
197
230
|
});
|
|
198
231
|
};
|
|
199
232
|
AWSLexProvider.prototype.onComplete = function (botname, callback) {
|
|
233
|
+
// does bot exist
|
|
200
234
|
if (!this._config[botname]) {
|
|
201
|
-
throw new
|
|
235
|
+
throw new Error('Bot ' + botname + ' does not exist');
|
|
202
236
|
}
|
|
203
237
|
this._botsCompleteCallback[botname] = callback;
|
|
204
238
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AWSLexProvider.js","sourceRoot":"","sources":["../../src/Providers/AWSLexProvider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG
|
|
1
|
+
{"version":3,"file":"AWSLexProvider.js","sourceRoot":"","sources":["../../src/Providers/AWSLexProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,4BAA4B,EAAE,MAAM,wBAAwB,CAAC;AAOtE,OAAO,EACN,uBAAuB,EACvB,eAAe,EAGf,kBAAkB,GAGlB,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EACN,aAAa,IAAI,MAAM,EACvB,WAAW,EACX,mBAAmB,GACnB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,OAAO,EAAE,MAAM,gCAAgC,CAAC;AAEzD,IAAM,MAAM,GAAG,IAAI,MAAM,CAAC,gBAAgB,CAAC,CAAC;AAE5C;IAAoC,kCAA4B;IAI/D,wBAAY,OAAiC;QAAjC,wBAAA,EAAA,YAAiC;QAA7C,YACC,kBAAM,OAAO,CAAC,SAEd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D,CAAoC,4BAA4B,GA8K/D"}
|
package/lib-esm/index.d.ts
CHANGED
package/lib-esm/index.js
CHANGED
|
@@ -15,6 +15,7 @@ import { Interactions } from './Interactions';
|
|
|
15
15
|
* @deprecated use named import
|
|
16
16
|
*/
|
|
17
17
|
export default Interactions;
|
|
18
|
+
export * from './Providers/InteractionsProvider';
|
|
18
19
|
export * from './Providers/AWSLexProvider';
|
|
19
20
|
export { Interactions };
|
|
20
21
|
//# sourceMappingURL=index.js.map
|
package/lib-esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,eAAe,YAAY,CAAC;AAG5B,cAAc,4BAA4B,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAE9C;;GAEG;AACH,eAAe,YAAY,CAAC;AAG5B,cAAc,kCAAkC,CAAC;AACjD,cAAc,4BAA4B,CAAC;AAE3C,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { InteractionsOptions } from './Interactions';
|
|
1
2
|
import { InteractionsResponse } from './Response';
|
|
2
3
|
export interface InteractionsProvider {
|
|
3
|
-
configure(config:
|
|
4
|
+
configure(config: InteractionsOptions): InteractionsOptions;
|
|
4
5
|
getCategory(): string;
|
|
5
6
|
getProviderName(): string;
|
|
6
7
|
sendMessage(botname: string, message: string | Object): Promise<object>;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export interface AWSLexProviderOption {
|
|
2
|
+
name: string;
|
|
3
|
+
alias: string;
|
|
4
|
+
region: string;
|
|
5
|
+
providerName?: string;
|
|
6
|
+
onComplete?(botname: string, callback: (err: any, confirmation: any) => void): void;
|
|
7
|
+
}
|
|
8
|
+
export interface AWSLexProviderOptions {
|
|
9
|
+
[key: string]: AWSLexProviderOption;
|
|
10
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=AWSLexProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AWSLexProvider.js","sourceRoot":"","sources":["../../../src/types/Providers/AWSLexProvider.ts"],"names":[],"mappings":""}
|
package/lib-esm/types/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aws-amplify/interactions",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.50",
|
|
4
4
|
"description": "Interactions category of aws-amplify",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib-esm/index.js",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"homepage": "https://aws-amplify.github.io/",
|
|
43
43
|
"dependencies": {
|
|
44
|
-
"@aws-amplify/core": "4.7.
|
|
44
|
+
"@aws-amplify/core": "4.7.1",
|
|
45
45
|
"@aws-sdk/client-lex-runtime-service": "3.6.1"
|
|
46
46
|
},
|
|
47
47
|
"jest": {
|
|
@@ -88,5 +88,5 @@
|
|
|
88
88
|
"lib-esm"
|
|
89
89
|
]
|
|
90
90
|
},
|
|
91
|
-
"gitHead": "
|
|
91
|
+
"gitHead": "2e016a69956f2bae1e88c4bf6ffb3af628aed454"
|
|
92
92
|
}
|
package/src/Interactions.ts
CHANGED
|
@@ -31,7 +31,7 @@ export class InteractionsClass {
|
|
|
31
31
|
*
|
|
32
32
|
* @param {InteractionsOptions} options - Configuration object for Interactions
|
|
33
33
|
*/
|
|
34
|
-
constructor(options: InteractionsOptions) {
|
|
34
|
+
constructor(options: InteractionsOptions = {}) {
|
|
35
35
|
this._options = options;
|
|
36
36
|
logger.debug('Interactions Options', this._options);
|
|
37
37
|
this._pluggables = {};
|
|
@@ -44,9 +44,9 @@ export class InteractionsClass {
|
|
|
44
44
|
/**
|
|
45
45
|
*
|
|
46
46
|
* @param {InteractionsOptions} options - Configuration object for Interactions
|
|
47
|
-
* @return {
|
|
47
|
+
* @return {InteractionsOptions} - The current configuration
|
|
48
48
|
*/
|
|
49
|
-
configure(options: InteractionsOptions) {
|
|
49
|
+
public configure(options: InteractionsOptions): InteractionsOptions {
|
|
50
50
|
const opt = options ? options.Interactions || options : {};
|
|
51
51
|
logger.debug('configure Interactions', { opt });
|
|
52
52
|
this._options = { bots: {}, ...opt, ...opt.Interactions };
|
|
@@ -63,35 +63,56 @@ export class InteractionsClass {
|
|
|
63
63
|
}
|
|
64
64
|
}
|
|
65
65
|
|
|
66
|
-
//
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
66
|
+
// configure bots to their specific providers
|
|
67
|
+
Object.keys(bots_config).forEach(botKey => {
|
|
68
|
+
const bot = bots_config[botKey];
|
|
69
|
+
const providerName = bot.providerName || 'AWSLexProvider';
|
|
70
|
+
|
|
71
|
+
// add default provider if required
|
|
72
|
+
if (
|
|
73
|
+
!this._pluggables.AWSLexProvider &&
|
|
74
|
+
providerName === 'AWSLexProvider'
|
|
75
|
+
) {
|
|
76
|
+
this._pluggables.AWSLexProvider = new AWSLexProvider();
|
|
77
|
+
}
|
|
76
78
|
|
|
77
|
-
|
|
78
|
-
this._pluggables[
|
|
79
|
+
// configure bot with it's respective provider
|
|
80
|
+
if (this._pluggables[providerName]) {
|
|
81
|
+
this._pluggables[providerName].configure({ [bot.name]: bot });
|
|
82
|
+
} else {
|
|
83
|
+
logger.debug(
|
|
84
|
+
`bot ${bot.name} was not configured as ${providerName} provider was not found`
|
|
85
|
+
);
|
|
86
|
+
}
|
|
79
87
|
});
|
|
80
88
|
|
|
81
89
|
return this._options;
|
|
82
90
|
}
|
|
83
91
|
|
|
84
92
|
public addPluggable(pluggable: InteractionsProvider) {
|
|
85
|
-
if (pluggable && pluggable.getCategory() === 'Interactions') {
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
93
|
+
if (!(pluggable && pluggable.getCategory() === 'Interactions')) {
|
|
94
|
+
throw new Error('Invalid pluggable');
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (!this._pluggables[pluggable.getProviderName()]) {
|
|
98
|
+
// configure bots for the new plugin
|
|
99
|
+
Object.keys(this._options.bots)
|
|
100
|
+
.filter(
|
|
101
|
+
botKey =>
|
|
102
|
+
this._options.bots[botKey].providerName ===
|
|
103
|
+
pluggable.getProviderName()
|
|
104
|
+
)
|
|
105
|
+
.forEach(botKey => {
|
|
106
|
+
const bot = this._options.bots[botKey];
|
|
107
|
+
pluggable.configure({ [bot.name]: bot });
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
this._pluggables[pluggable.getProviderName()] = pluggable;
|
|
111
|
+
return;
|
|
112
|
+
} else {
|
|
113
|
+
throw new Error(
|
|
114
|
+
'Pluggable ' + pluggable.getProviderName() + ' already plugged'
|
|
115
|
+
);
|
|
95
116
|
}
|
|
96
117
|
}
|
|
97
118
|
|
|
@@ -112,14 +133,14 @@ export class InteractionsClass {
|
|
|
112
133
|
message: string | object
|
|
113
134
|
): Promise<InteractionsResponse> {
|
|
114
135
|
if (!this._options.bots || !this._options.bots[botname]) {
|
|
115
|
-
|
|
136
|
+
return Promise.reject('Bot ' + botname + ' does not exist');
|
|
116
137
|
}
|
|
117
138
|
|
|
118
139
|
const botProvider =
|
|
119
140
|
this._options.bots[botname].providerName || 'AWSLexProvider';
|
|
120
141
|
|
|
121
142
|
if (!this._pluggables[botProvider]) {
|
|
122
|
-
|
|
143
|
+
return Promise.reject(
|
|
123
144
|
'Bot ' +
|
|
124
145
|
botProvider +
|
|
125
146
|
' does not have valid pluggin did you try addPluggable first?'
|
|
@@ -128,7 +149,10 @@ export class InteractionsClass {
|
|
|
128
149
|
return await this._pluggables[botProvider].sendMessage(botname, message);
|
|
129
150
|
}
|
|
130
151
|
|
|
131
|
-
public onComplete(
|
|
152
|
+
public onComplete(
|
|
153
|
+
botname: string,
|
|
154
|
+
callback: (err, confirmation) => void
|
|
155
|
+
): void {
|
|
132
156
|
if (!this._options.bots || !this._options.bots[botname]) {
|
|
133
157
|
throw new Error('Bot ' + botname + ' does not exist');
|
|
134
158
|
}
|
|
@@ -146,5 +170,5 @@ export class InteractionsClass {
|
|
|
146
170
|
}
|
|
147
171
|
}
|
|
148
172
|
|
|
149
|
-
export const Interactions = new InteractionsClass(
|
|
173
|
+
export const Interactions = new InteractionsClass();
|
|
150
174
|
Amplify.register(Interactions);
|
|
@@ -10,17 +10,21 @@
|
|
|
10
10
|
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
|
|
11
11
|
* and limitations under the License.
|
|
12
12
|
*/
|
|
13
|
-
|
|
14
13
|
import { AbstractInteractionsProvider } from './InteractionsProvider';
|
|
15
14
|
import {
|
|
16
15
|
InteractionsOptions,
|
|
16
|
+
AWSLexProviderOptions,
|
|
17
17
|
InteractionsResponse,
|
|
18
18
|
InteractionsMessage,
|
|
19
19
|
} from '../types';
|
|
20
20
|
import {
|
|
21
21
|
LexRuntimeServiceClient,
|
|
22
22
|
PostTextCommand,
|
|
23
|
+
PostTextCommandInput,
|
|
24
|
+
PostTextCommandOutput,
|
|
23
25
|
PostContentCommand,
|
|
26
|
+
PostContentCommandInput,
|
|
27
|
+
PostContentCommandOutput,
|
|
24
28
|
} from '@aws-sdk/client-lex-runtime-service';
|
|
25
29
|
import {
|
|
26
30
|
ConsoleLogger as Logger,
|
|
@@ -44,7 +48,24 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
44
48
|
return 'AWSLexProvider';
|
|
45
49
|
}
|
|
46
50
|
|
|
47
|
-
|
|
51
|
+
configure(config: AWSLexProviderOptions = {}): AWSLexProviderOptions {
|
|
52
|
+
const propertiesToTest = ['name', 'alias', 'region'];
|
|
53
|
+
|
|
54
|
+
Object.keys(config).forEach(botKey => {
|
|
55
|
+
const botConfig = config[botKey];
|
|
56
|
+
|
|
57
|
+
// is bot config correct
|
|
58
|
+
if (!propertiesToTest.every(x => x in botConfig)) {
|
|
59
|
+
throw new Error('invalid bot configuration');
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return super.configure(config);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
reportBotStatus(
|
|
66
|
+
data: PostTextCommandOutput | PostContentCommandOutput,
|
|
67
|
+
botname: string
|
|
68
|
+
) {
|
|
48
69
|
// Check if state is fulfilled to resolve onFullfilment promise
|
|
49
70
|
logger.debug('postContent state', data.dialogState);
|
|
50
71
|
if (
|
|
@@ -94,11 +115,16 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
94
115
|
botname: string,
|
|
95
116
|
message: string | InteractionsMessage
|
|
96
117
|
): Promise<InteractionsResponse> {
|
|
118
|
+
// check if bot exists
|
|
97
119
|
if (!this._config[botname]) {
|
|
98
120
|
return Promise.reject('Bot ' + botname + ' does not exist');
|
|
99
121
|
}
|
|
100
|
-
|
|
101
|
-
if
|
|
122
|
+
|
|
123
|
+
// check if credentials are present
|
|
124
|
+
let credentials;
|
|
125
|
+
try {
|
|
126
|
+
credentials = await Credentials.get();
|
|
127
|
+
} catch (error) {
|
|
102
128
|
return Promise.reject('No credentials');
|
|
103
129
|
}
|
|
104
130
|
|
|
@@ -108,7 +134,7 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
108
134
|
customUserAgent: getAmplifyUserAgent(),
|
|
109
135
|
});
|
|
110
136
|
|
|
111
|
-
let params;
|
|
137
|
+
let params: PostTextCommandInput | PostContentCommandInput;
|
|
112
138
|
if (typeof message === 'string') {
|
|
113
139
|
params = {
|
|
114
140
|
botAlias: this._config[botname].alias,
|
|
@@ -118,10 +144,10 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
118
144
|
};
|
|
119
145
|
|
|
120
146
|
logger.debug('postText to lex', message);
|
|
121
|
-
|
|
122
147
|
try {
|
|
123
148
|
const postTextCommand = new PostTextCommand(params);
|
|
124
149
|
const data = await this.lexRuntimeServiceClient.send(postTextCommand);
|
|
150
|
+
|
|
125
151
|
this.reportBotStatus(data, botname);
|
|
126
152
|
return data;
|
|
127
153
|
} catch (err) {
|
|
@@ -133,15 +159,21 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
133
159
|
options: { messageType },
|
|
134
160
|
} = message;
|
|
135
161
|
if (messageType === 'voice') {
|
|
162
|
+
if (!(content instanceof Blob || content instanceof ReadableStream))
|
|
163
|
+
return Promise.reject('invalid content type');
|
|
164
|
+
|
|
136
165
|
params = {
|
|
137
166
|
botAlias: this._config[botname].alias,
|
|
138
167
|
botName: botname,
|
|
139
|
-
contentType: 'audio/x-l16; sample-rate=16000',
|
|
140
|
-
inputStream: content,
|
|
168
|
+
contentType: 'audio/x-l16; sample-rate=16000; channel-count=1',
|
|
169
|
+
inputStream: await convert(content),
|
|
141
170
|
userId: credentials.identityId,
|
|
142
171
|
accept: 'audio/mpeg',
|
|
143
172
|
};
|
|
144
173
|
} else {
|
|
174
|
+
if (typeof content !== 'string')
|
|
175
|
+
return Promise.reject('invalid content type');
|
|
176
|
+
|
|
145
177
|
params = {
|
|
146
178
|
botAlias: this._config[botname].alias,
|
|
147
179
|
botName: botname,
|
|
@@ -157,7 +189,11 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
157
189
|
const data = await this.lexRuntimeServiceClient.send(
|
|
158
190
|
postContentCommand
|
|
159
191
|
);
|
|
160
|
-
|
|
192
|
+
|
|
193
|
+
const audioArray = data.audioStream
|
|
194
|
+
? await convert(data.audioStream)
|
|
195
|
+
: undefined;
|
|
196
|
+
|
|
161
197
|
this.reportBotStatus(data, botname);
|
|
162
198
|
return { ...data, ...{ audioStream: audioArray } };
|
|
163
199
|
} catch (err) {
|
|
@@ -166,9 +202,10 @@ export class AWSLexProvider extends AbstractInteractionsProvider {
|
|
|
166
202
|
}
|
|
167
203
|
}
|
|
168
204
|
|
|
169
|
-
onComplete(botname: string, callback) {
|
|
205
|
+
onComplete(botname: string, callback: (err, confirmation) => void) {
|
|
206
|
+
// does bot exist
|
|
170
207
|
if (!this._config[botname]) {
|
|
171
|
-
throw new
|
|
208
|
+
throw new Error('Bot ' + botname + ' does not exist');
|
|
172
209
|
}
|
|
173
210
|
this._botsCompleteCallback[botname] = callback;
|
|
174
211
|
}
|
package/src/index.ts
CHANGED
package/src/types/Provider.ts
CHANGED
|
@@ -15,7 +15,7 @@ import { InteractionsResponse } from './Response';
|
|
|
15
15
|
|
|
16
16
|
export interface InteractionsProvider {
|
|
17
17
|
// configure your provider
|
|
18
|
-
configure(config:
|
|
18
|
+
configure(config: InteractionsOptions): InteractionsOptions;
|
|
19
19
|
|
|
20
20
|
// return 'Interactions'
|
|
21
21
|
getCategory(): string;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export interface AWSLexProviderOption {
|
|
2
|
+
name: string;
|
|
3
|
+
alias: string;
|
|
4
|
+
region: string;
|
|
5
|
+
providerName?: string;
|
|
6
|
+
onComplete?(botname: string, callback: (err, confirmation) => void): void;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface AWSLexProviderOptions {
|
|
10
|
+
[key: string]: AWSLexProviderOption;
|
|
11
|
+
}
|