feat(whatsapp.gblib): add 'graphapi' option.
This commit is contained in:
parent
41ceedfc00
commit
ce84053d95
4 changed files with 50 additions and 17 deletions
|
@ -60,7 +60,8 @@ export class GBBasicPackage implements IGBPackage {
|
||||||
|
|
||||||
public async loadPackage (core: IGBCoreService, sequelize: Sequelize): Promise<void> {
|
public async loadPackage (core: IGBCoreService, sequelize: Sequelize): Promise<void> {
|
||||||
core.sequelize.addModels([GuaribasSchedule]);
|
core.sequelize.addModels([GuaribasSchedule]);
|
||||||
app.use(koaBody.koaBody({ multipart: true }));
|
app.use(koaBody.koaBody({ multipart: true, }));
|
||||||
|
|
||||||
app.listen(1111);
|
app.listen(1111);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -97,5 +98,11 @@ export class GBBasicPackage implements IGBPackage {
|
||||||
app.use(waRouter.routes());
|
app.use(waRouter.routes());
|
||||||
app.use(dbgRouter.routes());
|
app.use(dbgRouter.routes());
|
||||||
app.use(imgRouter.routes());
|
app.use(imgRouter.routes());
|
||||||
|
app.use(async (ctx, next) => {
|
||||||
|
if(ctx['status'] === 404){
|
||||||
|
ctx.status = 404
|
||||||
|
ctx.body = {msg:'emmmmmmm, seems 404'};
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,7 @@ import qrcode from 'qrcode-terminal';
|
||||||
import express from 'express';
|
import express from 'express';
|
||||||
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
|
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
|
||||||
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
|
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
|
||||||
|
import { method } from 'lodash';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Support for Whatsapp.
|
* Support for Whatsapp.
|
||||||
|
@ -101,6 +102,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
? 'GeneralBots'
|
? 'GeneralBots'
|
||||||
: whatsappServiceNumber.indexOf(';') > -1
|
: whatsappServiceNumber.indexOf(';') > -1
|
||||||
? 'maytapi'
|
? 'maytapi'
|
||||||
|
: whatsappServiceKey !== 'internal'
|
||||||
|
? 'graphapi'
|
||||||
: 'chatapi';
|
: 'chatapi';
|
||||||
this.groupId = groupId;
|
this.groupId = groupId;
|
||||||
}
|
}
|
||||||
|
@ -125,6 +128,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
|
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
|
||||||
);
|
);
|
||||||
let options: any;
|
let options: any;
|
||||||
|
const phoneId = this.whatsappServiceNumber.split(';')[0];
|
||||||
|
|
||||||
switch (this.provider) {
|
switch (this.provider) {
|
||||||
case 'GeneralBots':
|
case 'GeneralBots':
|
||||||
|
@ -203,7 +207,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
msg
|
msg
|
||||||
);
|
);
|
||||||
|
|
||||||
s.sendEmail({pid: 0, to: adminEmail, subject: `Check your WhatsApp for bot ${this.botId}`, body: msg });
|
s.sendEmail({ pid: 0, to: adminEmail, subject: `Check your WhatsApp for bot ${this.botId}`, body: msg });
|
||||||
}).bind(this)
|
}).bind(this)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -249,7 +253,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
setUrl = false;
|
setUrl = false;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'chatapi':
|
case 'chatapi':
|
||||||
url = urlJoin(this.whatsappServiceUrl, 'webhook');
|
url = urlJoin(this.whatsappServiceUrl, 'webhook');
|
||||||
options = {
|
options = {
|
||||||
|
@ -268,7 +271,6 @@ export class WhatsappDirectLine extends GBService {
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 'maytapi':
|
case 'maytapi':
|
||||||
let phoneId = this.whatsappServiceNumber.split(';')[0];
|
|
||||||
let productId = this.whatsappServiceNumber.split(';')[1];
|
let productId = this.whatsappServiceNumber.split(';')[1];
|
||||||
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
|
||||||
body = {
|
body = {
|
||||||
|
@ -288,7 +290,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
json: true
|
json: true
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setUrl && options && this.whatsappServiceUrl) {
|
if (setUrl && options && this.whatsappServiceUrl) {
|
||||||
GBServer.globals.server.use(`/audios`, express.static('work'));
|
GBServer.globals.server.use(`/audios`, express.static('work'));
|
||||||
|
@ -327,7 +329,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static providerFromRequest(req: any) {
|
public static providerFromRequest(req: any) {
|
||||||
return req.body.messages ? 'chatapi' : req.body.message ? 'maytapi' : 'GeneralBots';
|
return req.body.messages ? 'chatapi' : req.body.message ? 'maytapi' : req.body.message ? 'graphapi' : 'GeneralBots';
|
||||||
}
|
}
|
||||||
|
|
||||||
public async received(req, res) {
|
public async received(req, res) {
|
||||||
|
@ -379,6 +381,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
case 'graphapi':
|
||||||
|
break;
|
||||||
|
|
||||||
case 'maytapi':
|
case 'maytapi':
|
||||||
message = req.body.message;
|
message = req.body.message;
|
||||||
|
@ -621,7 +625,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private async endTransfer(id: any, locale: string, user: GuaribasUser, agent: GuaribasUser, sec: SecService) {
|
private async endTransfer(id: string, locale: string, user: GuaribasUser, agent: GuaribasUser, sec: SecService) {
|
||||||
await this.sendToDeviceEx(id, Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
|
await this.sendToDeviceEx(id, Messages[this.locale].notify_end_transfer(this.min.instance.botId), locale, null);
|
||||||
|
|
||||||
if (user.agentSystemId.charAt(2) === ':') {
|
if (user.agentSystemId.charAt(2) === ':') {
|
||||||
|
@ -643,7 +647,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
await sec.updateHumanAgent(id, this.min.instance.instanceId, null);
|
await sec.updateHumanAgent(id, this.min.instance.instanceId, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public inputMessage(client, conversationId, text, from, fromName, group, attachments) {
|
public inputMessage(client, conversationId: string, text: string, from, fromName: string, group, attachments: File) {
|
||||||
return client.Conversations.Conversations_PostActivity({
|
return client.Conversations.Conversations_PostActivity({
|
||||||
conversationId: conversationId,
|
conversationId: conversationId,
|
||||||
activity: {
|
activity: {
|
||||||
|
@ -794,7 +798,24 @@ export class WhatsappDirectLine extends GBService {
|
||||||
};
|
};
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'graphapi':
|
||||||
|
url = `https://graph.facebook.com/v15.0/${phoneId}/messages`
|
||||||
|
options = {
|
||||||
|
method:'POST',
|
||||||
|
timeout: 10000,
|
||||||
|
headers: {
|
||||||
|
token: `Bearer `,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
body:{
|
||||||
|
messaging_product: 'whatsapp',
|
||||||
|
recipient_type: 'individual',
|
||||||
|
to: phoneId,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
try {
|
try {
|
||||||
// tslint:disable-next-line: await-promise
|
// tslint:disable-next-line: await-promise
|
||||||
|
@ -845,7 +866,7 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async sendTextAsAudioToDevice(to, msg, chatId) {
|
public async sendTextAsAudioToDevice(to, msg: string, chatId) {
|
||||||
const url = await GBConversationalService.getAudioBufferFromText(msg);
|
const url = await GBConversationalService.getAudioBufferFromText(msg);
|
||||||
|
|
||||||
await this.sendFileToDevice(to, url, 'Audio', msg, chatId);
|
await this.sendFileToDevice(to, url, 'Audio', msg, chatId);
|
||||||
|
@ -906,6 +927,8 @@ export class WhatsappDirectLine extends GBService {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
break;
|
break;
|
||||||
|
case 'graphapi':
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options) {
|
if (options) {
|
||||||
|
|
10
src/app.ts
10
src/app.ts
|
@ -96,7 +96,7 @@ export class GBServer {
|
||||||
server.use(bodyParser.json());
|
server.use(bodyParser.json());
|
||||||
server.use(bodyParser.urlencoded({ extended: true }));
|
server.use(bodyParser.urlencoded({ extended: true }));
|
||||||
|
|
||||||
process.on('unhandledRejection', (err, p) => {
|
process.on('unhandledRejection', (err, p) => {
|
||||||
GBLog.error(`UNHANDLED_REJECTION(promises): ${p} ${err.toString()}`);
|
GBLog.error(`UNHANDLED_REJECTION(promises): ${p} ${err.toString()}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -126,7 +126,7 @@ export class GBServer {
|
||||||
const azureDeployer: AzureDeployerService = await AzureDeployerService.createInstance(deployer);
|
const azureDeployer: AzureDeployerService = await AzureDeployerService.createInstance(deployer);
|
||||||
const adminService: GBAdminService = new GBAdminService(core);
|
const adminService: GBAdminService = new GBAdminService(core);
|
||||||
|
|
||||||
if (process.env.NODE_ENV === 'development' ) {
|
if (process.env.NODE_ENV === 'development') {
|
||||||
const proxy = GBConfigService.get('BOT_URL');
|
const proxy = GBConfigService.get('BOT_URL');
|
||||||
if (proxy !== undefined) {
|
if (proxy !== undefined) {
|
||||||
GBServer.globals.publicAddress = proxy;
|
GBServer.globals.publicAddress = proxy;
|
||||||
|
@ -228,6 +228,12 @@ export class GBServer {
|
||||||
winston.default(server, loggers[1]);
|
winston.default(server, loggers[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
server.get('*', function(req, res){
|
||||||
|
GBLog.info(`HTTP 404: ${req.url}.`);
|
||||||
|
res.status(404);
|
||||||
|
res.end();
|
||||||
|
});
|
||||||
|
|
||||||
GBLog.info(`The Bot Server is in RUNNING mode...`);
|
GBLog.info(`The Bot Server is in RUNNING mode...`);
|
||||||
|
|
||||||
// Opens Navigator.
|
// Opens Navigator.
|
||||||
|
|
|
@ -13,7 +13,7 @@
|
||||||
"status": "Production"
|
"status": "Production"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"host": "generalbots.ai",
|
"host": "",
|
||||||
"basePath": "/",
|
"basePath": "/",
|
||||||
"schemes": [
|
"schemes": [
|
||||||
"https"
|
"https"
|
||||||
|
@ -21,7 +21,7 @@
|
||||||
"consumes": [],
|
"consumes": [],
|
||||||
"produces": [],
|
"produces": [],
|
||||||
"paths": {
|
"paths": {
|
||||||
"/api/talkTo": {
|
"/api/v2//dialog/talk": {
|
||||||
"post": {
|
"post": {
|
||||||
"summary": "Talk to the user.",
|
"summary": "Talk to the user.",
|
||||||
"description": "Talk to the user.",
|
"description": "Talk to the user.",
|
||||||
|
@ -58,9 +58,6 @@
|
||||||
"properties": {
|
"properties": {
|
||||||
"pid": {
|
"pid": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
},
|
|
||||||
"mobile": {
|
|
||||||
"type": "string"
|
|
||||||
},
|
},
|
||||||
"text": {
|
"text": {
|
||||||
"type": "string"
|
"type": "string"
|
||||||
|
|
Loading…
Add table
Reference in a new issue