fix(whatsapp.gblib): Fixing loop behaviour.
This commit is contained in:
parent
d61d2f86e8
commit
10d2a4afb9
5 changed files with 39 additions and 27 deletions
|
@ -305,11 +305,15 @@ STORAGE_SYNC=true
|
||||||
const instance = instances[0];
|
const instance = instances[0];
|
||||||
if (process.env.NODE_ENV === 'development') {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
GBLog.info(`Updating bot endpoint to local reverse proxy (ngrok)...`);
|
GBLog.info(`Updating bot endpoint to local reverse proxy (ngrok)...`);
|
||||||
|
try {
|
||||||
await installationDeployer.updateBotProxy(
|
await installationDeployer.updateBotProxy(
|
||||||
instance.botId,
|
instance.botId,
|
||||||
instance.botId,
|
instance.botId,
|
||||||
`${proxyAddress}/api/messages/${instance.botId}`
|
`${proxyAddress}/api/messages/${instance.botId}`
|
||||||
);
|
);
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(`Error updating bot proxy with proxy address${error.message}.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.parent === undefined) {
|
if (error.parent === undefined) {
|
||||||
|
@ -356,7 +360,7 @@ STORAGE_SYNC=true
|
||||||
// NOTE: if there is any code before this line a semicolon
|
// NOTE: if there is any code before this line a semicolon
|
||||||
// will be necessary before this line.
|
// will be necessary before this line.
|
||||||
// Loads all system packages.
|
// Loads all system packages.
|
||||||
|
const sysPackages: IGBPackage[] = [];
|
||||||
[
|
[
|
||||||
GBAdminPackage,
|
GBAdminPackage,
|
||||||
GBAnalyticsPackage,
|
GBAnalyticsPackage,
|
||||||
|
@ -367,9 +371,15 @@ STORAGE_SYNC=true
|
||||||
GBWhatsappPackage
|
GBWhatsappPackage
|
||||||
].forEach(e => {
|
].forEach(e => {
|
||||||
GBLog.info(`Loading sys package: ${e.name}...`);
|
GBLog.info(`Loading sys package: ${e.name}...`);
|
||||||
|
|
||||||
const p = Object.create(e.prototype) as IGBPackage;
|
const p = Object.create(e.prototype) as IGBPackage;
|
||||||
|
if (e.name === 'GBWhatsappPackage') {
|
||||||
|
sysPackages.push(p);
|
||||||
|
}
|
||||||
p.loadPackage(core, core.sequelize);
|
p.loadPackage(core, core.sequelize);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return sysPackages;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ensureAdminIsSecured() {
|
public ensureAdminIsSecured() {
|
||||||
|
|
|
@ -36,15 +36,12 @@
|
||||||
|
|
||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { DialogSet, TextPrompt } = require('botbuilder-dialogs');
|
|
||||||
import urlJoin = require('url-join');
|
import urlJoin = require('url-join');
|
||||||
|
const { DialogSet, TextPrompt } = require('botbuilder-dialogs');
|
||||||
const express = require('express');
|
const express = require('express');
|
||||||
|
|
||||||
const request = require('request-promise-native');
|
const request = require('request-promise-native');
|
||||||
const AuthenticationContext = require('adal-node').AuthenticationContext;
|
const AuthenticationContext = require('adal-node').AuthenticationContext;
|
||||||
|
|
||||||
import { AutoSaveStateMiddleware, BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } from 'botbuilder';
|
import { AutoSaveStateMiddleware, BotFrameworkAdapter, ConversationState, MemoryStorage, UserState } from 'botbuilder';
|
||||||
|
|
||||||
import { ConfirmPrompt, WaterfallDialog } from 'botbuilder-dialogs';
|
import { ConfirmPrompt, WaterfallDialog } from 'botbuilder-dialogs';
|
||||||
import {
|
import {
|
||||||
GBDialogStep,
|
GBDialogStep,
|
||||||
|
@ -57,6 +54,8 @@ import {
|
||||||
IGBPackage
|
IGBPackage
|
||||||
} from 'botlib';
|
} from 'botlib';
|
||||||
|
|
||||||
|
import { MicrosoftAppCredentials } from 'botframework-connector';
|
||||||
|
import { GBServer } from '../../../src/app';
|
||||||
import { GBAnalyticsPackage } from '../../analytics.gblib';
|
import { GBAnalyticsPackage } from '../../analytics.gblib';
|
||||||
import { GBCorePackage } from '../../core.gbapp';
|
import { GBCorePackage } from '../../core.gbapp';
|
||||||
import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp';
|
import { GBCustomerSatisfactionPackage } from '../../customer-satisfaction.gbapp';
|
||||||
|
@ -66,8 +65,8 @@ import { GBSecurityPackage } from '../../security.gblib';
|
||||||
import { GBWhatsappPackage } from '../../whatsapp.gblib';
|
import { GBWhatsappPackage } from '../../whatsapp.gblib';
|
||||||
import { Messages } from '../strings';
|
import { Messages } from '../strings';
|
||||||
import { GBAdminPackage } from './../../admin.gbapp/index';
|
import { GBAdminPackage } from './../../admin.gbapp/index';
|
||||||
import { GBDeployer } from './GBDeployer';
|
|
||||||
import { GBConfigService } from './GBConfigService';
|
import { GBConfigService } from './GBConfigService';
|
||||||
|
import { GBDeployer } from './GBDeployer';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Minimal service layer for a bot.
|
* Minimal service layer for a bot.
|
||||||
|
@ -319,12 +318,16 @@ export class GBMinService {
|
||||||
appId: instance.marketplaceId,
|
appId: instance.marketplaceId,
|
||||||
appPassword: instance.marketplacePassword
|
appPassword: instance.marketplacePassword
|
||||||
});
|
});
|
||||||
|
|
||||||
const storage = new MemoryStorage();
|
const storage = new MemoryStorage();
|
||||||
|
|
||||||
const conversationState = new ConversationState(storage);
|
const conversationState = new ConversationState(storage);
|
||||||
const userState = new UserState(storage);
|
const userState = new UserState(storage);
|
||||||
adapter.use(new AutoSaveStateMiddleware(conversationState, userState));
|
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.
|
// The minimal bot is built here.
|
||||||
|
|
||||||
const min = new GBMinInstance();
|
const min = new GBMinInstance();
|
||||||
|
@ -338,6 +341,7 @@ export class GBMinService {
|
||||||
min.cbMap = {};
|
min.cbMap = {};
|
||||||
min.scriptMap = {};
|
min.scriptMap = {};
|
||||||
min.sandBoxMap = {};
|
min.sandBoxMap = {};
|
||||||
|
min.packages = GBServer.globals.sysPackages[0]; // HACK: Whatsapp now.
|
||||||
min.userProfile = conversationState.createProperty('userProfile');
|
min.userProfile = conversationState.createProperty('userProfile');
|
||||||
const dialogState = conversationState.createProperty('dialogState');
|
const dialogState = conversationState.createProperty('dialogState');
|
||||||
|
|
||||||
|
@ -401,6 +405,7 @@ export class GBMinService {
|
||||||
step.context.activity.locale = 'pt-BR';
|
step.context.activity.locale = 'pt-BR';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
const user = await min.userProfile.get(context, {});
|
const user = await min.userProfile.get(context, {});
|
||||||
|
|
||||||
if (!user.loaded) {
|
if (!user.loaded) {
|
||||||
|
@ -423,14 +428,11 @@ export class GBMinService {
|
||||||
);
|
);
|
||||||
if (context.activity.type === 'conversationUpdate' && context.activity.membersAdded.length > 0) {
|
if (context.activity.type === 'conversationUpdate' && context.activity.membersAdded.length > 0) {
|
||||||
const member = context.activity.membersAdded[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...`);
|
GBLog.info(`Bot added to conversation, starting chat...`);
|
||||||
appPackages.forEach(e => {
|
appPackages.forEach(e => {
|
||||||
e.onNewSession(min, step);
|
e.onNewSession(min, step);
|
||||||
});
|
});
|
||||||
// Processes the root dialog.
|
|
||||||
|
|
||||||
await step.beginDialog('/');
|
|
||||||
} else {
|
} else {
|
||||||
GBLog.info(`Member added to conversation: ${member.name}`);
|
GBLog.info(`Member added to conversation: ${member.name}`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,8 +57,7 @@ export class GBWhatsappPackage implements IGBPackage {
|
||||||
min.instance.whatsappBotKey,
|
min.instance.whatsappBotKey,
|
||||||
min.instance.whatsappServiceKey,
|
min.instance.whatsappServiceKey,
|
||||||
min.instance.whatsappServiceNumber,
|
min.instance.whatsappServiceNumber,
|
||||||
min.instance.whatsappServiceUrl,
|
min.instance.whatsappServiceUrl
|
||||||
min.instance.botId
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,7 +67,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
const result = request.post(options);
|
const result = request.post(options);
|
||||||
GBLog.info(result);
|
GBLog.info(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error}`);
|
GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -92,7 +92,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
.then(response => {
|
.then(response => {
|
||||||
return response.obj.conversationId;
|
return response.obj.conversationId;
|
||||||
}).catch(err => {
|
}).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 => {
|
.then(generatedConversationId => {
|
||||||
|
@ -102,14 +102,14 @@ export class WhatsappDirectLine extends GBService {
|
||||||
this.pollMessages(client, generatedConversationId, from, fromName);
|
this.pollMessages(client, generatedConversationId, from, fromName);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
GBLog.error(`Error starting conversation ${err}`);
|
GBLog.error(`Error starting conversation ${err.data}`);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
this.inputMessage(client, conversationId, text, from, fromName);
|
this.inputMessage(client, conversationId, text, from, fromName);
|
||||||
}
|
}
|
||||||
res.end();
|
res.end();
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
GBLog.error(`Error initializing DirectLine for Whatsapp channel ${err}`);
|
GBLog.error(`Error initializing DirectLine for Whatsapp channel ${err.data}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
replyToId: from
|
replyToId: from
|
||||||
}
|
}
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
GBLog.error(`GBWhatsapp: Error receiving message: ${err}.`);
|
GBLog.error(`GBWhatsapp: Error receiving message: ${err.data}.`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -147,13 +147,13 @@ export class WhatsappDirectLine extends GBService {
|
||||||
return response.obj.activities;
|
return response.obj.activities;
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.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 => {
|
.then(activities => {
|
||||||
this.printMessages(activities, conversationId, from, fromName);
|
this.printMessages(activities, conversationId, from, fromName);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
GBLog.error(`Error calling printMessages on Whatsapp channel ${err}`);
|
GBLog.error(`Error calling printMessages on Whatsapp channel ${err.data}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
}, this.pollInterval);
|
}, this.pollInterval);
|
||||||
|
@ -225,7 +225,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
const result = request.post(options);
|
const result = request.post(options);
|
||||||
GBLog.info(result);
|
GBLog.info(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
GBLog.error(`Error sending message to Whatsapp provider ${error}`);
|
GBLog.error(`Error sending message to Whatsapp provider ${error.message}`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,6 +57,7 @@ const appPackages: IGBPackage[] = [];
|
||||||
export class RootData {
|
export class RootData {
|
||||||
public publicAddress: string;
|
public publicAddress: string;
|
||||||
public server: any;
|
public server: any;
|
||||||
|
sysPackages: any;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -122,7 +123,7 @@ export class GBServer {
|
||||||
// Deploys system and user packages.
|
// Deploys system and user packages.
|
||||||
|
|
||||||
GBLog.info(`Deploying packages...`);
|
GBLog.info(`Deploying packages...`);
|
||||||
core.loadSysPackages(core);
|
GBServer.globals.sysPackages = core.loadSysPackages(core);
|
||||||
await core.checkStorage(azureDeployer);
|
await core.checkStorage(azureDeployer);
|
||||||
await deployer.deployPackages(core, server, appPackages);
|
await deployer.deployPackages(core, server, appPackages);
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue