diff --git a/packages/default.gbdialog/simple-order.vbs b/packages/default.gbdialog/simple-order.vbs new file mode 100644 index 00000000..961f8719 --- /dev/null +++ b/packages/default.gbdialog/simple-order.vbs @@ -0,0 +1,4 @@ +talk "Qual seu pedido?" +hear pedido +talk "Obrigado, seu pedido serĂ¡ processado e retornamos." +ask payment \ No newline at end of file diff --git a/packages/whatsapp.gblib/index.ts b/packages/whatsapp.gblib/index.ts index fed2ccb8..35c0534e 100644 --- a/packages/whatsapp.gblib/index.ts +++ b/packages/whatsapp.gblib/index.ts @@ -49,7 +49,7 @@ export class GBWhatsappPackage implements IGBPackage { public loadBot(min: GBMinInstance): void { // Only loads engine if it is defined on services.json. - if (min.instance.whatsappServiceKey !== null ) { + if (min.instance.whatsappServiceKey !== null) { min.whatsAppDirectLine = new WhatsappDirectLine( min.botId, min.instance.whatsappBotKey, @@ -57,6 +57,9 @@ export class GBWhatsappPackage implements IGBPackage { min.instance.whatsappServiceNumber, min.instance.whatsappServiceUrl ); + (async () => { + await min.whatsAppDirectLine.setup(); + }); } } diff --git a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts index 5673f440..e7a974c9 100644 --- a/packages/whatsapp.gblib/services/WhatsappDirectLine.ts +++ b/packages/whatsapp.gblib/services/WhatsappDirectLine.ts @@ -36,6 +36,7 @@ const Swagger = require('swagger-client'); const rp = require('request-promise'); import { GBLog, GBService } from 'botlib'; import * as request from 'request-promise-native'; +const fs = require('fs'); import { GBServer } from '../../../src/app'; /** @@ -50,6 +51,7 @@ export class WhatsappDirectLine extends GBService { public whatsappServiceNumber: string; public whatsappServiceUrl: string; public botId: string; + private directLineSecret: string; public conversationIds = {}; @@ -63,44 +65,46 @@ export class WhatsappDirectLine extends GBService { super(); this.botId = botId; + this.directLineSecret = directLineSecret; this.whatsappServiceKey = whatsappServiceKey; this.whatsappServiceNumber = whatsappServiceNumber; this.whatsappServiceUrl = whatsappServiceUrl; - const fs = require('fs'); - let directLineClient = + } + + public async setup() { + this.directLineClient = new Swagger({ spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), usePromise: true }); - this.directLineClient = directLineClient; - directLineClient - .then(async client => { + let client = await this.directLineClient; - client.clientAuthorizations.add( - 'AuthorizationBotConnector', - new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${directLineSecret}`, 'header') - ); + client.clientAuthorizations.add( + 'AuthorizationBotConnector', + new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header') + ); + + const options = { + method: 'POST', + url: urlJoin(this.whatsappServiceUrl, 'webhook'), + qs: { + token: this.whatsappServiceKey, + webhookUrl: `${GBServer.globals.publicAddress}/webhooks/whatsapp`, + set: true + }, + headers: { + 'cache-control': 'no-cache' + } + }; + + try { + request.post(options); + } catch (error) { + GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`); + } - const options = { - method: 'POST', - url: urlJoin(this.whatsappServiceUrl, 'webhook'), - qs: { - token: this.whatsappServiceKey, - webhookUrl: `${GBServer.globals.publicAddress}/webhooks/whatsapp`, - set: true - }, - headers: { - 'cache-control': 'no-cache' - } - }; - try { - request.post(options); - } catch (error) { - GBLog.error(`Error initializing 3rd party Whatsapp provider(1) ${error.message}`); - } - }); } public static async asyncForEach(array, callback) {