2018-04-21 02:59:30 -03:00
|
|
|
/*****************************************************************************\
|
|
|
|
| ( )_ _ |
|
|
|
|
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
|
2020-07-01 15:00:40 -03:00
|
|
|
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
|
2019-03-09 16:59:31 -03:00
|
|
|
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
|
2018-04-21 02:59:30 -03:00
|
|
|
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
|
|
|
|
| | | ( )_) | |
|
|
|
|
| (_) \___/' |
|
|
|
|
| |
|
|
|
|
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
|
|
|
|
| Licensed under the AGPL-3.0. |
|
2018-11-11 19:09:18 -02:00
|
|
|
| |
|
2018-04-21 02:59:30 -03:00
|
|
|
| According to our dual licensing model, this program can be used either |
|
|
|
|
| under the terms of the GNU Affero General Public License, version 3, |
|
|
|
|
| or under a proprietary license. |
|
|
|
|
| |
|
|
|
|
| The texts of the GNU Affero General Public License with an additional |
|
|
|
|
| permission and of our proprietary license can be found at and |
|
|
|
|
| in the LICENSE file you have received along with this program. |
|
|
|
|
| |
|
|
|
|
| This program is distributed in the hope that it will be useful, |
|
2020-07-17 19:39:27 -03:00
|
|
|
| but WITHOUT ANY WARRANTY without even the implied warranty of |
|
2018-04-21 02:59:30 -03:00
|
|
|
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
|
|
|
| GNU Affero General Public License for more details. |
|
|
|
|
| |
|
|
|
|
| "General Bots" is a registered trademark of Pragmatismo.io. |
|
|
|
|
| The licensing of the program under the AGPLv3 does not imply a |
|
|
|
|
| trademark license. Therefore any rights, title and interest in |
|
|
|
|
| our trademarks remain entirely with us. |
|
|
|
|
| |
|
|
|
|
\*****************************************************************************/
|
|
|
|
|
2018-11-11 19:09:18 -02:00
|
|
|
/**
|
|
|
|
* @fileoverview General Bots server core.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
2018-09-16 17:00:17 -03:00
|
|
|
|
2022-11-18 22:39:14 -03:00
|
|
|
import crypto from 'crypto';
|
|
|
|
import urlJoin from 'url-join';
|
2019-03-08 06:37:13 -03:00
|
|
|
import { WaterfallDialog } from 'botbuilder-dialogs';
|
2020-09-20 12:05:24 -03:00
|
|
|
import { GBMinInstance, IGBDialog, GBLog, IGBPackage } from 'botlib';
|
2022-11-18 22:39:14 -03:00
|
|
|
import { GBDeployer } from '../../core.gbapp/services/GBDeployer.js';
|
|
|
|
import { GBImporter } from '../../core.gbapp/services/GBImporterService.js';
|
|
|
|
import { Messages } from '../strings.js';
|
|
|
|
import { GBAdminService } from '../services/GBAdminService.js';
|
2020-05-14 12:46:57 -03:00
|
|
|
import { CollectionUtil } from 'pragmatismo-io-framework';
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Dialogs for administration tasks.
|
|
|
|
*/
|
2018-04-21 02:59:30 -03:00
|
|
|
export class AdminDialog extends IGBDialog {
|
2022-12-22 11:31:37 -03:00
|
|
|
public static isIntentYes(locale, utterance) {
|
2020-05-19 12:36:17 -03:00
|
|
|
return utterance.toLowerCase().match(Messages[locale].affirmative_sentences);
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
public static isIntentNo(locale, utterance) {
|
2020-05-19 12:36:17 -03:00
|
|
|
return utterance.toLowerCase().match(Messages[locale].negative_sentences);
|
|
|
|
}
|
|
|
|
|
2018-09-09 14:39:37 -03:00
|
|
|
/**
|
|
|
|
* Setup dialogs flows and define services call.
|
2018-09-16 17:00:17 -03:00
|
|
|
*
|
2018-09-09 14:39:37 -03:00
|
|
|
* @param bot The bot adapter.
|
|
|
|
* @param min The minimal bot instance data.
|
|
|
|
*/
|
2022-12-22 11:31:37 -03:00
|
|
|
public static setup(min: GBMinInstance) {
|
2018-09-09 14:39:37 -03:00
|
|
|
// Setup services.
|
|
|
|
|
2018-11-12 12:20:44 -02:00
|
|
|
const importer = new GBImporter(min.core);
|
|
|
|
const deployer = new GBDeployer(min.core, importer);
|
2018-04-21 02:59:30 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
AdminDialog.setupSecurityDialogs(min);
|
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/admin-auth', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
2020-05-24 17:06:05 -03:00
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].authenticate;
|
|
|
|
|
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2022-06-01 20:55:58 -03:00
|
|
|
const sensitive = step.context.activity['originalText'];
|
2020-05-24 17:06:05 -03:00
|
|
|
|
2022-06-01 20:55:58 -03:00
|
|
|
if (sensitive === min.instance.adminPass) {
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].welcome);
|
|
|
|
|
|
|
|
return await step.endDialog(true);
|
|
|
|
} else {
|
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].wrong_password);
|
|
|
|
return await step.replaceDialog('/admin-auth');
|
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2020-05-24 17:06:05 -03:00
|
|
|
|
2018-11-01 21:06:11 -03:00
|
|
|
min.dialogs.add(
|
2018-11-12 12:20:44 -02:00
|
|
|
new WaterfallDialog('/admin', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
2018-11-01 21:06:11 -03:00
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].authenticate;
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
2020-03-08 09:24:28 -03:00
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2022-08-26 11:53:47 -03:00
|
|
|
const sensitive = step.context.activity['originalText'];
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-04-15 05:08:50 +00:00
|
|
|
if (sensitive === min.instance.adminPass) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].welcome);
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, Messages[locale].which_task);
|
2020-03-08 09:24:28 -03:00
|
|
|
} else {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].wrong_password);
|
2018-12-06 10:16:28 -02:00
|
|
|
|
2020-03-08 09:24:28 -03:00
|
|
|
return await step.endDialog();
|
|
|
|
}
|
2018-11-01 21:06:11 -03:00
|
|
|
},
|
|
|
|
async step => {
|
2019-03-08 19:13:00 -03:00
|
|
|
const locale: string = step.context.activity.locale;
|
|
|
|
// tslint:disable-next-line:no-unsafe-any
|
2021-09-13 19:04:32 -03:00
|
|
|
const text: string = step.context.activity['originalText'];
|
2018-11-12 12:20:44 -02:00
|
|
|
const cmdName = text.split(' ')[0];
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].working(cmdName));
|
2018-11-01 21:06:11 -03:00
|
|
|
let unknownCommand = false;
|
2018-09-24 15:27:26 -03:00
|
|
|
|
2019-08-23 14:36:47 -03:00
|
|
|
try {
|
|
|
|
if (text === 'quit') {
|
|
|
|
return await step.replaceDialog('/');
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'deployPackage' || cmdName === 'dp') {
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.deployPackageCommand(min, text, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2019-11-10 16:20:15 -03:00
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'redeployPackage' || cmdName === 'rp') {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, 'The package is being *unloaded*...');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.undeployPackageCommand(text, min);
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, 'Now, *deploying* package...');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.deployPackageCommand(min, text, deployer);
|
2020-09-20 12:05:24 -03:00
|
|
|
await min.conversationalService.sendText(
|
|
|
|
min,
|
|
|
|
step,
|
|
|
|
'Package deployed. Just need to rebuild the index... Doing it right now.'
|
|
|
|
);
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.rebuildIndexPackageCommand(min, deployer);
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, 'Finished importing of that .gbkb package. Thanks.');
|
2019-08-23 14:36:47 -03:00
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2020-05-12 09:06:47 -03:00
|
|
|
} else if (cmdName === 'undeployPackage' || cmdName === 'up') {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, 'The package is being *undeployed*...');
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.undeployPackageCommand(text, min);
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, 'Package *undeployed*.');
|
2019-08-23 14:36:47 -03:00
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
2021-09-13 18:53:47 -03:00
|
|
|
} else if (cmdName === 'rebuildIndex' || cmdName === 'ri' || cmdName === 'Ri') {
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.rebuildIndexPackageCommand(min, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
|
|
|
} else if (cmdName === 'syncBotServer') {
|
2020-04-01 15:42:57 -03:00
|
|
|
await GBAdminService.syncBotServerCommand(min, deployer);
|
2019-08-23 14:36:47 -03:00
|
|
|
|
|
|
|
return await step.replaceDialog('/admin', { firstRun: false });
|
|
|
|
} else if (cmdName === 'setupSecurity') {
|
|
|
|
return await step.beginDialog('/setupSecurity');
|
|
|
|
} else {
|
|
|
|
unknownCommand = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unknownCommand) {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].unknown_command);
|
2019-08-23 14:36:47 -03:00
|
|
|
} else {
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].finished_working);
|
2019-08-23 14:36:47 -03:00
|
|
|
}
|
|
|
|
} catch (error) {
|
2020-11-30 21:25:29 -03:00
|
|
|
await min.conversationalService.sendText(min, step, error.message ? error.message : error);
|
2018-11-01 21:06:11 -03:00
|
|
|
}
|
2019-08-23 14:36:47 -03:00
|
|
|
await step.replaceDialog('/ask', { isReturning: true });
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
])
|
|
|
|
);
|
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/install', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
2020-09-20 12:05:24 -03:00
|
|
|
async step => {
|
|
|
|
step.activeDialog.state.options.args = (step.options as any).args;
|
|
|
|
if (step.activeDialog.state.options.confirm) {
|
|
|
|
return await step.next('sim');
|
|
|
|
} else {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
return await min.conversationalService.prompt(min, step, Messages[locale].publish_type_yes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2019-08-23 14:36:47 -03:00
|
|
|
|
2020-09-20 12:05:24 -03:00
|
|
|
// If the user says yes, starts publishing.
|
|
|
|
|
|
|
|
if (AdminDialog.isIntentYes(locale, step.result)) {
|
2022-12-22 11:31:37 -03:00
|
|
|
const list = min.core.getParam(min.instance, '.gbapp List', null);
|
|
|
|
const items = list ? list.split(';') : [];
|
|
|
|
|
2020-09-20 12:05:24 -03:00
|
|
|
step.activeDialog.state.options.args;
|
2020-10-17 23:03:27 -03:00
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
for (let j = 0; j < min.appPackages.length; j++) {
|
|
|
|
if (items[i] === min.appPackages[j]['name']) {
|
|
|
|
const element = min.appPackages[i];
|
|
|
|
await element.onExchangeData(min, 'install', null);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_canceled);
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 21:06:11 -03:00
|
|
|
])
|
|
|
|
);
|
2020-05-12 09:06:47 -03:00
|
|
|
|
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/publish', [
|
2021-05-10 10:53:53 -03:00
|
|
|
async step => {
|
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-05-12 09:06:47 -03:00
|
|
|
async step => {
|
2022-11-19 23:34:58 -03:00
|
|
|
if (step.activeDialog.state.options.confirm || process.env.ADMIN_OPEN_PUBLISH === 'true') {
|
2020-05-19 12:36:17 -03:00
|
|
|
return await step.next('sim');
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-19 12:36:17 -03:00
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
return await min.conversationalService.prompt(min, step, Messages[locale].publish_type_yes);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
// If the user says yes, starts publishing.
|
|
|
|
|
|
|
|
if (AdminDialog.isIntentYes(locale, step.result)) {
|
|
|
|
let from = step.context.activity.from.id;
|
2020-05-23 11:10:06 -03:00
|
|
|
|
2020-10-27 12:34:46 -03:00
|
|
|
let canPublish: Boolean;
|
|
|
|
if (step.activeDialog.state.options.firstTime) {
|
|
|
|
canPublish = true;
|
|
|
|
} else {
|
2022-11-19 23:34:58 -03:00
|
|
|
canPublish = AdminDialog.canPublish(min, from) || process.env.ADMIN_OPEN_PUBLISH === 'true';
|
2020-10-27 12:34:46 -03:00
|
|
|
}
|
2020-05-19 12:36:17 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
if (!canPublish) {
|
|
|
|
await step.beginDialog('/admin-auth');
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-25 17:59:02 -03:00
|
|
|
await step.next(true);
|
2020-05-24 17:06:05 -03:00
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_canceled);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
if (!step.result) {
|
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_must_be_admin);
|
|
|
|
|
|
|
|
return step.endDialog();
|
|
|
|
}
|
2020-05-19 12:36:17 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
const botId = min.instance.botId;
|
2020-05-25 17:59:02 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].working('Publishing'));
|
2020-05-19 12:36:17 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
step.activeDialog.state.options.args = (step.options as any).args;
|
2020-09-20 12:05:24 -03:00
|
|
|
const filename = step.activeDialog.state.options.args
|
|
|
|
? step.activeDialog.state.options.args.split(' ')[0]
|
|
|
|
: null;
|
2020-05-23 19:58:38 -03:00
|
|
|
|
2020-05-24 17:06:05 -03:00
|
|
|
const packages = [];
|
2020-06-14 18:06:29 -03:00
|
|
|
let skipError = false;
|
2020-09-20 12:05:24 -03:00
|
|
|
if (filename === null || filename === '') {
|
2020-06-14 18:06:29 -03:00
|
|
|
await min.conversationalService.sendText(min, step, `Starting publishing for ${botId} packages...`);
|
2020-05-24 17:06:05 -03:00
|
|
|
packages.push(`${botId}.gbkb`);
|
2020-06-14 18:06:29 -03:00
|
|
|
packages.push(`${botId}.gbtheme`);
|
|
|
|
packages.push(`${botId}.gbdialog`);
|
|
|
|
packages.push(`${botId}.gbot`);
|
|
|
|
skipError = true;
|
2020-05-24 17:06:05 -03:00
|
|
|
} else {
|
|
|
|
await min.conversationalService.sendText(min, step, `Starting publishing for ${filename}...`);
|
|
|
|
packages.push(filename);
|
|
|
|
}
|
2020-05-14 12:46:57 -03:00
|
|
|
|
2020-06-14 18:06:29 -03:00
|
|
|
await CollectionUtil.asyncForEach(packages, async packageName => {
|
|
|
|
try {
|
2020-10-17 23:03:27 -03:00
|
|
|
let cmd1;
|
|
|
|
if (packageName.indexOf('.') !== -1) {
|
2022-12-22 11:31:37 -03:00
|
|
|
cmd1 = `deployPackage ${process.env.STORAGE_SITE} /${process.env.STORAGE_LIBRARY}/${botId}.gbai/${packageName}`;
|
2020-10-17 23:03:27 -03:00
|
|
|
} else {
|
2020-10-27 12:34:46 -03:00
|
|
|
cmd1 = `deployPackage ${packageName}`;
|
2020-10-17 23:03:27 -03:00
|
|
|
}
|
2022-11-19 23:34:58 -03:00
|
|
|
if (
|
|
|
|
(await (deployer as any).getStoragePackageByName(min.instance.instanceId, packageName)) !== null &&
|
2021-02-05 09:55:06 -03:00
|
|
|
!process.env.DONT_DOWNLOAD
|
|
|
|
) {
|
2020-05-24 17:06:05 -03:00
|
|
|
const cmd2 = `undeployPackage ${packageName}`;
|
|
|
|
await GBAdminService.undeployPackageCommand(cmd2, min);
|
2020-05-14 12:46:57 -03:00
|
|
|
}
|
2020-05-24 17:06:05 -03:00
|
|
|
await GBAdminService.deployPackageCommand(min, cmd1, deployer);
|
|
|
|
await min.conversationalService.sendText(min, step, `Finished publishing ${packageName}.`);
|
2020-06-14 18:06:29 -03:00
|
|
|
} catch (error) {
|
|
|
|
GBLog.error(error);
|
|
|
|
if (!skipError) {
|
|
|
|
await min.conversationalService.sendText(min, step, `ERROR: ${error}`);
|
|
|
|
|
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-05-24 17:06:05 -03:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].publish_success);
|
|
|
|
if (!step.activeDialog.state.options.confirm) {
|
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2020-09-20 12:05:24 -03:00
|
|
|
} else {
|
2020-05-24 17:06:05 -03:00
|
|
|
return await step.endDialog();
|
|
|
|
}
|
2020-09-20 12:05:24 -03:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2018-09-16 17:00:17 -03:00
|
|
|
}
|
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
/**
|
2020-09-20 12:05:24 -03:00
|
|
|
* Check if the specified phone can receive a message by running
|
|
|
|
* the /broadcast command with specific phone numbers.
|
|
|
|
* @param phone Phone number to check (eg.: +5521900002233)
|
|
|
|
*/
|
2022-12-22 11:31:37 -03:00
|
|
|
public static canPublish(min: GBMinInstance, phone: string): Boolean {
|
2020-10-27 12:34:46 -03:00
|
|
|
if (process.env.SECURITY_CAN_PUBLISH !== undefined) {
|
2021-08-19 08:02:56 -03:00
|
|
|
let list = process.env.SECURITY_CAN_PUBLISH.split(';');
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
const canPublish = min.core.getParam(min.instance, 'Can Publish', null);
|
2021-08-19 08:02:56 -03:00
|
|
|
if (canPublish) {
|
|
|
|
list = list.concat(canPublish.split(';'));
|
|
|
|
}
|
|
|
|
|
2020-10-27 12:34:46 -03:00
|
|
|
let result = list.includes(phone);
|
|
|
|
|
|
|
|
if (!result && min.instance.params) {
|
|
|
|
const params = JSON.parse(min.instance.params);
|
|
|
|
return list.includes(params['Can Publish']);
|
|
|
|
}
|
|
|
|
return result;
|
2020-05-23 17:59:01 -03:00
|
|
|
}
|
|
|
|
}
|
2020-05-23 19:58:38 -03:00
|
|
|
|
2022-12-22 11:31:37 -03:00
|
|
|
private static setupSecurityDialogs(min: GBMinInstance) {
|
2019-01-31 11:32:33 -02:00
|
|
|
min.dialogs.add(
|
|
|
|
new WaterfallDialog('/setupSecurity', [
|
|
|
|
async step => {
|
2021-05-10 10:53:53 -03:00
|
|
|
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
|
|
|
|
return await step.beginDialog('/auth');
|
2022-11-19 23:34:58 -03:00
|
|
|
} else {
|
2021-05-10 10:53:53 -03:00
|
|
|
return await step.next(step.options);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
async step => {
|
2019-01-31 11:32:33 -02:00
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].enter_authenticator_tenant;
|
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
2019-01-31 11:32:33 -02:00
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
step.activeDialog.state.authenticatorTenant = step.result;
|
|
|
|
const locale = step.context.activity.locale;
|
|
|
|
const prompt = Messages[locale].enter_authenticator_authority_host_url;
|
|
|
|
|
2020-05-19 12:36:17 -03:00
|
|
|
return await min.conversationalService.prompt(min, step, prompt);
|
2019-01-31 11:32:33 -02:00
|
|
|
},
|
|
|
|
async step => {
|
|
|
|
step.activeDialog.state.authenticatorAuthorityHostUrl = step.result;
|
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
min.instance.authenticatorTenant = step.activeDialog.state.authenticatorTenant;
|
|
|
|
min.instance.authenticatorAuthorityHostUrl = step.activeDialog.state.authenticatorAuthorityHostUrl;
|
2020-11-30 21:25:29 -03:00
|
|
|
|
2019-01-31 11:32:33 -02:00
|
|
|
await min.adminService.updateSecurityInfo(
|
|
|
|
min.instance.instanceId,
|
|
|
|
step.activeDialog.state.authenticatorTenant,
|
2020-05-14 17:16:27 -03:00
|
|
|
step.activeDialog.state.authenticatorAuthorityHostUrl
|
2019-01-31 11:32:33 -02:00
|
|
|
);
|
|
|
|
|
|
|
|
const locale = step.context.activity.locale;
|
2019-08-21 21:04:55 +00:00
|
|
|
const buf = Buffer.alloc(16);
|
|
|
|
const state = `${min.instance.instanceId}${crypto.randomFillSync(buf).toString('hex')}`;
|
2020-05-12 09:06:47 -03:00
|
|
|
|
2019-03-08 19:13:00 -03:00
|
|
|
min.adminService.setValue(min.instance.instanceId, 'AntiCSRFAttackState', state);
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2022-11-19 23:34:58 -03:00
|
|
|
const redirectUri = urlJoin(min.instance.botEndpoint, min.instance.botId, '/token');
|
2022-12-22 11:31:37 -03:00
|
|
|
const url = `https://login.microsoftonline.com/${step.activeDialog.state.authenticatorTenant}/oauth2/authorize?client_id=${min.instance.marketplaceId}&response_type=code&redirect_uri=${redirectUri}&scope=https://graph.microsoft.com/.default&state=${state}&response_mode=query`;
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2020-05-17 21:30:21 +00:00
|
|
|
await min.conversationalService.sendText(min, step, Messages[locale].consent(url));
|
2019-01-31 11:32:33 -02:00
|
|
|
|
2019-03-08 06:37:13 -03:00
|
|
|
return await step.replaceDialog('/ask', { isReturning: true });
|
2019-01-31 11:32:33 -02:00
|
|
|
}
|
|
|
|
])
|
|
|
|
);
|
2018-04-21 02:59:30 -03:00
|
|
|
}
|
|
|
|
}
|