Skip to content

Commit

Permalink
feat(whatsapp.gblib): Now Whatsapp will display markdown from .gbkb i…
Browse files Browse the repository at this point in the history
…ncluding images.
  • Loading branch information
rodrigorodriguez committed Aug 24, 2019
1 parent 246b222 commit faa5ec7
Show file tree
Hide file tree
Showing 8 changed files with 222 additions and 100 deletions.
1 change: 0 additions & 1 deletion packages/core.gbapp/dialogs/SwitchBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export class SwitchBotDialog extends IGBDialog {
async step => {
let sec = new SecService();
let from = step.context.activity.from.id;
const minBoot = GBServer.globals.minInstances[0];
await sec.updateCurrentBotId(from, step.result);
await step.context.sendActivity(`Opa, vamos lá!`);

Expand Down
10 changes: 8 additions & 2 deletions packages/core.gbapp/services/GBConversationalService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,17 @@ export class GBConversationalService implements IGBConversationalService {
}

public async sendFile(min: GBMinInstance, step: GBDialogStep, url: string): Promise<any> {
let mobile = step.context.activity.from.id;
min.whatsAppDirectLine.sendFile(mobile, url);
const mobile = step.context.activity.from.id;
const filename = url.substring(url.lastIndexOf('/')+1);
await min.whatsAppDirectLine.sendFileToDevice(mobile, url, filename);

}

public async sendAudio(min: GBMinInstance, step: GBDialogStep, url: string): Promise<any> {
const mobile = step.context.activity.from.id;
await min.whatsAppDirectLine.sendAudioToDevice(mobile, url);
}

public async sendEvent(step: GBDialogStep, name: string, value: Object): Promise<any> {
if (step.context.activity.channelId === 'webchat') {
const msg = MessageFactory.text('');
Expand Down
15 changes: 12 additions & 3 deletions packages/core.gbapp/services/GBDeployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,10 @@ export class GBDeployer {
instance.whatsappServiceNumber = bootInstance.whatsappServiceNumber;
instance.whatsappServiceUrl = bootInstance.whatsappServiceUrl;
instance.whatsappServiceWebhookUrl = bootInstance.whatsappServiceWebhookUrl;
instance.storageServer = bootInstance.storageServer;
instance.storageName = bootInstance.storageName;
instance.storageUsername = bootInstance.storageUsername;
instance.storagePassword = bootInstance.storagePassword;

instance = await service.internalDeployBot(
instance,
Expand Down Expand Up @@ -422,9 +426,7 @@ export class GBDeployer {
server.use(`/themes/${filenameOnly}`, express.static(filename));
GBLog.info(`Theme (.gbtheme) assets accessible at: /themes/${filenameOnly}.`);
} else if (Path.extname(filename) === '.gbkb') {
server.use(`/kb/${filenameOnly}/subjects`, express.static(urlJoin(filename, 'subjects')));
server.use(`/kb/${filenameOnly}/images`, express.static(urlJoin(filename, 'images')));
GBLog.info(`KB (.gbkb) assets accessible at: /kb/${filenameOnly}.`);
this.mountGBKBAssets( filenameOnly, filename);
} else if (Path.extname(filename) === '.gbui') {
// Already Handled
} else if (Path.extname(filename) === '.gbdialog') {
Expand Down Expand Up @@ -458,6 +460,13 @@ export class GBDeployer {
return { generalPackages, totalPackages };
}

private mountGBKBAssets(packageName: any, filename: string) {
GBServer.globals.server.use(`/kb/${packageName}/subjects`, express.static(urlJoin(filename, 'subjects')));
GBServer.globals.server.use(`/kb/${packageName}/images`, express.static(urlJoin(filename, 'images')));
GBServer.globals.server.use(`/kb/${packageName}/audios`, express.static(urlJoin(filename, 'audios')));
GBLog.info(`KB (.gbkb) assets accessible at: /kb/${packageName}.`);
}

private isSystemPackage(name: string): Boolean {
const names = ['core.gbapp', 'admin.gbapp', 'azuredeployer.gbapp', 'customer-satisfaction.gbapp', 'kb.gbapp'];

Expand Down
17 changes: 11 additions & 6 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ export class GBMinService {
return; // Exit here.
}

const minBoot = GBServer.globals.bootInstance;
const toSwitchMin = GBServer.globals.minInstances.filter(p => p.botId === text)[0];
let activeMin = toSwitchMin ? toSwitchMin : minBoot;
let activeMin = toSwitchMin ? toSwitchMin : GBServer.globals.minBoot;

let sec = new SecService();
let user = await sec.getUserFromPhone(id);
Expand Down Expand Up @@ -190,6 +189,10 @@ export class GBMinService {

// Build bot adapter.
const { min, adapter, conversationState } = await this.buildBotAdapter(instance, GBServer.globals.publicAddress, GBServer.globals.sysPackages);

if (GBServer.globals.minInstances.length === 0) {
GBServer.globals.minBoot = min;
}
GBServer.globals.minInstances.push(min);

// Install default VBA module.
Expand Down Expand Up @@ -453,11 +456,13 @@ export class GBMinService {
user.cb = undefined;
await min.userProfile.set(step.context, user);

let sec = new SecService();
const member = context.activity.membersAdded[0];
if (context.activity.membersAdded !== undefined) {
let sec = new SecService();
const member = context.activity.membersAdded[0];

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

GBLog.info(
Expand Down
8 changes: 4 additions & 4 deletions packages/kb.gbapp/dialogs/AskDialog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class AskDialog extends IGBDialog {

// Sends the answer to all outputs, including projector.

await service.sendAnswer(AskDialog.getChannel(step), min.conversationalService, step, resultsA.answer);
await service.sendAnswer(min, AskDialog.getChannel(step), step, resultsA.answer);

// Goes to ask loop, again.
return await step.replaceDialog('/ask', { isReturning: true });
Expand All @@ -164,7 +164,7 @@ export class AskDialog extends IGBDialog {
await step.context.sendActivity(Messages[locale].wider_answer);
}
// Sends the answer to all outputs, including projector.
await service.sendAnswer(AskDialog.getChannel(step), min.conversationalService, step, resultsB.answer);
await service.sendAnswer(min, AskDialog.getChannel(step), step, resultsB.answer);

return await step.replaceDialog('/ask', { isReturning: true });
} else {
Expand All @@ -180,7 +180,7 @@ export class AskDialog extends IGBDialog {
}

private static getChannel(step): string {
return Number.isInteger(step.context.activity.from.id) ? 'whatsapp' : step.context.activity.channelId;
return !isNaN(step.context.activity.from.id) ? 'whatsapp' : step.context.activity.channelId;
}

private static getAnswerEventDialog(service: KBService, min: GBMinInstance) {
Expand All @@ -191,7 +191,7 @@ export class AskDialog extends IGBDialog {
const question = await service.getQuestionById(min.instance.instanceId, data.questionId);
const answer = await service.getAnswerById(min.instance.instanceId, question.answerId);
// Sends the answer to all outputs, including projector.
await service.sendAnswer(AskDialog.getChannel(step), min.conversationalService, step, answer);
await service.sendAnswer(min, AskDialog.getChannel(step), step, answer);
await step.replaceDialog('/ask', { isReturning: true });
}

Expand Down
Loading

0 comments on commit faa5ec7

Please sign in to comment.