Skip to content

Commit

Permalink
feat(whatsapp.gblib): Switch from Whatsapp channel which bot to talk …
Browse files Browse the repository at this point in the history
…to with the same number.
  • Loading branch information
rodrigorodriguez committed Aug 23, 2019
1 parent cb3d241 commit 650779e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 67 deletions.
135 changes: 91 additions & 44 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"azure-arm-search": "1.3.0-preview",
"azure-arm-sql": "5.7.0",
"azure-arm-website": "5.7.0",
"azure-search-client": "^3.1.5",
"bluebird": "3.5.4",
"body-parser": "1.19.0",
"botbuilder": "4.4.0",
Expand Down
1 change: 0 additions & 1 deletion packages/admin.gbapp/dialogs/AdminDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export class AdminDialog extends IGBDialog {
await s.downloadFolder(localFolder, siteName, folderName,
GBConfigService.get('CLOUD_USERNAME'), GBConfigService.get('CLOUD_PASSWORD'))
await deployer.deployPackage(min, localFolder);
rimraf.sync(localFolder);
}
}

Expand Down
6 changes: 5 additions & 1 deletion packages/core.gbapp/dialogs/SwitchBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import { WaterfallDialog } from 'botbuilder-dialogs';
import { GBMinInstance, IGBDialog } from 'botlib';
import { Messages } from '../strings';
import { SecService } from '../../security.gblib/services/SecService';
import { GBServer } from '../../../src/app';
/**
* Dialog for the bot explains about itself.
*/
Expand All @@ -62,7 +63,10 @@ export class SwitchBotDialog extends IGBDialog {
async step => {
let sec = new SecService();
let from = step.context.activity.from.id;
await sec.updateCurrentBotId(min.instance.instanceId, from, step.result);
const minBoot = GBServer.globals.minInstances[0];
await sec.updateCurrentBotId(minBoot.instance.instanceId, from, step.result);
await step.context.sendActivity(`Opa, vamos lá!`);

return await step.next();
}
]));
Expand Down
16 changes: 14 additions & 2 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,19 @@ export class GBDeployer {

} else {

console.log(GBServer.globals.bootInstance);
instance = Object.assign(instance, GBServer.globals.bootInstance);
let botId = GBConfigService.get('BOT_ID');
let bootInstance = await this.core.loadInstance(botId);

instance.searchHost = bootInstance.searchHost;
instance.searchIndex = bootInstance.searchIndex;
instance.searchIndexer = bootInstance.searchIndexer;
instance.searchKey = bootInstance.searchKey;
instance.whatsappServiceKey = bootInstance.whatsappServiceKey;
instance.whatsappBotKey = bootInstance.whatsappBotKey;
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;
instance.whatsappServiceWebhookUrl = bootInstance.whatsappServiceWebhookUrl;

instance = await service.internalDeployBot(
instance,
accessToken,
Expand All @@ -212,6 +223,7 @@ export class GBDeployer {
await GBServer.globals.minService.mountBot(instance);
}
await this.core.saveInstance(instance);

}


Expand Down
17 changes: 12 additions & 5 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,17 @@ export class GBMinService {
}
const url = '/webhooks/whatsapp';
GBServer.globals.server.post(url, async (req, res) => {
const from = req.body.messages[0].chatId.split('@')[0];
const id = req.body.messages[0].chatId.split('@')[0];

let sec = new SecService();
let user = await sec.getUserFromPhone(from);

const minBoot = GBServer.globals.minInstances[0];
let user = await sec.getUserFromPhone(id);
if (user === null) {
user = await sec.ensureUser(minBoot.instance.instanceId, id,
minBoot.botId, id, "", "whatsapp", id, id);
}

let botId = user.currentBotId;
const min = GBServer.globals.minInstances.filter(p => p.botId === botId)[0];
(min as any).whatsAppDirectLine.received(req, res);
Expand Down Expand Up @@ -163,7 +170,7 @@ export class GBMinService {
GBServer.globals.minInstances.push(min);

// Install default VBA module.
//this.deployer.deployPackage(min, 'packages/default.gbdialog');
// this.deployer.deployPackage(min, 'packages/default.gbdialog');

// Call the loadBot context.activity for all packages.
this.invokeLoadBot(GBServer.globals.appPackages, GBServer.globals.sysPackages, min, GBServer.globals.server);
Expand Down Expand Up @@ -427,7 +434,7 @@ export class GBMinService {
const member = context.activity.membersAdded[0];

await sec.ensureUser(instance.instanceId, member.id,
min.botId, member.id, "", "web", member.name, member.id);
min.botId, member.id, "", "web", member.name, member.id);
}

GBLog.info(
Expand Down
28 changes: 15 additions & 13 deletions packages/kb.gbapp/services/KBService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ const asyncPromise = require('async-promises');
const walkPromise = require('walk-promise');
// tslint:disable-next-line:newline-per-chained-call
const parse = require('bluebird').promisify(require('csv-parse'));
const { SearchService } = require('azure-search-client');

import { GBDialogStep, GBLog, IGBConversationalService, IGBCoreService, IGBInstance } from 'botlib';
import { AzureSearch } from 'pragmatismo-io-framework';
Expand Down Expand Up @@ -169,22 +170,23 @@ export class KBService {
query = `${query} ${text}`;
}
}
// TODO: query = `${query}&$filter=instanceId eq ${instance.instanceId}`;


// tslint:disable:no-unsafe-any
if (instance.searchKey !== null && GBConfigService.get('STORAGE_DIALECT') === 'mssql') {
const service = new AzureSearch(
instance.searchKey,
instance.searchHost,
instance.searchIndex,
instance.searchIndexer
);
const results = await service.search(query);

if (results && results.length > 0 && results[0]['@search.score'] >= searchScore) {
const value = await this.getAnswerById(instance.instanceId, results[0].answerId);
const client = new SearchService(instance.searchHost.split('.')[0], instance.searchKey );
const results = await client.indexes.use(instance.searchIndex)
.buildQuery()
.filter((f) => f.eq('instanceId', instance.instanceId))
.search(query)
.top(1)
.executeQuery();

const values = results.result.value;

if (values && values.length > 0 && values[0]['@search.score'] >= searchScore) {
const value = await this.getAnswerById(instance.instanceId, values[0].answerId);
if (value !== null) {
return Promise.resolve({ answer: value, questionId: results[0].questionId });
return Promise.resolve({ answer: value, questionId: values[0].questionId });
} else {
return Promise.resolve({ answer: undefined, questionId: 0 });
}
Expand Down
2 changes: 1 addition & 1 deletion packages/security.gblib/services/SecService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class SecService extends GBService {
currentBotId: string
): Promise<GuaribasUser> {
let user = await GuaribasUser.findOne({
where: {
where: {
instanceId: instanceId,
userSystemId: userSystemId
}
Expand Down

0 comments on commit 650779e

Please sign in to comment.