botserver/packages/security.gbapp/dialogs/ProfileDialog.ts

206 lines
8.6 KiB
TypeScript
Raw Normal View History

/*****************************************************************************\
| ( )_ _ |
| _ _ _ __ _ _ __ ___ ___ _ _ | ,_)(_) ___ ___ _ |
| ( '_`\ ( '__)/'_` ) /'_ `\/' _ ` _ `\ /'_` )| | | |/',__)/' v `\ /'_`\ |
| | (_) )| | ( (_| |( (_) || ( ) ( ) |( (_| || |_ | |\__, \| (˅) |( (_) ) |
| | ,__/'(_) `\__,_)`\__ |(_) (_) (_)`\__,_)`\__)(_)(____/(_) (_)`\___/' |
| | | ( )_) | |
| (_) \___/' |
| |
| General Bots Copyright (c) Pragmatismo.io. All rights reserved. |
| Licensed under the AGPL-3.0. |
| |
| 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, |
| but WITHOUT ANY WARRANTY, without even the implied warranty of |
| 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. |
| |
\*****************************************************************************/
/**
* @fileoverview Main dialog for kb.gbapp
*/
'use strict';
2020-12-31 15:36:19 -03:00
import { GBLog, GBMinInstance, IGBDialog } from 'botlib';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import { Messages } from '../strings.js';
import * as phone from 'google-libphonenumber';
/**
* Dialogs for handling Menu control.
*/
export class ProfileDialog extends IGBDialog {
public static getNameDialog(min: GBMinInstance) {
return {
2022-11-19 23:34:58 -03:00
id: '/profile_name',
waterfall: [
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);
}
},
async step => {
step.activeDialog.state.options = step.options;
const locale = step.context.activity.locale;
2020-12-31 15:36:19 -03:00
await step.prompt('textPrompt', Messages[locale].whats_name);
},
async step => {
const locale = step.context.activity.locale;
2022-11-19 23:34:58 -03:00
const fullName = text => {
return text.match(/^[a-zA-Z]+(([',. -][a-zA-Z ])?[a-zA-Z]*)*$/gi);
2020-12-31 15:36:19 -03:00
};
const value = fullName(step.result);
if (value === null) {
await step.context.sendActivity(Messages[locale].validation_enter_name);
await step.replaceDialog('/profile_name', step.activeDialog.state.options);
2020-12-31 15:36:19 -03:00
} else {
step.activeDialog.state.options.name = value[0];
return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
}
2022-11-19 23:34:58 -03:00
}
]
2020-12-31 15:36:19 -03:00
};
}
public static getMobileDialog(min: GBMinInstance) {
return {
2022-11-19 23:34:58 -03:00
id: '/profile_mobile',
waterfall: [
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);
}
},
async step => {
step.activeDialog.state.options = step.options;
const locale = step.context.activity.locale;
2020-12-31 15:36:19 -03:00
await step.prompt('textPrompt', Messages[locale].whats_mobile);
},
async step => {
const locale = step.context.activity.locale;
let phoneNumber;
try {
let p = phone.PhoneNumberUtil.getInstance();
2022-12-26 13:37:11 -03:00
phoneNumber = p(step.result, 'BRA')[0]; // https://github.com/GeneralBots/BotServer/issues/307
phoneNumber = phone.phoneUtil.parse(phoneNumber);
} catch (error) {
await step.context.sendActivity(Messages[locale].validation_enter_valid_mobile);
return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
}
if (!phone.phoneUtil.isPossibleNumber(phoneNumber)) {
await step.context.sendActivity(Messages[locale].validation_enter_valid_mobile);
return await step.replaceDialog('/profile_mobile', step.activeDialog.state.options);
2020-12-31 15:36:19 -03:00
} else {
step.activeDialog.state.options.mobile = `${phoneNumber.values_['1']}${phoneNumber.values_['2']}`;
step.activeDialog.state.options.mobileCode = GBAdminService.getMobileCode();
return await step.replaceDialog('/profile_mobile_confirm', step.activeDialog.state.options);
}
2022-11-19 23:34:58 -03:00
}
]
2020-12-31 15:36:19 -03:00
};
}
public static getMobileConfirmDialog(min: GBMinInstance) {
return {
2022-11-19 23:34:58 -03:00
id: '/profile_mobile_confirm',
waterfall: [
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);
}
},
async step => {
step.activeDialog.state.options = step.options;
const locale = step.context.activity.locale;
2020-12-31 15:36:19 -03:00
const from = step.activeDialog.state.options.mobile;
if (min.whatsAppDirectLine) {
2022-11-19 23:34:58 -03:00
await min.whatsAppDirectLine.sendToDevice(
from,
`${step.activeDialog.state.options.mobileCode} is your General Bots creation code.`
);
} else {
GBLog.info(`WhatsApp not configured. Here is the code: ${step.activeDialog.state.options.mobileCode}.`);
}
2020-12-31 15:36:19 -03:00
await step.prompt('textPrompt', Messages[locale].confirm_mobile);
},
async step => {
const locale = step.context.activity.locale;
if (step.result !== step.activeDialog.state.options.mobileCode) {
await step.context.sendActivity(Messages[locale].confirm_mobile_again);
return await step.replaceDialog('/profile_mobile_confirm', step.activeDialog.state.options);
2020-12-31 15:36:19 -03:00
} else {
await step.replaceDialog('/profile_email', step.activeDialog.state.options);
}
2022-11-19 23:34:58 -03:00
}
]
2020-12-31 15:36:19 -03:00
};
}
public static getEmailDialog(min: GBMinInstance) {
return {
2022-11-19 23:34:58 -03:00
id: '/profile_email',
waterfall: [
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);
}
},
async step => {
const locale = step.context.activity.locale;
2020-12-31 15:36:19 -03:00
await step.prompt('textPrompt', Messages[locale].whats_email);
},
async step => {
const locale = step.context.activity.locale;
2022-11-19 23:34:58 -03:00
const extractEntity = text => {
return text.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)/gi);
2020-12-31 15:36:19 -03:00
};
const value = extractEntity(step.context.activity.originalText);
if (value === null) {
await step.context.sendActivity(Messages[locale].validation_enter_valid_email);
await step.replaceDialog('/profile_email', step.activeDialog.state.options);
2020-12-31 15:36:19 -03:00
} else {
step.activeDialog.state.options.email = value[0];
await step.replaceDialog(`/${step.activeDialog.state.options.nextDialog}`, step.activeDialog.state.options);
}
2022-11-19 23:34:58 -03:00
}
]
2020-12-31 15:36:19 -03:00
};
}
}