-
Notifications
You must be signed in to change notification settings - Fork 1
/
exporter.js
172 lines (131 loc) · 4.75 KB
/
exporter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
Copyright 2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in
compliance with the License. A copy of the License is located at
http://aws.amazon.com/apache2.0/
or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
exportlexbot
------------
This script can be used to export the definition for an Amazon Lex bot. It relies on the
AWS SDK and the Amazon Lex Model Building Service API.
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/LexModelBuildingService.html
The user must have IAM permissions to invoke the API functions (e.g., AmazonLexReadOnly)
http://docs.aws.amazon.com/lex/latest/dg/access-control-managing-permissions.html#access-policy-examples-aws-managed
To install:
$ npm init
$ npm install aws-sdk # if you don't have it installed globally
# copy this file as exportlexbot.js
To use:
$ node exportlexbot.js <BotName> <BotVersion>
# e.g., node exportlexbot.js PressoBot "\$LATEST"
# e.g., node exportlexbot.js PressoBot "\$LATEST" | jq '.'
*/
'use strict';
let AWS = require('aws-sdk');
AWS.config.region = 'us-east-1'; // Region
let lexModels = new AWS.LexModelBuildingService();
function getSlotTypeDefinitions(intentDefinitions, callback) {
// first let's get a list of the slot types we need to collect
let slotTypes = [];
let slotTypeDefinitions = [];
intentDefinitions.forEach(function(intentDefinition) {
intentDefinition.slots.forEach(function(slot) {
if (slot.slotTypeVersion && slotTypes.findIndex( obj => obj.slotType == slot.slotType ) == -1) {
// we only want custom slot types
slotTypes.push({
slotType: slot.slotType,
slotTypeVersion: slot.slotTypeVersion
});
}
});
});
if (slotTypes.length > 0) {
// now let's get the slotTypes we need
slotTypes.forEach(function(slotType) {
let params = {
name: slotType.slotType,
version: slotType.slotTypeVersion
};
lexModels.getSlotType(params, function(err, slotTypeDefinition) {
if (err) {
console.error(err);
callback(err, null);
} else {
slotTypeDefinitions.push(slotTypeDefinition);
// callback if we have collected all the definitions we need
if (slotTypeDefinitions.length >= slotTypes.length) {
callback(null, slotTypeDefinitions);
}
}
});
});
} else {
callback(null, []);
}
}
function getIntentDefinitions(intents, callback) {
let intentDefinitions = [];
intents.forEach(function(intent) {
let params = {
name: intent.intentName,
version: intent.intentVersion
};
lexModels.getIntent(params, function(err, intentDefinition) {
if (err) {
callback(err, null);
} else {
// console.log(`adding intent ${intentDefinition.name}`);
intentDefinitions.push(intentDefinition);
// callback if we have collected all the definitions we need
if (intentDefinitions.length >= intents.length) {
callback(null, intentDefinitions);
}
}
});
});
}
function getBotDefinition(myBotName, myBotVersion, callback) {
let params = {
name: myBotName,
versionOrAlias: myBotVersion
};
lexModels.getBot(params, function(err, botDefinition) {
if (err) {
callback(err, null);
} else {
getIntentDefinitions(botDefinition.intents, function(err, intentDefinitions) {
if (err) {
console.log(err);
callback(err, null);
} else {
botDefinition.dependencies = {};
botDefinition.dependencies.intents = intentDefinitions;
getSlotTypeDefinitions(botDefinition.dependencies.intents, function(err, slotTypeDefinitions) {
if (err) {
console.log(err);
callback(err, null);
} else {
botDefinition.dependencies.slotTypes = slotTypeDefinitions;
callback(null, botDefinition);
}
});
}
});
}
});
}
if (process.argv.length < 3 || process.argv.length > 4) {
console.log(`Usage: ${__filename} <BotName> <BotVersion>`);
console.log(` for example: ${__filename} PressoBot "\\$LATEST"`)
process.exit(-1);
}
let myBotName = process.argv[2];
let myBotVersion = process.argv[3] || '$LATEST';
getBotDefinition(myBotName, myBotVersion, function(err, botDefinition) {
if ( err )
console.log( err )
else
console.log(JSON.stringify(botDefinition));
});