Skip to content

Commit

Permalink
fix(whatsapp.gblib): Fixing loop behaviour.
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorodriguez committed Jun 18, 2019
1 parent d61d2f8 commit 10d2a4a
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 27 deletions.
22 changes: 16 additions & 6 deletions packages/core.gbapp/services/GBCoreService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,15 @@ STORAGE_SYNC=true
const instance = instances[0];
if (process.env.NODE_ENV === 'development') {
GBLog.info(`Updating bot endpoint to local reverse proxy (ngrok)...`);
await installationDeployer.updateBotProxy(
instance.botId,
instance.botId,
`${proxyAddress}/api/messages/${instance.botId}`
);
try {
await installationDeployer.updateBotProxy(
instance.botId,
instance.botId,
`${proxyAddress}/api/messages/${instance.botId}`
);
} catch (error) {
throw new Error(`Error updating bot proxy with proxy address${error.message}.`);
}
}
} catch (error) {
if (error.parent === undefined) {
Expand Down Expand Up @@ -356,7 +360,7 @@ STORAGE_SYNC=true
// NOTE: if there is any code before this line a semicolon
// will be necessary before this line.
// Loads all system packages.

const sysPackages: IGBPackage[] = [];
[
GBAdminPackage,
GBAnalyticsPackage,
Expand All @@ -367,9 +371,15 @@ STORAGE_SYNC=true
GBWhatsappPackage
].forEach(e => {
GBLog.info(`Loading sys package: ${e.name}...`);

const p = Object.create(e.prototype) as IGBPackage;
if (e.name === 'GBWhatsappPackage') {
sysPackages.push(p);
}
p.loadPackage(core, core.sequelize);
});

return sysPackages;
}

public ensureAdminIsSecured() {
Expand Down
22 changes: 12 additions & 10 deletions packages/core.gbapp/services/GBMinService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,12 @@

'use strict';

const { DialogSet, TextPrompt } = require('botbuilder-dialogs');
import urlJoin = require('url-join');
const { DialogSet, TextPrompt } = require('botbuilder-dialogs');
const express = require('express');

const request = require('request-promise-native');
const AuthenticationContext = require('adal-node').AuthenticationContext;

import { AutoSaveStateMiddleware, BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } from 'botbuilder';

import { ConfirmPrompt, WaterfallDialog } from 'botbuilder-dialogs';
import {
GBDialogStep,
Expand All @@ -57,6 +54,8 @@ import {
IGBPackage
} from 'botlib';

import { MicrosoftAppCredentials } from 'botframework-connector';
import { GBServer } from '../../../src/app';
import { GBAnalyticsPackage } from '../../analytics.gblib';
import { GBCorePackage } from '../../core.gbapp';
import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp';
Expand All @@ -66,8 +65,8 @@ import { GBSecurityPackage } from '../../security.gblib';
import { GBWhatsappPackage } from '../../whatsapp.gblib';
import { Messages } from '../strings';
import { GBAdminPackage } from './../../admin.gbapp/index';
import { GBDeployer } from './GBDeployer';
import { GBConfigService } from './GBConfigService';
import { GBDeployer } from './GBDeployer';

/**
* Minimal service layer for a bot.
Expand Down Expand Up @@ -319,12 +318,16 @@ export class GBMinService {
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',
new Date(new Date().setFullYear(new Date().getFullYear() + 10)));


// The minimal bot is built here.

const min = new GBMinInstance();
Expand All @@ -338,6 +341,7 @@ export class GBMinService {
min.cbMap = {};
min.scriptMap = {};
min.sandBoxMap = {};
min.packages = GBServer.globals.sysPackages[0]; // HACK: Whatsapp now.
min.userProfile = conversationState.createProperty('userProfile');
const dialogState = conversationState.createProperty('dialogState');

Expand Down Expand Up @@ -401,6 +405,7 @@ export class GBMinService {
step.context.activity.locale = 'pt-BR';

try {

const user = await min.userProfile.get(context, {});

if (!user.loaded) {
Expand All @@ -423,14 +428,11 @@ export class GBMinService {
);
if (context.activity.type === 'conversationUpdate' && context.activity.membersAdded.length > 0) {
const member = context.activity.membersAdded[0];
if (member.name === 'GeneralBots') {
if (member.name === min.instance.title) {
GBLog.info(`Bot added to conversation, starting chat...`);
appPackages.forEach(e => {
e.onNewSession(min, step);
});
// Processes the root dialog.

await step.beginDialog('/');
} else {
GBLog.info(`Member added to conversation: ${member.name}`);
}
Expand Down
3 changes: 1 addition & 2 deletions packages/whatsapp.gblib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ export class GBWhatsappPackage implements IGBPackage {
min.instance.whatsappBotKey,
min.instance.whatsappServiceKey,
min.instance.whatsappServiceNumber,
min.instance.whatsappServiceUrl,
min.instance.botId
min.instance.whatsappServiceUrl
);
}
}
Expand Down
16 changes: 8 additions & 8 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class WhatsappDirectLine extends GBService {
const result = request.post(options);
GBLog.info(result);
} catch (error) {
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error}`);
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
}
});
}
Expand All @@ -92,7 +92,7 @@ export class WhatsappDirectLine extends GBService {
.then(response => {
return response.obj.conversationId;
}).catch(err => {
GBLog.error(`Error calling Conversations_StartConversation on Whatsapp channel ${err}`);
GBLog.error(`Error calling Conversations_StartConversation on Whatsapp channel ${err.data}`);
})

.then(generatedConversationId => {
Expand All @@ -102,14 +102,14 @@ export class WhatsappDirectLine extends GBService {
this.pollMessages(client, generatedConversationId, from, fromName);
})
.catch(err => {
GBLog.error(`Error starting conversation ${err}`);
GBLog.error(`Error starting conversation ${err.data}`);
});
} else {
this.inputMessage(client, conversationId, text, from, fromName);
}
res.end();
}).catch(err => {
GBLog.error(`Error initializing DirectLine for Whatsapp channel ${err}`);
GBLog.error(`Error initializing DirectLine for Whatsapp channel ${err.data}`);
});

}
Expand All @@ -128,7 +128,7 @@ export class WhatsappDirectLine extends GBService {
replyToId: from
}
}).catch(err => {
GBLog.error(`GBWhatsapp: Error receiving message: ${err}.`);
GBLog.error(`GBWhatsapp: Error receiving message: ${err.data}.`);
});
}

Expand All @@ -147,13 +147,13 @@ export class WhatsappDirectLine extends GBService {
return response.obj.activities;
})
.catch(err => {
GBLog.error(`Error calling Conversations_GetActivities on Whatsapp channel ${err}`);
GBLog.error(`Error calling Conversations_GetActivities on Whatsapp channel ${err.data}`);
})
.then(activities => {
this.printMessages(activities, conversationId, from, fromName);
})
.catch(err => {
GBLog.error(`Error calling printMessages on Whatsapp channel ${err}`);
GBLog.error(`Error calling printMessages on Whatsapp channel ${err.data}`);
});

}, this.pollInterval);
Expand Down Expand Up @@ -225,7 +225,7 @@ export class WhatsappDirectLine extends GBService {
const result = request.post(options);
GBLog.info(result);
} catch (error) {
GBLog.error(`Error sending message to Whatsapp provider ${error}`);
GBLog.error(`Error sending message to Whatsapp provider ${error.message}`);
}
}
}
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const appPackages: IGBPackage[] = [];
export class RootData {
public publicAddress: string;
public server: any;
sysPackages: any;
}

/**
Expand Down Expand Up @@ -122,7 +123,7 @@ export class GBServer {
// Deploys system and user packages.

GBLog.info(`Deploying packages...`);
core.loadSysPackages(core);
GBServer.globals.sysPackages = core.loadSysPackages(core);
await core.checkStorage(azureDeployer);
await deployer.deployPackages(core, server, appPackages);

Expand Down

0 comments on commit 10d2a4a

Please sign in to comment.