Skip to content

Commit 6647ef6

Browse files
authored
Merge pull request #16 from samagra-comms/main
Deploying latest changes to dev
2 parents cf6f199 + 286dad1 commit 6647ef6

File tree

9 files changed

+933
-994
lines changed

9 files changed

+933
-994
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ lerna-debug.log*
3535
!.vscode/settings.json
3636
!.vscode/tasks.json
3737
!.vscode/launch.json
38-
!.vscode/extensions.json
38+
!.vscode/extensions.json
39+
.prettierrc

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@nestjs/config": "^3.1.1",
2525
"@nestjs/core": "^10.0.0",
2626
"@nestjs/platform-express": "^10.0.0",
27-
"@samagra-x/gupshup-whatsapp-adapter": "^1.0.0",
27+
"@samagra-x/gupshup-whatsapp-adapter": "1.0.2",
2828
"@samagra-x/xmessage": "^1.0.1",
2929
"@supabase/supabase-js": "^2.38.5",
3030
"axios": "^1.6.2",

src/message/controllers/inbound.message.controller.ts renamed to src/message/controllers/inbound/gupshup.whatsapp.controller.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Controller, Get, Post, Body, Logger } from '@nestjs/common';
22
import { GSWhatsAppMessage } from '@samagra-x/gupshup-whatsapp-adapter';
33
import { ConfigService } from '@nestjs/config';
4-
import { InboundService } from '../services/inbound/inbound.service';
4+
import { GupshupWhatsappInboundService } from '../../services/inbound/gupshup.whatsapp.service';
55

66
@Controller('/inbound/gupshup/whatsapp')
7-
export class MessageController {
7+
export class GupshupWhatsappInboundController {
88
constructor(
99
private configService: ConfigService,
10-
private readonly inboundService: InboundService
10+
private readonly inboundService: GupshupWhatsappInboundService
1111
) {}
12-
private readonly logger = new Logger(MessageController.name);
12+
private readonly logger = new Logger(GupshupWhatsappInboundController.name);
1313

1414
@Get('/health')
1515
async verifyEndpointIsActive(): Promise<string> {
@@ -19,6 +19,7 @@ export class MessageController {
1919
@Post()
2020
async handleIncomingMessageData(@Body() requestData: GSWhatsAppMessage): Promise<any> {
2121
if ("mobile" in requestData){
22+
this.logger.log(requestData)
2223
await this.inboundService.handleIncomingGsWhatsappMessage(requestData);
2324
}
2425
}

src/message/controllers/outbound/outbound.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export class OutboundMessageController {
88

99
@Post()
1010
async handleIncomingXMessage(@Body() orchestratorRequest: XMessage): Promise<any> {
11-
await this.outboundService.handleOrchestratorResponse(orchestratorRequest)
11+
// await this.outboundService.handleOrchestratorResponse(orchestratorRequest)
1212
}
1313

1414
}

src/message/message.module.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { Module } from '@nestjs/common';
2-
import { MessageController } from './controllers/inbound.message.controller';
3-
import { InboundService } from './services/inbound/inbound.service';
2+
import { GupshupWhatsappInboundController } from './controllers/inbound/gupshup.whatsapp.controller';
3+
import { GupshupWhatsappInboundService } from './services/inbound/gupshup.whatsapp.service';
44
import { ConfigModule } from '@nestjs/config';
55
import { OutboundService } from './services/outbound/outbound.service';
6+
import { CredentialService } from './services/credentials/credentials.service';
67

78
@Module({
89
imports: [ConfigModule.forRoot()],
9-
controllers: [MessageController],
10+
controllers: [GupshupWhatsappInboundController],
1011
providers: [
11-
InboundService,
12-
OutboundService
12+
GupshupWhatsappInboundService,
13+
OutboundService,
14+
CredentialService
1315
],
1416
})
1517
export class MessageModule {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Injectable, Logger } from '@nestjs/common';
2+
import axios from 'axios';
3+
4+
5+
@Injectable()
6+
export class CredentialService {
7+
private readonly logger = new Logger(CredentialService.name);
8+
async getCredentialsFromVault(
9+
baseUrl: string,
10+
secretPath: string,
11+
headers: Record<string, string>
12+
) {
13+
const webClient = axios.create({
14+
baseURL: baseUrl,
15+
headers: headers
16+
});
17+
18+
const response = await webClient.get(`${secretPath}`);
19+
return response.data
20+
}
21+
}

src/message/services/inbound/inbound.service.ts renamed to src/message/services/inbound/gupshup.whatsapp.service.ts

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import { v4 as uuid4 } from 'uuid';
1111

1212
import { OutboundService } from '../outbound/outbound.service';
1313
import { XMessage, MessageType, MessageState } from '@samagra-x/xmessage';
14+
import { CredentialService } from '../credentials/credentials.service';
1415

1516
@Injectable()
16-
export class InboundService {
17+
export class GupshupWhatsappInboundService {
1718
constructor(
1819
private configService: ConfigService,
19-
private readonly outboundService: OutboundService
20+
private readonly outboundService: OutboundService,
21+
private readonly credentialService: CredentialService
2022
) {}
21-
private readonly logger = new Logger(InboundService.name);
23+
private readonly logger = new Logger(GupshupWhatsappInboundService.name);
2224

2325
convertApiResponseToXMessage(data: any, phoneNumber): XMessage {
2426
return {
@@ -35,23 +37,52 @@ export class InboundService {
3537
};
3638
}
3739

40+
async getAdapterCredentials(number: string) {
41+
if (number.endsWith('88')) {
42+
const vaultResponse = await this.credentialService.getCredentialsFromVault(
43+
this.configService.get<string>('VAULT_SERVICE_URL'),
44+
'/admin/secret/gupshupWhatsappVariable',
45+
{
46+
ownerId: '8f7ee860-0163-4229-9d2a-01cef53145ba',
47+
ownerOrgId: 'org1',
48+
'admin-token': this.configService.get<string>('VAULT_SERVICE_TOKEN')
49+
}
50+
);
51+
52+
const credentials = vaultResponse['result']['gupshupWhatsappVariable'];
53+
return credentials;
54+
} else if (number.endsWith('87')) {
55+
const vaultResponse = await this.credentialService.getCredentialsFromVault(
56+
this.configService.get<string>('VAULT_SERVICE_URL'),
57+
'/admin/secret/gupshupWhatsappVariable2',
58+
{
59+
ownerId: '8f7ee860-0163-4229-9d2a-01cef53145ba',
60+
ownerOrgId: 'org1',
61+
'admin-token': this.configService.get<string>('VAULT_SERVICE_TOKEN')
62+
}
63+
);
64+
65+
const credentials = vaultResponse['result']['gupshupWhatsappVariable2'];
66+
return credentials;
67+
} else {
68+
//throw error
69+
return;
70+
}
71+
}
72+
3873
async handleIncomingGsWhatsappMessage(whatsappMessage: GSWhatsAppMessage) {
39-
gupshupWhatsappAdapterServiceConfig.setConfig({
40-
baseUrl: this.configService.get<string>('BASE_URL'),
41-
adminToken: this.configService.get<string>('ADAPTER_ADMIN_TOKEN'),
42-
vaultServiceToken: this.configService.get<string>('VAULT_SERVICE_TOKEN'),
43-
vaultServiceUrl: this.configService.get<string>('VAULT_SERVICE_URL'),
44-
gupshupUrl: this.configService.get<string>('GUPSHUP_API_ENDPOINT')
45-
});
74+
const adapterCredentials = await this.getAdapterCredentials(whatsappMessage.waNumber);
4675
try {
47-
if ("interactive" in whatsappMessage) {
48-
const interactiveInteraction = JSON.parse(whatsappMessage.interactive)
49-
if (interactiveInteraction.type = 'button_reply') {
76+
//Handle Feedback First
77+
if ('interactive' in whatsappMessage) {
78+
const interactiveInteraction = JSON.parse(whatsappMessage.interactive);
79+
if ((interactiveInteraction.type = 'button_reply')) {
5080
//handle feedback
51-
this.logger.log("Feedback is not being handled right now!")
52-
return
81+
this.logger.log('Feedback is not being handled right now!');
82+
return;
5383
}
5484
}
85+
5586
const xMessagePayload = await convertMessageToXMsg(whatsappMessage);
5687
xMessagePayload.from.userID = uuid4();
5788
xMessagePayload.to.userID = uuid4();
@@ -65,7 +96,9 @@ export class InboundService {
6596
});
6697

6798
const xResponse = this.convertApiResponseToXMessage(resp.data, whatsappMessage.mobile.substring(2));
68-
const sentResp = await this.outboundService.handleOrchestratorResponse(xResponse);
99+
this.logger.log("OrchestratorResponse", xResponse)
100+
const sentResp = await this.outboundService.handleOrchestratorResponse(xResponse, adapterCredentials);
101+
this.logger.log("OutboundResponse",sentResp)
69102
} catch (error) {
70103
const errorResponse = this.convertApiResponseToXMessage(
71104
{
@@ -81,7 +114,8 @@ export class InboundService {
81114
},
82115
whatsappMessage.mobile.substring(2)
83116
);
84-
await this.outboundService.handleOrchestratorResponse(errorResponse);
117+
const sentResp = await this.outboundService.handleOrchestratorResponse(errorResponse, adapterCredentials);
118+
this.logger.log("OutboundErrorResponse",sentResp)
85119
}
86120
}
87121
}
Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,22 @@
11
import { Injectable, Logger } from '@nestjs/common';
22
import { convertXMessageToMsg, gupshupWhatsappAdapterServiceConfig } from '@samagra-x/gupshup-whatsapp-adapter';
33
import { ConfigService } from '@nestjs/config';
4-
import { StylingTag, XMessage } from '@samagra-x/xmessage';
4+
import { MessageState, XMessage } from '@samagra-x/xmessage';
5+
6+
import { CredentialService } from '../credentials/credentials.service';
57

68
@Injectable()
79
export class OutboundService {
810
constructor(private configService: ConfigService) {}
911
private readonly logger = new Logger(OutboundService.name);
1012

11-
async handleOrchestratorResponse(orchestratorRequest: XMessage) {
13+
async handleOrchestratorResponse(orchestratorRequest: XMessage, credentials) {
1214
gupshupWhatsappAdapterServiceConfig.setConfig({
13-
adapter: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
14-
baseUrl: this.configService.get<string>('BASE_URL'),
15-
adminToken: this.configService.get<string>('ADAPTER_ADMIN_TOKEN'),
16-
vaultServiceToken: this.configService.get<string>('VAULT_SERVICE_TOKEN'),
17-
vaultServiceUrl: this.configService.get<string>('VAULT_SERVICE_URL'),
18-
gupshupUrl: this.configService.get<string>('GUPSHUP_API_ENDPOINT')
15+
adapterCredentials: credentials
1916
});
20-
21-
orchestratorRequest.payload.buttonChoices = [
22-
{
23-
backmenu: true,
24-
key: 'positive',
25-
text: '👍'
26-
},
27-
{
28-
backmenu: true,
29-
key: 'negative',
30-
text: '👎'
31-
}
32-
];
33-
orchestratorRequest.payload.stylingTag = StylingTag.QUICKREPLYBTN;
34-
35-
if ("text" in orchestratorRequest.payload) {
36-
orchestratorRequest.payload.text = orchestratorRequest.payload.text.substring(0,1023)
37-
console.log(orchestratorRequest.payload.text.length)
38-
}
39-
4017
const adapterResponse = await convertXMessageToMsg(orchestratorRequest);
41-
//throw if Not Sent
18+
if (adapterResponse.messageState = MessageState.NOT_SENT) {
19+
throw new Error("Message Not Sent")
20+
}
4221
}
4322
}

0 commit comments

Comments
 (0)