Skip to content

Commit

Permalink
feat(whatsapp.gblib): add 'graphapi' option.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan committed Feb 9, 2023
1 parent 41ceedf commit ce84053
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
9 changes: 8 additions & 1 deletion packages/basic.gblib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ export class GBBasicPackage implements IGBPackage {

public async loadPackage (core: IGBCoreService, sequelize: Sequelize): Promise<void> {
core.sequelize.addModels([GuaribasSchedule]);
app.use(koaBody.koaBody({ multipart: true }));
app.use(koaBody.koaBody({ multipart: true, }));

app.listen(1111);
}

Expand Down Expand Up @@ -97,5 +98,11 @@ export class GBBasicPackage implements IGBPackage {
app.use(waRouter.routes());
app.use(dbgRouter.routes());
app.use(imgRouter.routes());
app.use(async (ctx, next) => {
if(ctx['status'] === 404){
ctx.status = 404
ctx.body = {msg:'emmmmmmm, seems 404'};
}
});
}
}
41 changes: 32 additions & 9 deletions packages/whatsapp.gblib/services/WhatsappDirectLine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import qrcode from 'qrcode-terminal';
import express from 'express';
import { DialogKeywords } from '../../basic.gblib/services/DialogKeywords.js';
import { GBAdminService } from '../../admin.gbapp/services/GBAdminService.js';
import { method } from 'lodash';

/**
* Support for Whatsapp.
Expand Down Expand Up @@ -96,11 +97,13 @@ export class WhatsappDirectLine extends GBService {
this.whatsappServiceKey = whatsappServiceKey;
this.whatsappServiceNumber = whatsappServiceNumber;
this.whatsappServiceUrl = whatsappServiceUrl;
this.provider =
this.provider =
whatsappServiceKey === 'internal'
? 'GeneralBots'
: whatsappServiceNumber.indexOf(';') > -1
? 'maytapi'
: whatsappServiceKey !== 'internal'
? 'graphapi'
: 'chatapi';
this.groupId = groupId;
}
Expand All @@ -125,6 +128,7 @@ export class WhatsappDirectLine extends GBService {
new Swagger.ApiKeyAuthorization('Authorization', `Bearer ${this.directLineSecret}`, 'header')
);
let options: any;
const phoneId = this.whatsappServiceNumber.split(';')[0];

switch (this.provider) {
case 'GeneralBots':
Expand Down Expand Up @@ -203,7 +207,7 @@ export class WhatsappDirectLine extends GBService {
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)
);

Expand Down Expand Up @@ -249,7 +253,6 @@ export class WhatsappDirectLine extends GBService {
setUrl = false;

break;

case 'chatapi':
url = urlJoin(this.whatsappServiceUrl, 'webhook');
options = {
Expand All @@ -268,7 +271,6 @@ export class WhatsappDirectLine extends GBService {

break;
case 'maytapi':
let phoneId = this.whatsappServiceNumber.split(';')[0];
let productId = this.whatsappServiceNumber.split(';')[1];
url = `${this.INSTANCE_URL}/${productId}/${phoneId}/config`;
body = {
Expand All @@ -288,7 +290,7 @@ export class WhatsappDirectLine extends GBService {
json: true
};
break;
}
}

if (setUrl && options && this.whatsappServiceUrl) {
GBServer.globals.server.use(`/audios`, express.static('work'));
Expand Down Expand Up @@ -327,7 +329,7 @@ export class WhatsappDirectLine extends GBService {
}

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) {
Expand Down Expand Up @@ -379,6 +381,8 @@ export class WhatsappDirectLine extends GBService {
}

break;
case 'graphapi':
break;

case 'maytapi':
message = req.body.message;
Expand Down Expand Up @@ -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);

if (user.agentSystemId.charAt(2) === ':') {
Expand All @@ -643,7 +647,7 @@ export class WhatsappDirectLine extends GBService {
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({
conversationId: conversationId,
activity: {
Expand Down Expand Up @@ -794,7 +798,24 @@ export class WhatsappDirectLine extends GBService {
};

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) {
try {
// tslint:disable-next-line: await-promise
Expand Down Expand Up @@ -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);

await this.sendFileToDevice(to, url, 'Audio', msg, chatId);
Expand Down Expand Up @@ -906,6 +927,8 @@ export class WhatsappDirectLine extends GBService {
}
};
break;
case 'graphapi':

}

if (options) {
Expand Down
10 changes: 8 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class GBServer {
server.use(bodyParser.json());
server.use(bodyParser.urlencoded({ extended: true }));

process.on('unhandledRejection', (err, p) => {
process.on('unhandledRejection', (err, p) => {
GBLog.error(`UNHANDLED_REJECTION(promises): ${p} ${err.toString()}`);
});

Expand Down Expand Up @@ -126,7 +126,7 @@ export class GBServer {
const azureDeployer: AzureDeployerService = await AzureDeployerService.createInstance(deployer);
const adminService: GBAdminService = new GBAdminService(core);

if (process.env.NODE_ENV === 'development' ) {
if (process.env.NODE_ENV === 'development') {
const proxy = GBConfigService.get('BOT_URL');
if (proxy !== undefined) {
GBServer.globals.publicAddress = proxy;
Expand Down Expand Up @@ -228,6 +228,12 @@ export class GBServer {
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...`);

// Opens Navigator.
Expand Down
7 changes: 2 additions & 5 deletions swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
"status": "Production"
}
},
"host": "generalbots.ai",
"host": "",
"basePath": "/",
"schemes": [
"https"
],
"consumes": [],
"produces": [],
"paths": {
"/api/talkTo": {
"/api/v2//dialog/talk": {
"post": {
"summary": "Talk to the user.",
"description": "Talk to the user.",
Expand Down Expand Up @@ -58,9 +58,6 @@
"properties": {
"pid": {
"type": "string"
},
"mobile": {
"type": "string"
},
"text": {
"type": "string"
Expand Down

0 comments on commit ce84053

Please sign in to comment.