From 9bc82abf11fb7ad8aed8e53315ba250cbbcbc15a Mon Sep 17 00:00:00 2001 From: Rodrigo Rodriguez Date: Sat, 26 Dec 2020 09:38:21 -0300 Subject: [PATCH] fix(core.gbapp): More comments in GBMinService. --- package.json | 2 +- packages/core.gbapp/services/GBMinService.ts | 578 ++++++++++++------- 2 files changed, 373 insertions(+), 207 deletions(-) diff --git a/package.json b/package.json index ad9e83e5..98c50b7e 100644 --- a/package.json +++ b/package.json @@ -69,7 +69,7 @@ "botbuilder-ai": "4.11.0", "botbuilder-dialogs": "4.11.0", "botframework-connector": "4.11.0", - "botlib": "1.7.0", + "botlib": "1.7.1", "cli-spinner": "0.2.10", "core-js": "3.7.0", "dotenv-extended": "2.9.0", diff --git a/packages/core.gbapp/services/GBMinService.ts b/packages/core.gbapp/services/GBMinService.ts index 45f90df6..68b43bb5 100644 --- a/packages/core.gbapp/services/GBMinService.ts +++ b/packages/core.gbapp/services/GBMinService.ts @@ -81,18 +81,35 @@ import { GBAdminService } from '../../admin.gbapp/services/GBAdminService'; import { GBConversationalService } from './GBConversationalService'; /** - * Minimal service layer for a bot. + * Minimal service layer for a bot and encapsulation of BOT Framework. */ export class GBMinService { + + /** + * Main core service attached to this bot service. + */ public core: IGBCoreService; + + /** + * Reference to conversation services like receive and prompt text. + */ public conversationalService: IGBConversationalService; + + /** + * Conversational administration services like publishing packages. + */ public adminService: IGBAdminService; + + /** + * Deployent of packages and publishing related services. + */ public deployer: GBDeployer; + /** + * Default General Bots User Interface package. + */ private static uiPackage = 'default.gbui'; - public corePackage = 'core.gbai'; - /** * Static initialization of minimal instance. * @@ -110,19 +127,124 @@ export class GBMinService { this.deployer = deployer; } - /** - * - * Constructs a new minimal instance for each bot. - * - * @param server An HTTP server. - * @param appPackages List of loaded .gbapp associated with this instance. - * - * @return Loaded minimal bot instance. - * - */ + private async WhatsAppCallback(req, res) { + try { + + // Detects if the message is echo fro itself. + + const id = req.body.messages[0].chatId.split('@')[0]; + const senderName = req.body.messages[0].senderName; + const text = req.body.messages[0].body; + if (req.body.messages[0].fromMe) { + res.end(); + return; // Exit here. + } + + // Detects if the welcome message is enabled. + + let activeMin; + if (process.env.WHATSAPP_WELCOME_DISABLED !== 'true') { + + // Tries to find if user wants to switch bots. + + let toSwitchMin = GBServer.globals.minInstances.filter( + p => p.instance.botId.toLowerCase() === text.toLowerCase() + )[0]; + if (!toSwitchMin) { + toSwitchMin = GBServer.globals.minInstances.filter(p => + p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false + )[0]; + } + + // Find active bot instance. + + activeMin = toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; + + // If it is the first time for the user, tries to auto-execute + // start dialog if any is specified in Config.xlsx. + + let sec = new SecService(); + let user = await sec.getUserFromSystemId(id); + if (user === null || user.hearOnDialog) { + user = await sec.ensureUser(activeMin.instance.instanceId, id, senderName, '', 'whatsapp', senderName); + + let startDialog = user.hearOnDialog ? + user.hearOnDialog : + activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); + + GBLog.info(`Auto start dialog is now being called: ${startDialog}...`); + if (startDialog) { + req.body.messages[0].body = `${startDialog}`; + + // Resets HEAR ON DIALOG value to none and passes + // current dialog to the direct line. + + await sec.updateUserHearOnDialog(user.userId, null); + await (activeMin as any).whatsAppDirectLine.received(req, res); + } + else { + await (activeMin as any).whatsAppDirectLine.sendToDevice( + id, + `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.` + ); + res.end(); + } + } else { + + // User wants to switch bots. + + if (toSwitchMin !== undefined) { + + // So gets the new bot instance information and prepares to + // auto start dialog if any is specified. + + const instance = await this.core.loadInstanceByBotId(activeMin.botId); + await sec.updateUserInstance(id, instance.instanceId); + await (activeMin as any).whatsAppDirectLine.resetConversationId(id); + let startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); + GBLog.info(`Auto start dialog is now being called: ${startDialog}...`); + + if (startDialog) { + req.body.messages[0].body = `${startDialog}`; + await (activeMin as any).whatsAppDirectLine.received(req, res); + } + else { + await (activeMin as any).whatsAppDirectLine.sendToDevice( + id, + `Agora falando com ${activeMin.instance.title}...` + ); + res.end(); + } + } else { + activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0]; + if (activeMin === undefined) { + activeMin = GBServer.globals.minBoot; + await (activeMin as any).whatsAppDirectLine.sendToDevice( + id, + `O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...` + ); + } + await (activeMin as any).whatsAppDirectLine.received(req, res); + } + } + } else { + + // Just pass the message to the receiver. + + await (GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res); + } + } catch (error) { + GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error}`); + } + } + + /** + * Constructs a new minimal instance for each bot. + */ public async buildMin(instances: IGBInstance[]) { - // Serves default UI on root address '/' if web enabled. + + // Servers default UI on root address '/' if web enabled. if (process.env.DISABLE_WEB !== 'true') { let url = GBServer.globals.wwwroot ? GBServer.globals.wwwroot @@ -130,106 +252,19 @@ export class GBMinService { GBServer.globals.server.use('/', express.static(url)); } - // Serves the bot information object via HTTP so clients can get + + // Servers the bot information object via HTTP so clients can get // instance information stored on server. + if (process.env.DISABLE_WEB !== 'true') { - GBServer.globals.server.get('/instances/:botId', (req, res) => { - (async () => { - await this.handleGetInstanceForClient(req, res); - })(); - }); + GBServer.globals.server.get('/instances/:botId', this.handleGetInstanceForClient); } - const url = '/webhooks/whatsapp'; - GBServer.globals.server.post(url, async (req, res) => { - try { - const id = req.body.messages[0].chatId.split('@')[0]; - const senderName = req.body.messages[0].senderName; - const text = req.body.messages[0].body; - if (req.body.messages[0].fromMe) { - res.end(); - return; // Exit here. - } - let activeMin; - if (process.env.WHATSAPP_WELCOME_DISABLED !== 'true') { - let toSwitchMin = GBServer.globals.minInstances.filter( - p => p.instance.botId.toLowerCase() === text.toLowerCase() - )[0]; - if (!toSwitchMin) { - toSwitchMin = GBServer.globals.minInstances.filter(p => - p.instance.activationCode ? p.instance.activationCode.toLowerCase() === text.toLowerCase() : false - )[0]; - } - activeMin = toSwitchMin ? toSwitchMin : GBServer.globals.minBoot; + // Servers the WhatsApp callback. - let sec = new SecService(); - let user = await sec.getUserFromSystemId(id); + GBServer.globals.server.post('/webhooks/whatsapp', this.WhatsAppCallback); - if (user === null || user.hearOnDialog) { - user = await sec.ensureUser(activeMin.instance.instanceId, id, senderName, '', 'whatsapp', senderName); - - let startDialog = user.hearOnDialog ? - user.hearOnDialog : - activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); - - GBLog.info(`Auto start dialog is now being called: ${startDialog}...`); - if (startDialog) { - req.body.messages[0].body = `${startDialog}`; - - // Resets HEAR ON DIALOG value to none and passes - // current dialog to the direct line. - - await sec.updateUserHearOnDialog(user.userId, null); - await (activeMin as any).whatsAppDirectLine.received(req, res); - } - else { - await (activeMin as any).whatsAppDirectLine.sendToDevice( - id, - `Olá! Seja bem-vinda(o)!\nMe chamo ${activeMin.instance.title}. Como posso ajudar? Pode me falar que eu te ouço, me manda um aúdio.` - ); - res.end(); - } - } else { - // User wants to switch bots. - if (toSwitchMin !== undefined) { - const instance = await this.core.loadInstanceByBotId(activeMin.botId); - await sec.updateUserInstance(id, instance.instanceId); - - await (activeMin as any).whatsAppDirectLine.resetConversationId(id); - let startDialog = activeMin.core.getParam(activeMin.instance, 'Start Dialog', null); - - GBLog.info(`Auto start dialog is now being called: ${startDialog}...`); - - if (startDialog) { - req.body.messages[0].body = `${startDialog}`; - await (activeMin as any).whatsAppDirectLine.received(req, res); - } - else { - await (activeMin as any).whatsAppDirectLine.sendToDevice( - id, - `Agora falando com ${activeMin.instance.title}...` - ); - res.end(); - } - } else { - activeMin = GBServer.globals.minInstances.filter(p => p.instance.instanceId === user.instanceId)[0]; - if (activeMin === undefined) { - activeMin = GBServer.globals.minBoot; - await (activeMin as any).whatsAppDirectLine.sendToDevice( - id, - `O outro Bot que você estava falando(${user.instanceId}), não está mais disponível. Agora você está falando comigo, ${activeMin.instance.title}...` - ); - } - await (activeMin as any).whatsAppDirectLine.received(req, res); - } - } - } else { - await (GBServer.globals.minBoot as any).whatsAppDirectLine.received(req, res); - } - } catch (error) { - GBLog.error(`Error on Whatsapp callback: ${error.data ? error.data : error}`); - } - }); + // Call mountBot event to all bots. await CollectionUtil.asyncForEach(instances, async instance => { try { @@ -240,6 +275,10 @@ export class GBMinService { }); } + /** + * Removes bot endpoint from web listeners and remove bot instance + * from list of global server bot instances. + */ public async unmountBot(botId: string) { const url = `/api/messages/${botId}`; removeRoute(GBServer.globals.server, url); @@ -250,8 +289,15 @@ export class GBMinService { GBServer.globals.minInstances = GBServer.globals.minInstances.filter(p => p.instance.botId !== botId); } + /** + * Mount the instance by creating an BOT Framework bot object, + * serving bot endpoint in several URL like WhatsApp endpoint, .gbkb assets, + * installing all BASIC artifacts from .gbdialog and OAuth2. + */ public async mountBot(instance: IGBInstance) { + // Build bot adapter. + const { min, adapter, conversationState } = await this.buildBotAdapter( instance, GBServer.globals.sysPackages, @@ -280,10 +326,12 @@ export class GBMinService { await this.deployer.deployPackage(min, packagePath); } - // Call the loadBot context.activity for all packages. + // Calls the loadBot context.activity for all packages. + await this.invokeLoadBot(GBServer.globals.appPackages, GBServer.globals.sysPackages, min); // Serves individual URL for each bot conversational interface... + const url = `/api/messages/${instance.botId}`; GBServer.globals.server.post(url, async (req, res) => { await this.receiver(adapter, req, res, conversationState, min, instance, GBServer.globals.appPackages); @@ -291,6 +339,7 @@ export class GBMinService { GBLog.info(`GeneralBots(${instance.engineName}) listening on: ${url}.`); // Serves individual URL for each bot user interface. + if (process.env.DISABLE_WEB !== 'true') { const uiUrl = `/${instance.botId}`; const uiUrlAlt = `/${instance.activationCode}`; @@ -309,19 +358,30 @@ export class GBMinService { // Clients get redirected here in order to create an OAuth authorize url and redirect them to AAD. // There they will authenticate and give their consent to allow this app access to // some resource they own. + this.handleOAuthRequests(GBServer.globals.server, min); // After consent is granted AAD redirects here. The ADAL library // is invoked via the AuthenticationContext and retrieves an // access token that can be used to access the user owned resource. + this.handleOAuthTokenRequests(GBServer.globals.server, min, instance); + // Provides checking of instance health. + this.createCheckHealthAddress(GBServer.globals.server, min, min.instance); } + /** + * Creates a listener that can be used by external monitors to check + * bot instance health. + */ private createCheckHealthAddress(server: any, min: GBMinInstance, instance: IGBInstance) { server.get(`/${min.instance.botId}/check`, async (req, res) => { try { + + // Performs the checking of WhatsApp API if enabled for this instance. + if (min.whatsAppDirectLine != undefined && instance.whatsappServiceKey !== null) { if (!(await min.whatsAppDirectLine.check(min))) { const error = `WhatsApp API lost connection.`; @@ -331,16 +391,31 @@ export class GBMinService { return; } } + + // GB is OK, so 200. + res.status(200).send(`General Bot ${min.botId} is healthly.`); + } catch (error) { + + // GB is not OK, 500 and detail the information on response content. + GBLog.error(error); res.status(500).send(error.toString()); } }); } + /** + * Handle OAuth2 web service calls for token requests + * on https:////token URL. + */ private handleOAuthTokenRequests(server: any, min: GBMinInstance, instance: IGBInstance) { + server.get(`/${min.instance.botId}/token`, async (req, res) => { + + // Checks request state by reading AntiCSRFAttackState from GB Admin infrastructure. + const state = await min.adminService.getValue(instance.instanceId, 'AntiCSRFAttackState'); if (req.query.state !== state) { const msg = 'WARNING: state field was not provided as anti-CSRF token'; @@ -351,6 +426,9 @@ export class GBMinService { urlJoin(min.instance.authenticatorAuthorityHostUrl, min.instance.authenticatorTenant) ); const resource = 'https://graph.microsoft.com'; + + // Calls MSFT to get token. + authenticationContext.acquireTokenWithAuthorizationCode( req.query.code, urlJoin(instance.botEndpoint, min.instance.botId, '/token'), @@ -363,11 +441,16 @@ export class GBMinService { GBLog.error(msg); res.send(msg); } else { - // TODO: await these calls. - this.adminService.setValue(instance.instanceId, 'accessToken', token.accessToken); - this.adminService.setValue(instance.instanceId, 'refreshToken', token.refreshToken); - this.adminService.setValue(instance.instanceId, 'expiresOn', token.expiresOn.toString()); - this.adminService.setValue(instance.instanceId, 'AntiCSRFAttackState', undefined); + + // Saves token to the database. + + await this.adminService.setValue(instance.instanceId, 'accessToken', token.accessToken); + await this.adminService.setValue(instance.instanceId, 'refreshToken', token.refreshToken); + await this.adminService.setValue(instance.instanceId, 'expiresOn', token.expiresOn.toString()); + await this.adminService.setValue(instance.instanceId, 'AntiCSRFAttackState', undefined); + + // Inform the home for default .gbui after finishing token retrival. + res.redirect(min.instance.botEndpoint); } } @@ -375,6 +458,10 @@ export class GBMinService { }); } + /** + * Handle OAuth2 web service calls for authorization requests + * on https:////auth URL. + */ private handleOAuthRequests(server: any, min: GBMinInstance) { server.get(`/${min.instance.botId}/auth`, (req, res) => { let authorizationUrl = urlJoin( @@ -392,27 +479,38 @@ export class GBMinService { * Returns the instance object to clients requesting bot info. */ private async handleGetInstanceForClient(req: any, res: any) { - let botId = req.params.botId; - if (botId === '[default]' || botId === undefined) { - botId = GBConfigService.get('BOT_ID'); - } - let instance = await this.core.loadInstanceByBotId(botId); + // Translates the requested botId. + + let id = req.params.botId; + if (id === '[default]' || id === undefined) { + id = GBConfigService.get('BOT_ID'); + } + + // Loads by the botId itself or by the activationCode field. + + let instance = await this.core.loadInstanceByBotId(id); if (instance === null) { - instance = await this.core.loadInstanceByActivationCode(botId); + instance = await this.core.loadInstanceByActivationCode(id); } if (instance !== null) { + + // Gets the webchat token, speech token and theme. + const webchatTokenContainer = await this.getWebchatToken(instance); const speechToken = instance.speechKey != null ? await this.getSTSToken(instance) : null; let theme = instance.theme; + + // Sends all information to the .gbui web client. + if (theme === undefined) { theme = 'default.gbtheme'; } res.send( JSON.stringify({ instanceId: instance.instanceId, - botId: botId, + botId: id, theme: theme, webchatToken: webchatTokenContainer.token, speechToken: speechToken, @@ -422,17 +520,14 @@ export class GBMinService { }) ); } else { - const error = `Instance not found: ${botId}.`; + const error = `Instance not found while retrieving from .gbui web client: ${id}.`; res.sendStatus(error); GBLog.error(error); } } /** - * Get Webchat key from Bot Service. - * - * @param instance The Bot instance. - * + * Gets Webchat token from Bot Service. */ private async getWebchatToken(instance: any) { const options = { @@ -445,20 +540,15 @@ export class GBMinService { try { const json = await request(options); - return JSON.parse(json); } catch (error) { - const msg = `[botId:${instance.botId}] Error calling Direct Line client, verify Bot endpoint on the cloud. Error is: ${error}.`; - + const msg = `[botId:${instance.botId}] Error calling Direct Line to generate a token for Web control: ${error}.`; return Promise.reject(new Error(msg)); } } /** * Gets a Speech to Text / Text to Speech token from the provider. - * - * @param instance The general bot instance. - * */ private async getSTSToken(instance: any) { const options = { @@ -478,30 +568,25 @@ export class GBMinService { } } + /** + * Builds the BOT Framework & GB infrastructures. + */ private async buildBotAdapter(instance: any, sysPackages: IGBPackage[], appPackages: IGBPackage[]) { - const adapter = new BotFrameworkAdapter({ - appId: instance.marketplaceId, - appPassword: instance.marketplacePassword - }); - const storage = new MemoryStorage(); + // MSFT stuff. + + const adapter = new BotFrameworkAdapter({ appId: instance.marketplaceId, appPassword: instance.marketplacePassword }); + const storage = new MemoryStorage(); const conversationState = new ConversationState(storage); const userState = new UserState(storage); adapter.use(new AutoSaveStateMiddleware(conversationState, userState)); - - MicrosoftAppCredentials.trustServiceUrl( - 'https://directline.botframework.com', + MicrosoftAppCredentials.trustServiceUrl('https://directline.botframework.com', new Date(new Date().setFullYear(new Date().getFullYear() + 10)) ); // The minimal bot is built here. const min = new GBMinInstance(); - - if (GBServer.globals.minBoot === undefined) { - GBServer.globals.minBoot = min; - } - min.botId = instance.botId; min.bot = adapter; min.userState = userState; @@ -516,11 +601,13 @@ export class GBMinService { min.sandBoxMap = {}; min.packages = sysPackages; min.appPackages = appPackages; + if (GBServer.globals.minBoot === undefined) { + GBServer.globals.minBoot = min; + } // TODO: min.appPackages = core.getPackagesByInstanceId(min.instance.instanceId); - // Create a hub of services available in .gbapps. + // Creates a hub of services available in .gbapps. - let handled = false; await CollectionUtil.asyncForEach(min.appPackages, async (e: IGBPackage) => { let services: ConcatArray; if ((services = await e.onExchangeData(min, 'getServices', null))) { @@ -528,6 +615,9 @@ export class GBMinService { } }); + // If there is WhatsApp configuration specified, initialize + // infrastructure objects. + if (min.instance.whatsappServiceKey !== null) { min.whatsAppDirectLine = new WhatsappDirectLine( min, @@ -553,6 +643,8 @@ export class GBMinService { } } + // Setups default BOT Framework dialogs. + min.userProfile = conversationState.createProperty('userProfile'); const dialogState = conversationState.createProperty('dialogState'); @@ -571,7 +663,13 @@ export class GBMinService { return { min, adapter, conversationState }; } + /** + * Performs calling of loadBot event in all .gbapps. + */ private async invokeLoadBot(appPackages: IGBPackage[], sysPackages: IGBPackage[], min: GBMinInstance) { + + // Calls loadBot event in all .gbapp packages. + await CollectionUtil.asyncForEach(sysPackages, async p => { p.sysPackages = sysPackages; if (p.getDialogs !== undefined) { @@ -586,6 +684,8 @@ export class GBMinService { await p.loadBot(min); }); + // Adds all dialogs from .gbapps into global dialo list for this minimal instance. + await CollectionUtil.asyncForEach(appPackages, async p => { p.sysPackages = sysPackages; await p.loadBot(min); @@ -601,7 +701,7 @@ export class GBMinService { } /** - * Bot Service hook method. + * BOT Framework web service hook method. */ private async receiver( adapter: BotFrameworkAdapter, @@ -612,8 +712,13 @@ export class GBMinService { instance: any, appPackages: any[] ) { + + // Default activity processing and handler. + await adapter.processActivity(req, res, async context => { + // Get loaded user state + const step = await min.dialogs.createContext(context); step.context.activity.locale = 'pt-BR'; let firstTime = false; @@ -625,21 +730,29 @@ export class GBMinService { const sec = new SecService(); if (!user.loaded) { + user.loaded = true; + user.subjects = []; + user.cb = undefined; + user.welcomed = false; firstTime = true; + + // Sends loadInstance event to .gbui clients. + await min.conversationalService.sendEvent(min, step, 'loadInstance', { instanceId: instance.instanceId, botId: instance.botId, theme: instance.theme ? instance.theme : 'default.gbtheme', secret: instance.webchatKey }); - user.loaded = true; - user.subjects = []; - user.cb = undefined; - user.welcomed = false; + + // This same event is dispatched either to all participants + // including the bot, that is filtered bellow. if (context.activity.from.id !== min.botId) { - const member = context.activity.from; + // Creates a new row in user table if it does not exists. + + const member = context.activity.from; const persistedUser = await sec.ensureUser( instance.instanceId, member.id, @@ -648,61 +761,87 @@ export class GBMinService { 'web', member.name ); + + // Required for MSTEAMS handling of persisted conversations. + if (step.context.activity.channelId === 'msteams') { persistedUser.conversationReference = JSON.stringify( TurnContext.getConversationReference(context.activity) ); await persistedUser.save(); } - const analytics = new AnalyticsService(); + // Stores conversation associated to the user to group each message. + + const analytics = new AnalyticsService(); user.systemUser = persistedUser; user.conversation = await analytics.createConversation(persistedUser); + } + // Saves session user (persisted GuaribasUser is inside). + await min.userProfile.set(step.context, user); } - GBLog.info( - `User>: text:${context.activity.text} (type: ${context.activity.type}, name: ${context.activity.name}, channelId: ${context.activity.channelId}, value: ${context.activity.value})` - ); + GBLog.info(`User>: text:${context.activity.text} (type: ${context.activity.type}, name: ${context.activity.name}, channelId: ${context.activity.channelId}, value: ${context.activity.value})`); + + // Answer to specific BOT Framework event conversationUpdate to auto start dialogs. + if (context.activity.type === 'conversationUpdate' && context.activity.membersAdded.length > 0) { + + // Check if a bot or a human participant is being added to the conversation. + const member = context.activity.membersAdded[0]; if (context.activity.membersAdded[0].id === context.activity.recipient.id) { GBLog.info(`Bot added to conversation, starting chat...`); + + // Calls onNewSession event on each .gbapp package. + await CollectionUtil.asyncForEach(appPackages, async e => { await e.onNewSession(min, step); }); + // Auto starts dialogs if any is specified. + const startDialog = min.core.getParam(min.instance, 'Start Dialog', null); if (startDialog && !user.welcomed) { user.welcomed = true; GBLog.info(`Auto start dialog is now being called: ${startDialog}...`); await GBVMService.callVM(startDialog.toLowerCase(), min, step, this.deployer); } - else - { + else { + + // Otherwise, calls / (root) to default welcome users. + await step.beginDialog('/'); - } + } } else { GBLog.info(`Member added to conversation: ${member.name}`); } - // Processes messages. + } else if (context.activity.type === 'message') { - // Checks for /admin request. + + // Process messages activities. + await this.processMessageActivity(context, min, step); - // Processes events. } else if (context.activity.type === 'event') { - // Empties dialog stack before going to the target. + + // Processes events activies. await this.processEventActivity(context, step); } + + // Saves conversation state for later use. + await conversationState.saveChanges(context, true); + } catch (error) { + const msg = `ERROR: ${error.message} ${error.stack ? error.stack : ''}`; GBLog.error(msg); @@ -711,12 +850,17 @@ export class GBMinService { step, Messages[step.context.activity.locale].very_sorry_about_error ); + await step.beginDialog('/ask', { isReturning: true }); } }); } + /** + * Called to handle all event sent by .gbui clients. + */ private async processEventActivity(context, step: GBDialogStep) { + if (context.activity.name === 'whoAmI') { await step.beginDialog('/whoAmI'); } else if (context.activity.name === 'showSubjects') { @@ -744,19 +888,23 @@ export class GBMinService { } } + /** + * Called to handle all text messages sent and received by the bot. + */ private async processMessageActivity(context, min: GBMinInstance, step: GBDialogStep) { - const user = await min.userProfile.get(context, {}); // Removes Bot Id from MS Teams. context.activity.text = context.activity.text.trim(); context.activity.text = context.activity.text.replace(/\.*\<\/at\>\s/gi, '') + const user = await min.userProfile.get(context, {}); let message: GuaribasConversationMessage; if (process.env.PRIVACY_STORE_MESSAGES === 'true') { - const analytics = new AnalyticsService(); + // Adds message to the analytics layer. + const analytics = new AnalyticsService(); if (user) { if (!user.conversation) { @@ -778,12 +926,18 @@ export class GBMinService { return utterance.match(Messages.global_quit); }; + // Files in .gbdialog can be called directly by typing its name normalized into JS . + const isVMCall = Object.keys(min.scriptMap).find(key => min.scriptMap[key] === context.activity.text) !== undefined; - - if (isVMCall) { await GBVMService.callVM(context.activity.text, min, step, this.deployer); - } else if (context.activity.text.charAt(0) === '/') { + } + + // When user starts text by typing a slash, it can call either a dialog directly + // or /call