From 0c08797845183ef7002c439722e117925fc2d3e4 Mon Sep 17 00:00:00 2001 From: KDwevedi Date: Tue, 5 Dec 2023 09:45:58 +0530 Subject: [PATCH 1/2] dockerfile --- Dockerfile | 2 -- 1 file changed, 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 9924491..6673a5c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,8 +28,6 @@ COPY --from=builder /app/node_modules ./node_modules COPY --from=builder /app/dist ./dist COPY --from=builder /app/package.json ./ COPY --from=builder /app/yarn.lock ./ -COPY --from=builder /app/src ./src -COPY --from=builder /app/test ./test ARG ENV ENV CI_ENV=${ENV} From 9b10fe73604de965718189d9a2a98b782924addf Mon Sep 17 00:00:00 2001 From: KDwevedi Date: Tue, 5 Dec 2023 13:27:01 +0530 Subject: [PATCH 2/2] for deplyment --- .../controllers/inbound.message.controller.ts | 23 ++--- .../orchestrator.message.controller.ts | 0 .../outbound/outbound.controller.ts | 2 +- .../services/inbound/inbound.service.ts | 89 +++++++++++++++---- .../services/outbound/outbound.service.ts | 25 ++++-- 5 files changed, 101 insertions(+), 38 deletions(-) delete mode 100644 src/message/controllers/orchestrator.message.controller.ts diff --git a/src/message/controllers/inbound.message.controller.ts b/src/message/controllers/inbound.message.controller.ts index 0b38d39..a15d059 100644 --- a/src/message/controllers/inbound.message.controller.ts +++ b/src/message/controllers/inbound.message.controller.ts @@ -1,5 +1,5 @@ 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'; @@ -7,24 +7,17 @@ import { InboundService } from '../services/inbound/inbound.service'; 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 { - return "Endpoint Active!" + return 'Endpoint Active!'; } @Post() async handleIncomingMessageData(@Body() requestData: GSWhatsAppMessage): Promise { - - await this.inboundService.handleIncomingGsWhatsappMessage(requestData) - try { - this.logger.log("Message Received and Sent!"); - } catch (error) { - this.logger.error(`Error sending message: ${error}`); - throw error; - } - } -} \ No newline at end of file + await this.inboundService.handleIncomingGsWhatsappMessage(requestData); + } +} diff --git a/src/message/controllers/orchestrator.message.controller.ts b/src/message/controllers/orchestrator.message.controller.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/message/controllers/outbound/outbound.controller.ts b/src/message/controllers/outbound/outbound.controller.ts index a61390c..77414eb 100644 --- a/src/message/controllers/outbound/outbound.controller.ts +++ b/src/message/controllers/outbound/outbound.controller.ts @@ -8,7 +8,7 @@ export class OutboundMessageController { @Post() async handleIncomingXMessage(@Body() orchestratorRequest: XMessage): Promise { - await this.outboundService.handleOrchestratorRequest(orchestratorRequest) + await this.outboundService.handleOrchestratorResponse(orchestratorRequest) } } diff --git a/src/message/services/inbound/inbound.service.ts b/src/message/services/inbound/inbound.service.ts index 944978b..8579b7b 100644 --- a/src/message/services/inbound/inbound.service.ts +++ b/src/message/services/inbound/inbound.service.ts @@ -1,13 +1,40 @@ 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('BASE_URL'), @@ -15,18 +42,46 @@ export class InboundService { vaultServiceToken: this.configService.get('VAULT_SERVICE_TOKEN'), vaultServiceUrl: this.configService.get('VAULT_SERVICE_URL'), gupshupUrl: this.configService.get('GUPSHUP_API_ENDPOINT') - }) - - const xMessagePayload = await convertMessageToXMsg(whatsappMessage) - this.logger.log(xMessagePayload) - const orchestratorServiceUrl = this.configService.get('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('ORCHESTRATOR_API_ENDPOINT'); + const resp = await axios.post(orchestratorServiceUrl, xMessagePayload, { + headers: { + 'Content-Type': 'application/json' + } + }); -} \ No newline at end of file + 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); + } + } +} diff --git a/src/message/services/outbound/outbound.service.ts b/src/message/services/outbound/outbound.service.ts index 55be035..9d5b7e7 100644 --- a/src/message/services/outbound/outbound.service.ts +++ b/src/message/services/outbound/outbound.service.ts @@ -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('BASE_URL'), adminToken: this.configService.get('ADAPTER_ADMIN_TOKEN'), vaultServiceToken: this.configService.get('VAULT_SERVICE_TOKEN'), vaultServiceUrl: this.configService.get('VAULT_SERVICE_URL'), gupshupUrl: this.configService.get('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); } }