Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deployment V1 #11

Merged
merged 4 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions src/message/controllers/inbound.message.controller.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import { Controller, Get, Post, Body, Logger } from '@nestjs/common';
import { GSWhatsAppMessage, convertXMessageToMsg, convertMessageToXMsg, gupshupWhatsappAdapterServiceConfig} from '@samagra-x/gupshup-whatsapp-adapter';
import { GSWhatsAppMessage } from '@samagra-x/gupshup-whatsapp-adapter';
import { ConfigService } from '@nestjs/config';
import { InboundService } from '../services/inbound/inbound.service';

@Controller('/inbound/gupshup/whatsapp')
export class MessageController {
constructor(
private configService: ConfigService,
private readonly inboundService:InboundService
) {}
private readonly inboundService: InboundService
) {}
private readonly logger = new Logger(MessageController.name);

@Get("/health")
@Get('/health')
async verifyEndpointIsActive(): Promise<string> {
return "Endpoint Active!"
return 'Endpoint Active!';
}

@Post()
async handleIncomingMessageData(@Body() requestData: GSWhatsAppMessage): Promise<any> {

await this.inboundService.handleIncomingGsWhatsappMessage(requestData)
try {
this.logger.log("Message Received and Sent!");
} catch (error) {
this.logger.error(`Error sending message: ${error}`);
throw error;
}
}
}
await this.inboundService.handleIncomingGsWhatsappMessage(requestData);
}
}
Empty file.
2 changes: 1 addition & 1 deletion src/message/controllers/outbound/outbound.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class OutboundMessageController {

@Post()
async handleIncomingXMessage(@Body() orchestratorRequest: XMessage): Promise<any> {
await this.outboundService.handleOrchestratorRequest(orchestratorRequest)
await this.outboundService.handleOrchestratorResponse(orchestratorRequest)
}

}
89 changes: 72 additions & 17 deletions src/message/services/inbound/inbound.service.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,87 @@
import { Injectable, Logger } from '@nestjs/common';
import { GSWhatsAppMessage, convertMessageToXMsg, convertXMessageToMsg, gupshupWhatsappAdapterServiceConfig } from '@samagra-x/gupshup-whatsapp-adapter';
import {
GSWhatsAppMessage,
convertMessageToXMsg,
convertXMessageToMsg,
gupshupWhatsappAdapterServiceConfig
} from '@samagra-x/gupshup-whatsapp-adapter';
import { ConfigService } from '@nestjs/config';
import axios from 'axios';
import { url } from 'inspector';
import { v4 as uuid4 } from 'uuid';

import { OutboundService } from '../outbound/outbound.service';
import { XMessage, MessageType, MessageState } from '@samagra-x/xmessage';

@Injectable()
export class InboundService {
constructor(private configService: ConfigService){}
private readonly logger = new Logger(InboundService.name)
constructor(
private configService: ConfigService,
private readonly outboundService: OutboundService
) {}
private readonly logger = new Logger(InboundService.name);

convertApiResponseToXMessage(data: any, phoneNumber): XMessage {
return {
adapterId: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
messageType: data.messageType,
messageId: data.messageId,
to: { userID: phoneNumber },
from: { userID: 'admin' },
channelURI: data.channelURI,
providerURI: data.providerURI,
timestamp: data.timestamp,
messageState: MessageState.REPLIED,
payload: data.payload
};
}

async handleIncomingGsWhatsappMessage(whatsappMessage: GSWhatsAppMessage) {
gupshupWhatsappAdapterServiceConfig.setConfig({
baseUrl: this.configService.get<string>('BASE_URL'),
adminToken: this.configService.get<string>('ADAPTER_ADMIN_TOKEN'),
vaultServiceToken: this.configService.get<string>('VAULT_SERVICE_TOKEN'),
vaultServiceUrl: this.configService.get<string>('VAULT_SERVICE_URL'),
gupshupUrl: this.configService.get<string>('GUPSHUP_API_ENDPOINT')
})

const xMessagePayload = await convertMessageToXMsg(whatsappMessage)
this.logger.log(xMessagePayload)
const orchestratorServiceUrl = this.configService.get<string>('ORCHESTRATOR_API_ENDPOINT')
const resp = await axios.post(orchestratorServiceUrl, xMessagePayload, {
headers: {
'Content-Type': 'application/json',
});
try {
if ("interactive" in whatsappMessage) {
const interactiveInteraction = JSON.parse(whatsappMessage.interactive)
if (interactiveInteraction.type = 'button_reply') {
//handle feedback
this.logger.log("Feedback is not being handled right now!")
return
}
})
//log to supabase
//handle errors
}
}
const xMessagePayload = await convertMessageToXMsg(whatsappMessage);
xMessagePayload.from.userID = uuid4();
xMessagePayload.to.userID = uuid4();
xMessagePayload.messageId.Id = uuid4();

const orchestratorServiceUrl = this.configService.get<string>('ORCHESTRATOR_API_ENDPOINT');
const resp = await axios.post(orchestratorServiceUrl, xMessagePayload, {
headers: {
'Content-Type': 'application/json'
}
});

}
const xResponse = this.convertApiResponseToXMessage(resp.data, whatsappMessage.mobile.substring(2));
const sentResp = await this.outboundService.handleOrchestratorResponse(xResponse);
} catch (error) {
const errorResponse = this.convertApiResponseToXMessage(
{
adapterId: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
messageType: MessageType.TEXT,
messageId: {},
from: { userID: 'admin' },
channelURI: 'WhatsApp',
providerURI: 'gupshup',
timestamp: Date.now(),
messageState: MessageState.REPLIED,
payload: { text: 'Something went wrong. Please try again later.' }
},
whatsappMessage.mobile.substring(2)
);
await this.outboundService.handleOrchestratorResponse(errorResponse);
}
}
}
25 changes: 20 additions & 5 deletions src/message/services/outbound/outbound.service.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,36 @@
import { Injectable } from '@nestjs/common';
import { convertXMessageToMsg, gupshupWhatsappAdapterServiceConfig } from '@samagra-x/gupshup-whatsapp-adapter';
import { ConfigService } from '@nestjs/config';
import { XMessage } from '@samagra-x/xmessage';
import { StylingTag, XMessage } from '@samagra-x/xmessage';

@Injectable()
export class OutboundService {
constructor(private configService: ConfigService){}
constructor(private configService: ConfigService) {}

async handleOrchestratorRequest(orchestratorRequest: XMessage) {
async handleOrchestratorResponse(orchestratorRequest: XMessage) {
gupshupWhatsappAdapterServiceConfig.setConfig({
adapter: '7b0cf232-38a2-4f9b-8070-9b988ff94c2c',
baseUrl: this.configService.get<string>('BASE_URL'),
adminToken: this.configService.get<string>('ADAPTER_ADMIN_TOKEN'),
vaultServiceToken: this.configService.get<string>('VAULT_SERVICE_TOKEN'),
vaultServiceUrl: this.configService.get<string>('VAULT_SERVICE_URL'),
gupshupUrl: this.configService.get<string>('GUPSHUP_API_ENDPOINT')
})
});

await convertXMessageToMsg(orchestratorRequest)
orchestratorRequest.payload.buttonChoices = [
{
backmenu: true,
key: 'positive',
text: '👍'
},
{
backmenu: true,
key: 'negative',
text: '👎'
}
];
orchestratorRequest.payload.stylingTag = StylingTag.QUICKREPLYBTN;

await convertXMessageToMsg(orchestratorRequest);
}
}
Loading