Skip to content

Commit d90e856

Browse files
authored
Merge pull request #11 from samagra-comms/main
Deployment V1
2 parents 86fbb99 + fd09f04 commit d90e856

File tree

5 files changed

+101
-38
lines changed

5 files changed

+101
-38
lines changed
Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,23 @@
11
import { Controller, Get, Post, Body, Logger } from '@nestjs/common';
2-
import { GSWhatsAppMessage, convertXMessageToMsg, convertMessageToXMsg, gupshupWhatsappAdapterServiceConfig} from '@samagra-x/gupshup-whatsapp-adapter';
2+
import { GSWhatsAppMessage } from '@samagra-x/gupshup-whatsapp-adapter';
33
import { ConfigService } from '@nestjs/config';
44
import { InboundService } from '../services/inbound/inbound.service';
55

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

14-
@Get("/health")
14+
@Get('/health')
1515
async verifyEndpointIsActive(): Promise<string> {
16-
return "Endpoint Active!"
16+
return 'Endpoint Active!';
1717
}
1818

1919
@Post()
2020
async handleIncomingMessageData(@Body() requestData: GSWhatsAppMessage): Promise<any> {
21-
22-
await this.inboundService.handleIncomingGsWhatsappMessage(requestData)
23-
try {
24-
this.logger.log("Message Received and Sent!");
25-
} catch (error) {
26-
this.logger.error(`Error sending message: ${error}`);
27-
throw error;
28-
}
29-
}
30-
}
21+
await this.inboundService.handleIncomingGsWhatsappMessage(requestData);
22+
}
23+
}

src/message/controllers/orchestrator.message.controller.ts

Whitespace-only changes.

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.handleOrchestratorRequest(orchestratorRequest)
11+
await this.outboundService.handleOrchestratorResponse(orchestratorRequest)
1212
}
1313

1414
}
Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,87 @@
11
import { Injectable, Logger } from '@nestjs/common';
2-
import { GSWhatsAppMessage, convertMessageToXMsg, convertXMessageToMsg, gupshupWhatsappAdapterServiceConfig } from '@samagra-x/gupshup-whatsapp-adapter';
2+
import {
3+
GSWhatsAppMessage,
4+
convertMessageToXMsg,
5+
convertXMessageToMsg,
6+
gupshupWhatsappAdapterServiceConfig
7+
} from '@samagra-x/gupshup-whatsapp-adapter';
38
import { ConfigService } from '@nestjs/config';
49
import axios from 'axios';
5-
import { url } from 'inspector';
10+
import { v4 as uuid4 } from 'uuid';
11+
12+
import { OutboundService } from '../outbound/outbound.service';
13+
import { XMessage, MessageType, MessageState } from '@samagra-x/xmessage';
614

715
@Injectable()
816
export class InboundService {
9-
constructor(private configService: ConfigService){}
10-
private readonly logger = new Logger(InboundService.name)
17+
constructor(
18+
private configService: ConfigService,
19+
private readonly outboundService: OutboundService
20+
) {}
21+
private readonly logger = new Logger(InboundService.name);
22+
23+
convertApiResponseToXMessage(data: any, phoneNumber): XMessage {
24+
return {
25+
adapterId: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
26+
messageType: data.messageType,
27+
messageId: data.messageId,
28+
to: { userID: phoneNumber },
29+
from: { userID: 'admin' },
30+
channelURI: data.channelURI,
31+
providerURI: data.providerURI,
32+
timestamp: data.timestamp,
33+
messageState: MessageState.REPLIED,
34+
payload: data.payload
35+
};
36+
}
37+
1138
async handleIncomingGsWhatsappMessage(whatsappMessage: GSWhatsAppMessage) {
1239
gupshupWhatsappAdapterServiceConfig.setConfig({
1340
baseUrl: this.configService.get<string>('BASE_URL'),
1441
adminToken: this.configService.get<string>('ADAPTER_ADMIN_TOKEN'),
1542
vaultServiceToken: this.configService.get<string>('VAULT_SERVICE_TOKEN'),
1643
vaultServiceUrl: this.configService.get<string>('VAULT_SERVICE_URL'),
1744
gupshupUrl: this.configService.get<string>('GUPSHUP_API_ENDPOINT')
18-
})
19-
20-
const xMessagePayload = await convertMessageToXMsg(whatsappMessage)
21-
this.logger.log(xMessagePayload)
22-
const orchestratorServiceUrl = this.configService.get<string>('ORCHESTRATOR_API_ENDPOINT')
23-
const resp = await axios.post(orchestratorServiceUrl, xMessagePayload, {
24-
headers: {
25-
'Content-Type': 'application/json',
45+
});
46+
try {
47+
if ("interactive" in whatsappMessage) {
48+
const interactiveInteraction = JSON.parse(whatsappMessage.interactive)
49+
if (interactiveInteraction.type = 'button_reply') {
50+
//handle feedback
51+
this.logger.log("Feedback is not being handled right now!")
52+
return
2653
}
27-
})
28-
//log to supabase
29-
//handle errors
30-
}
54+
}
55+
const xMessagePayload = await convertMessageToXMsg(whatsappMessage);
56+
xMessagePayload.from.userID = uuid4();
57+
xMessagePayload.to.userID = uuid4();
58+
xMessagePayload.messageId.Id = uuid4();
59+
60+
const orchestratorServiceUrl = this.configService.get<string>('ORCHESTRATOR_API_ENDPOINT');
61+
const resp = await axios.post(orchestratorServiceUrl, xMessagePayload, {
62+
headers: {
63+
'Content-Type': 'application/json'
64+
}
65+
});
3166

32-
}
67+
const xResponse = this.convertApiResponseToXMessage(resp.data, whatsappMessage.mobile.substring(2));
68+
const sentResp = await this.outboundService.handleOrchestratorResponse(xResponse);
69+
} catch (error) {
70+
const errorResponse = this.convertApiResponseToXMessage(
71+
{
72+
adapterId: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
73+
messageType: MessageType.TEXT,
74+
messageId: {},
75+
from: { userID: 'admin' },
76+
channelURI: 'WhatsApp',
77+
providerURI: 'gupshup',
78+
timestamp: Date.now(),
79+
messageState: MessageState.REPLIED,
80+
payload: { text: 'Something went wrong. Please try again later.' }
81+
},
82+
whatsappMessage.mobile.substring(2)
83+
);
84+
await this.outboundService.handleOrchestratorResponse(errorResponse);
85+
}
86+
}
87+
}
Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,36 @@
11
import { Injectable } from '@nestjs/common';
22
import { convertXMessageToMsg, gupshupWhatsappAdapterServiceConfig } from '@samagra-x/gupshup-whatsapp-adapter';
33
import { ConfigService } from '@nestjs/config';
4-
import { XMessage } from '@samagra-x/xmessage';
4+
import { StylingTag, XMessage } from '@samagra-x/xmessage';
55

66
@Injectable()
77
export class OutboundService {
8-
constructor(private configService: ConfigService){}
8+
constructor(private configService: ConfigService) {}
99

10-
async handleOrchestratorRequest(orchestratorRequest: XMessage) {
10+
async handleOrchestratorResponse(orchestratorRequest: XMessage) {
1111
gupshupWhatsappAdapterServiceConfig.setConfig({
12+
adapter: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
1213
baseUrl: this.configService.get<string>('BASE_URL'),
1314
adminToken: this.configService.get<string>('ADAPTER_ADMIN_TOKEN'),
1415
vaultServiceToken: this.configService.get<string>('VAULT_SERVICE_TOKEN'),
1516
vaultServiceUrl: this.configService.get<string>('VAULT_SERVICE_URL'),
1617
gupshupUrl: this.configService.get<string>('GUPSHUP_API_ENDPOINT')
17-
})
18+
});
1819

19-
await convertXMessageToMsg(orchestratorRequest)
20+
orchestratorRequest.payload.buttonChoices = [
21+
{
22+
backmenu: true,
23+
key: 'positive',
24+
text: '👍'
25+
},
26+
{
27+
backmenu: true,
28+
key: 'negative',
29+
text: '👎'
30+
}
31+
];
32+
orchestratorRequest.payload.stylingTag = StylingTag.QUICKREPLYBTN;
33+
34+
await convertXMessageToMsg(orchestratorRequest);
2035
}
2136
}

0 commit comments

Comments
 (0)