msbot
Version:
MSBot command line tool for manipulating Microsoft Bot Framework .bot files
122 lines • 4.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Copyright(c) Microsoft Corporation.All rights reserved.
* Licensed under the MIT License.
*/
// tslint:disable:no-console
// tslint:disable:no-object-literal-type-assertion
const botframework_config_1 = require("botframework-config");
const chalk = require("chalk");
const program = require("commander");
const txtfile = require("read-text-file");
const stdioAsync_1 = require("./stdioAsync");
const utils_1 = require("./utils");
program.Command.prototype.unknownOption = (flag) => {
console.error(chalk.default.redBright(`Unknown arguments: ${flag}`));
showErrorHelp();
};
program
.name('msbot connect dispatch')
.description('Connect the bot to a dispatch model')
.option('-n, --name <name>', 'name for the dispatch')
.option('-a, --appId <appid>', 'LUID AppId for the dispatch app')
.option('--version <version>', 'version for the dispatch app, (example: 0.1)')
.option('--authoringKey <authoringkey>', 'authoring key for using manipulating the dispatch model via the LUIS authoring API\n')
.option('r, --region <region>', 'region to use (example: westus)')
.option('--subscriptionKey <subscriptionKey>', '(OPTIONAL) subscription key used for querying the dispatch model')
.option('--ids <ids>', '(OPTIONAL) comma delimited list of service ids in this bot (qna or luis) to build a dispatch model over.')
.option('-b, --bot <path>', 'path to bot file. If omitted, local folder will look for a .bot file')
.option('--input <jsonfile>', 'path to arguments in JSON format { id:\'\',name:\'\', ... }')
.option('--secret <secret>', 'bot file secret password for encrypting service secrets')
.option('--stdin', 'arguments are passed in as JSON object via stdin')
.option('--prefix', 'Append [msbot] prefix to all messages')
.action((cmd, actions) => undefined);
const command = program.parse(process.argv);
const args = {};
Object.assign(args, command);
if (args.stdin) {
//force verbosity output if args are passed via stdin
process.env.PREFIX = 'prefix';
}
if (process.argv.length < 3) {
program.help();
}
else {
if (!args.bot) {
botframework_config_1.BotConfiguration.loadBotFromFolder(process.cwd(), args.secret)
.then(processConnectDispatch)
.catch((reason) => {
console.error(chalk.default.redBright(reason.toString().split('\n')[0]));
showErrorHelp();
});
}
else {
botframework_config_1.BotConfiguration.load(args.bot, args.secret)
.then(processConnectDispatch)
.catch((reason) => {
console.error(chalk.default.redBright(reason.toString().split('\n')[0]));
showErrorHelp();
});
}
}
async function processConnectDispatch(config) {
args.name = args.hasOwnProperty('name') ? args.name : config.name;
if (args.stdin) {
Object.assign(args, JSON.parse(await utils_1.getStdin()));
}
else if (args.input != null) {
Object.assign(args, JSON.parse(await txtfile.read(args.input)));
}
if (!args.hasOwnProperty('name')) {
throw new Error('Bad or missing --name');
}
if (!args.appId || !utils_1.uuidValidate(args.appId)) {
throw new Error('bad or missing --appId');
}
if (!args.version) {
throw new Error('bad or missing --version');
}
args.version = args.version.toString();
if (!args.authoringKey || !utils_1.uuidValidate(args.authoringKey)) {
throw new Error('bad or missing --authoringKey');
}
if (args.subscriptionKey && !utils_1.uuidValidate(args.subscriptionKey)) {
throw new Error('bad --subscriptionKey');
}
if (args.ids && args.ids.length > 0) {
args.serviceIds = args.ids.split(',');
}
if (args.services) {
let botConfig2 = botframework_config_1.BotConfiguration.fromJSON({ services: args.services });
for (let service of botConfig2.services) {
if (service.id) {
if (!config.findService(service.id)) {
config.services.push(service);
}
}
}
}
const newService = new botframework_config_1.DispatchService({
name: args.name,
appId: args.appId,
authoringKey: args.authoringKey,
subscriptionKey: args.subscriptionKey,
version: args.version,
region: args.region,
serviceIds: args.serviceIds
});
// add the service
const id = config.connectService(newService);
await config.save(args.secret);
await stdioAsync_1.stdoutAsync(JSON.stringify(config.findService(id), null, 2));
return config;
}
function showErrorHelp() {
program.outputHelp((str) => {
console.error(str);
return '';
});
process.exit(1);
}
//# sourceMappingURL=msbot-connect-dispatch.js.map