fix(all): MSTeams fixes.

This commit is contained in:
Rodrigo Rodriguez 2022-04-26 15:13:19 -03:00
parent 6778f2eb59
commit d6f8574ecb
5 changed files with 250 additions and 235 deletions

View file

@ -255,7 +255,8 @@ export class GBConversationalService {
} }
public async sendEvent(min: GBMinInstance, step: GBDialogStep, name: string, value: Object): Promise<any> { public async sendEvent(min: GBMinInstance, step: GBDialogStep, name: string, value: Object): Promise<any> {
if (!this.userMobile(step)) { if (!this.userMobile(step) &&
step.context.activity.channelId !== 'msteams') {
GBLog.info(`Sending event ${name}:${typeof value === 'object' ? JSON.stringify(value) : GBLog.info(`Sending event ${name}:${typeof value === 'object' ? JSON.stringify(value) :
value ? value : ''} to client...`); value ? value : ''} to client...`);
const msg = MessageFactory.text(''); const msg = MessageFactory.text('');

View file

@ -163,7 +163,7 @@ export class GBMinService {
setTimeout(resolve, ms); setTimeout(resolve, ms);
}); });
}; };
await sleep(10000); await sleep(20000);
res.status(200); res.status(200);
res.end(); res.end();
}); });
@ -889,8 +889,9 @@ export class GBMinService {
const sec = new SecService(); const sec = new SecService();
if (!user.loaded) { if (!user.loaded) {
if (step.context.activity.channelId !== 'msteams') {
await min.conversationalService.sendEvent(min, step, 'loadInstance', {}); await min.conversationalService.sendEvent(min, step, 'loadInstance', {});
}
user.loaded = true; user.loaded = true;
user.subjects = []; user.subjects = [];
@ -925,12 +926,14 @@ export class GBMinService {
} }
if (step.context.activity.channelId !== 'msteams') {
const service = new KBService(min.core.sequelize); const service = new KBService(min.core.sequelize);
const data = await service.getFaqBySubjectArray(instance.instanceId, 'faq', undefined); const data = await service.getFaqBySubjectArray(instance.instanceId, 'faq', undefined);
await min.conversationalService.sendEvent(min, step, 'play', { await min.conversationalService.sendEvent(min, step, 'play', {
playerType: 'bullet', playerType: 'bullet',
data: data.slice(0, 10) data: data.slice(0, 10)
}); });
}
// Saves session user (persisted GuaribasUser is inside). // Saves session user (persisted GuaribasUser is inside).

View file

@ -34,227 +34,228 @@
* @fileoverview General Bots server core. * @fileoverview General Bots server core.
*/ */
'use strict'; 'use strict';
import { BotAdapter } from 'botbuilder'; import { BotAdapter } from 'botbuilder';
import { WaterfallDialog } from 'botbuilder-dialogs'; import { WaterfallDialog } from 'botbuilder-dialogs';
import { GBMinInstance, IGBDialog } from 'botlib'; import { GBMinInstance, IGBDialog } from 'botlib';
import { GBMinService } from '../../core.gbapp/services/GBMinService'; import { GBMinService } from '../../core.gbapp/services/GBMinService';
import { AnalyticsService } from '../../analytics.gblib/services/AnalyticsService'; import { AnalyticsService } from '../../analytics.gblib/services/AnalyticsService';
import { SecService } from '../../security.gbapp/services/SecService'; import { SecService } from '../../security.gbapp/services/SecService';
import { CSService } from '../services/CSService'; import { CSService } from '../services/CSService';
import { Messages } from '../strings'; import { Messages } from '../strings';
/**
* Dialog for feedback collecting.
*/
export class FeedbackDialog extends IGBDialog {
/**
* Setup dialogs flows and define services call.
*
* @param bot The bot adapter.
* @param min The minimal bot instance data.
*/
public static setup(bot: BotAdapter, min: GBMinInstance) {
const service = new CSService();
min.dialogs.add(
new WaterfallDialog('/pleaseNoBadWords', [
async step => {
const locale = step.context.activity.locale;
await min.conversationalService.sendText(min, step, Messages[locale].please_no_bad_words);
return await step.next();
}
])
);
min.dialogs.add(
new WaterfallDialog('/t', [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
return await step.beginDialog('/auth');
}
else{
return await step.next(step.options);
}
},
async step => {
const locale = step.context.activity.locale;
const sec = new SecService();
let from = GBMinService.userMobile(step);
await min.conversationalService.sendText(min, step, Messages[locale].please_wait_transfering);
const agentSystemId = await sec.assignHumanAgent(from, min.instance.instanceId);
const user = await min.userProfile.get(step.context, {});
user.systemUser = await sec.getUserFromAgentSystemId(agentSystemId);
await min.userProfile.set(step.context, user);
if (agentSystemId.charAt(2) === ":" || agentSystemId.indexOf("@") > -1) { // Agent is from Teams or Google Chat.
const agent = await sec.getUserFromSystemId(agentSystemId);
await min.conversationalService['sendOnConversation'](min, agent,
Messages[locale].notify_agent(step.context.activity.from.name));
}
else {
await min.whatsAppDirectLine.sendToDevice(agentSystemId, Messages[locale].notify_agent(step.context.activity.from.name));
}
return await step.next();
}
])
);
min.dialogs.add(
new WaterfallDialog('/qt', [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
return await step.beginDialog('/auth');
}
else{
return await step.next(step.options);
}
},
async step => {
const locale = step.context.activity.locale;
const sec = new SecService();
const userSystemId = GBMinService.userMobile(step);
const user = await min.userProfile.get(step.context, {});
if (user.systemUser.agentMode === 'self') {
const manualUser = await sec.getUserFromAgentSystemId(userSystemId);
await min.whatsAppDirectLine.sendToDeviceEx(manualUser.userSystemId,
Messages[locale].notify_end_transfer(min.instance.botId), locale, step.context.activity.conversation.id);
if (userSystemId.charAt(2) === ":" || userSystemId.indexOf('@') > -1) { // Agent is from Teams or Google Chat.
await min.conversationalService.sendText(min, step, Messages[locale].notify_end_transfer(min.instance.botId));
}
else {
await min.whatsAppDirectLine.sendToDeviceEx(userSystemId,
Messages[locale].notify_end_transfer(min.instance.botId), locale
, step.context.activity.conversation.id);
}
await sec.updateHumanAgent(userSystemId, min.instance.instanceId, null);
await sec.updateHumanAgent(manualUser.userSystemId, min.instance.instanceId, null);
user.systemUser = await sec.getUserFromSystemId(userSystemId);
await min.userProfile.set(step.context, user);
} /**
* Dialog for feedback collecting.
else if (user.systemUser.agentMode === 'human') { */
const agent = await sec.getUserFromSystemId(user.systemUser.agentSystemId); export class FeedbackDialog extends IGBDialog {
/**
await min.whatsAppDirectLine.sendToDeviceEx(user.systemUser.userSystemId, * Setup dialogs flows and define services call.
Messages[locale].notify_end_transfer(min.instance.botId), locale, step.context.activity.conversation.id); *
* @param bot The bot adapter.
* @param min The minimal bot instance data.
if (user.systemUser.agentSystemId.charAt(2) === ":" || userSystemId.indexOf('@') > -1) { // Agent is from Teams or Google Chat. */
await min.conversationalService.sendText(min, step, Messages[locale].notify_end_transfer(min.instance.botId)); public static setup(bot: BotAdapter, min: GBMinInstance) {
} const service = new CSService();
else {
await min.whatsAppDirectLine.sendToDeviceEx(user.systemUser.agentSystemId, min.dialogs.add(
Messages[locale].notify_end_transfer(min.instance.botId), locale, step.context.activity.conversation.id); new WaterfallDialog('/pleaseNoBadWords', [
} async step => {
const locale = step.context.activity.locale;
await sec.updateHumanAgent(user.systemUser.userSystemId, min.instance.instanceId, null); await min.conversationalService.sendText(min, step, Messages[locale].please_no_bad_words);
await sec.updateHumanAgent(agent.userSystemId, min.instance.instanceId, null);
return await step.next();
user.systemUser = await sec.getUserFromSystemId(userSystemId); }
await min.userProfile.set(step.context, user); ])
);
}
else min.dialogs.add(
{ new WaterfallDialog('/t', [
if (user.systemUser.userSystemId.charAt(2) === ":" || userSystemId.indexOf('@') > -1) { // Agent is from Teams or Google Chat. async step => {
await min.conversationalService.sendText(min, step, 'Nenhum atendimento em andamento.'); if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
} return await step.beginDialog('/auth');
else { }
await min.whatsAppDirectLine.sendToDeviceEx(user.systemUser.userSystemId, else{
'Nenhum atendimento em andamento.', locale, step.context.activity.conversation.id); return await step.next(step.options);
} }
} },
async step => {
return await step.next();
} const locale = step.context.activity.locale;
])
); const sec = new SecService();
let from = GBMinService.userMobile(step);
min.dialogs.add(
new WaterfallDialog('/feedbackNumber', [ await min.conversationalService.sendText(min, step, Messages[locale].please_wait_transfering);
async step => { const agentSystemId = await sec.assignHumanAgent(min, from);
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
return await step.beginDialog('/auth'); const user = await min.userProfile.get(step.context, {});
} user.systemUser = await sec.getUserFromAgentSystemId(agentSystemId);
else{ await min.userProfile.set(step.context, user);
return await step.next(step.options);
} if (agentSystemId.charAt(2) === ":" || agentSystemId.indexOf("@") > -1) { // Agent is from Teams or Google Chat.
}, const agent = await sec.getUserFromSystemId(agentSystemId);
async step => { await min.conversationalService['sendOnConversation'](min, agent,
const locale = step.context.activity.locale; Messages[locale].notify_agent(step.context.activity.from.name));
return await step.next(); }
}, else {
async step => { await min.whatsAppDirectLine.sendToDevice(agentSystemId, Messages[locale].notify_agent(step.context.activity.from.name));
const locale = step.context.activity.locale;
const rate = step.result.entity; }
const user = await min.userProfile.get(step.context, {}); return await step.next();
await service.updateConversationRate(user.conversation, rate); }
await min.conversationalService.sendText(min, step, Messages[locale].thanks); ])
);
return await step.next();
} min.dialogs.add(
]) new WaterfallDialog('/qt', [
); async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
min.dialogs.add( return await step.beginDialog('/auth');
new WaterfallDialog('/feedback', [ }
async step => { else{
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) { return await step.next(step.options);
return await step.beginDialog('/auth'); }
} },
else{ async step => {
return await step.next(step.options);
} const locale = step.context.activity.locale;
},
async step => { const sec = new SecService();
const locale = step.context.activity.locale; const userSystemId = GBMinService.userMobile(step);
const user = await min.userProfile.get(step.context, {});
await min.conversationalService.sendText(min, step, Messages[locale].about_suggestions);
step.activeDialog.state.cbId = (step.options as any).id; if (user.systemUser.agentMode === 'self') {
const manualUser = await sec.getUserFromAgentSystemId(userSystemId);
return await min.conversationalService.prompt(min, step, Messages[locale].what_about_service);
}, await min.whatsAppDirectLine.sendToDeviceEx(manualUser.userSystemId,
async step => { Messages[locale].notify_end_transfer(min.instance.botId), locale, step.context.activity.conversation.id);
const fixedLocale = 'en-US';
const user = await min.userProfile.get(step.context, {}); if (userSystemId.charAt(2) === ":" || userSystemId.indexOf('@') > -1) { // Agent is from Teams or Google Chat.
await min.conversationalService.sendText(min, step, Messages[locale].notify_end_transfer(min.instance.botId));
// Updates values to perform Bot Analytics. }
else {
const analytics = new AnalyticsService(); await min.whatsAppDirectLine.sendToDeviceEx(userSystemId,
const rate = await analytics.updateConversationSuggestion( Messages[locale].notify_end_transfer(min.instance.botId), locale
min.instance.instanceId, user.conversation.conversationId, step.result, user.systemUser.locale); , step.context.activity.conversation.id);
}
if (rate > 0.5) {
await min.conversationalService.sendText(min, step, Messages[fixedLocale].glad_you_liked); await sec.updateHumanAgent(userSystemId, min.instance.instanceId, null);
} else { await sec.updateHumanAgent(manualUser.userSystemId, min.instance.instanceId, null);
const message = min.core.getParam<string>(min.instance, 'Feedback Improve Message', user.systemUser = await sec.getUserFromSystemId(userSystemId);
Messages[fixedLocale].we_will_improve); // TODO: Improve to be multi-language. await min.userProfile.set(step.context, user);
await min.conversationalService.sendText(min, step, message); }
}
else if (user.systemUser.agentMode === 'human') {
return await step.replaceDialog('/ask', { isReturning: true }); const agent = await sec.getUserFromSystemId(user.systemUser.agentSystemId);
}
]) await min.whatsAppDirectLine.sendToDeviceEx(user.systemUser.userSystemId,
); Messages[locale].notify_end_transfer(min.instance.botId), locale, step.context.activity.conversation.id);
}
}
if (user.systemUser.agentSystemId.charAt(2) === ":" || userSystemId.indexOf('@') > -1) { // Agent is from Teams or Google Chat.
await min.conversationalService.sendText(min, step, Messages[locale].notify_end_transfer(min.instance.botId));
}
else {
await min.whatsAppDirectLine.sendToDeviceEx(user.systemUser.agentSystemId,
Messages[locale].notify_end_transfer(min.instance.botId), locale, step.context.activity.conversation.id);
}
await sec.updateHumanAgent(user.systemUser.userSystemId, min.instance.instanceId, null);
await sec.updateHumanAgent(agent.userSystemId, min.instance.instanceId, null);
user.systemUser = await sec.getUserFromSystemId(userSystemId);
await min.userProfile.set(step.context, user);
}
else
{
if (user.systemUser.userSystemId.charAt(2) === ":" || userSystemId.indexOf('@') > -1) { // Agent is from Teams or Google Chat.
await min.conversationalService.sendText(min, step, 'Nenhum atendimento em andamento.');
}
else {
await min.whatsAppDirectLine.sendToDeviceEx(user.systemUser.userSystemId,
'Nenhum atendimento em andamento.', locale, step.context.activity.conversation.id);
}
}
return await step.next();
}
])
);
min.dialogs.add(
new WaterfallDialog('/feedbackNumber', [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
return await step.beginDialog('/auth');
}
else{
return await step.next(step.options);
}
},
async step => {
const locale = step.context.activity.locale;
return await step.next();
},
async step => {
const locale = step.context.activity.locale;
const rate = step.result.entity;
const user = await min.userProfile.get(step.context, {});
await service.updateConversationRate(user.conversation, rate);
await min.conversationalService.sendText(min, step, Messages[locale].thanks);
return await step.next();
}
])
);
min.dialogs.add(
new WaterfallDialog('/feedback', [
async step => {
if (step.context.activity.channelId !== 'msteams' && process.env.ENABLE_AUTH) {
return await step.beginDialog('/auth');
}
else{
return await step.next(step.options);
}
},
async step => {
const locale = step.context.activity.locale;
await min.conversationalService.sendText(min, step, Messages[locale].about_suggestions);
step.activeDialog.state.cbId = (step.options as any).id;
return await min.conversationalService.prompt(min, step, Messages[locale].what_about_service);
},
async step => {
const fixedLocale = 'en-US';
const user = await min.userProfile.get(step.context, {});
// Updates values to perform Bot Analytics.
const analytics = new AnalyticsService();
const rate = await analytics.updateConversationSuggestion(
min.instance.instanceId, user.conversation.conversationId, step.result, user.systemUser.locale);
if (rate > 0.5) {
await min.conversationalService.sendText(min, step, Messages[fixedLocale].glad_you_liked);
} else {
const message = min.core.getParam<string>(min.instance, 'Feedback Improve Message',
Messages[fixedLocale].we_will_improve); // TODO: Improve to be multi-language.
await min.conversationalService.sendText(min, step, message);
}
return await step.replaceDialog('/ask', { isReturning: true });
}
])
);
}
}

View file

@ -188,9 +188,9 @@ export class AskDialog extends IGBDialog {
const locale = step.context.activity.locale; const locale = step.context.activity.locale;
// Stops any content on projector. // Stops any content on projector.
if (step.context.activity.channelId !== 'msteams') {
await min.conversationalService.sendEvent(min, step, 'stop', undefined); await min.conversationalService.sendEvent(min, step, 'stop', undefined);
}
// Handle extra text from FAQ. // Handle extra text from FAQ.
if (step.options && step.options.query) { if (step.options && step.options.query) {

View file

@ -2,7 +2,7 @@ const Fs = require('fs');
const urlJoin = require('url-join'); const urlJoin = require('url-join');
import { ConversationReference } from 'botbuilder'; import { ConversationReference } from 'botbuilder';
import { GBLog, GBService, IGBInstance } from 'botlib'; import { GBLog, GBMinInstance, GBService, IGBInstance } from 'botlib';
import { CollectionUtil } from 'pragmatismo-io-framework'; import { CollectionUtil } from 'pragmatismo-io-framework';
import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models'; import { GuaribasGroup, GuaribasUser, GuaribasUserGroup } from '../models';
import {FindOptions} from 'sequelize'; import {FindOptions} from 'sequelize';
@ -167,21 +167,31 @@ export class SecService extends GBService {
return user.agentMode === 'self'; return user.agentMode === 'self';
} }
public async assignHumanAgent(userSystemId: string, instanceId: number): Promise<string> { public async assignHumanAgent(min: GBMinInstance, userSystemId: string): Promise<string> {
let agentSystemId; let agentSystemId;
const list = process.env.TRANSFER_TO.split(';');
let list = min.core.getParam<string>(
min.instance,
'Transfer To',
process.env.TRANSFER_TO
);
if (list){
list = list.split(';')
}
await CollectionUtil.asyncForEach(list, async item => { await CollectionUtil.asyncForEach(list, async item => {
if ( if (
!(item !== undefined && item !== undefined &&
agentSystemId === undefined && agentSystemId === undefined &&
item !== userSystemId && await this.isAgentSystemId(item)) item !== userSystemId && !await this.isAgentSystemId(item)
) { ) {
// TODO: Optimize loop. // TODO: Optimize loop.
agentSystemId = item; agentSystemId = item;
} }
}); });
GBLog.info(`Selected agentId: ${agentSystemId}`); GBLog.info(`Selected agentId: ${agentSystemId}`);
await this.updateHumanAgent(userSystemId, instanceId, agentSystemId); await this.updateHumanAgent(userSystemId, min.instance.instanceId, agentSystemId);
GBLog.info(`Updated agentId to: ${agentSystemId}`); GBLog.info(`Updated agentId to: ${agentSystemId}`);
return agentSystemId; return agentSystemId;