fix(whatsapp.gblib): await fix.

This commit is contained in:
Rodrigo Rodriguez 2020-04-08 21:33:27 -03:00
parent 0645c310c3
commit 5015fb15fb
3 changed files with 39 additions and 28 deletions

View file

@ -0,0 +1,4 @@
talk "Qual seu pedido?"
hear pedido
talk "Obrigado, seu pedido será processado e retornamos."
ask payment

View file

@ -49,7 +49,7 @@ export class GBWhatsappPackage implements IGBPackage {
public loadBot(min: GBMinInstance): void { public loadBot(min: GBMinInstance): void {
// Only loads engine if it is defined on services.json. // 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.whatsAppDirectLine = new WhatsappDirectLine(
min.botId, min.botId,
min.instance.whatsappBotKey, min.instance.whatsappBotKey,
@ -57,6 +57,9 @@ export class GBWhatsappPackage implements IGBPackage {
min.instance.whatsappServiceNumber, min.instance.whatsappServiceNumber,
min.instance.whatsappServiceUrl min.instance.whatsappServiceUrl
); );
(async () => {
await min.whatsAppDirectLine.setup();
});
} }
} }

View file

@ -36,6 +36,7 @@ const Swagger = require('swagger-client');
const rp = require('request-promise'); const rp = require('request-promise');
import { GBLog, GBService } from 'botlib'; import { GBLog, GBService } from 'botlib';
import * as request from 'request-promise-native'; import * as request from 'request-promise-native';
const fs = require('fs');
import { GBServer } from '../../../src/app'; import { GBServer } from '../../../src/app';
/** /**
@ -50,6 +51,7 @@ export class WhatsappDirectLine extends GBService {
public whatsappServiceNumber: string; public whatsappServiceNumber: string;
public whatsappServiceUrl: string; public whatsappServiceUrl: string;
public botId: string; public botId: string;
private directLineSecret: string;
public conversationIds = {}; public conversationIds = {};
@ -63,44 +65,46 @@ export class WhatsappDirectLine extends GBService {
super(); super();
this.botId = botId; this.botId = botId;
this.directLineSecret = directLineSecret;
this.whatsappServiceKey = whatsappServiceKey; this.whatsappServiceKey = whatsappServiceKey;
this.whatsappServiceNumber = whatsappServiceNumber; this.whatsappServiceNumber = whatsappServiceNumber;
this.whatsappServiceUrl = whatsappServiceUrl; this.whatsappServiceUrl = whatsappServiceUrl;
const fs = require('fs');
let directLineClient = }
public async setup() {
this.directLineClient =
new Swagger({ new Swagger({
spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')), spec: JSON.parse(fs.readFileSync('directline-3.0.json', 'utf8')),
usePromise: true usePromise: true
}); });
this.directLineClient = directLineClient; let client = await this.directLineClient;
directLineClient
.then(async client => {
client.clientAuthorizations.add( client.clientAuthorizations.add(
'AuthorizationBotConnector', 'AuthorizationBotConnector',
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${directLineSecret}`, 'header') 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) { public static async asyncForEach(array, callback) {