From a98861af7bb022aea0f44937b57da88f69b1792f Mon Sep 17 00:00:00 2001 From: hvs-flick Date: Tue, 6 Feb 2024 01:00:04 +0530 Subject: [PATCH 1/3] Publish message to group --- .github/workflows/whatsapp-deploy.yml | 1 + .../platform/proxy/WhatsAppProxy.java | 5 +- .../resources/application-beta.properties | 4 +- .../main/resources/application-dev.properties | 4 +- .../resources/application-prod.properties | 2 + whatsapp-js/main.js | 8 ++- whatsapp-js/package-lock.json | 2 +- whatsapp-js/package.json | 2 +- whatsapp-js/src/notificationRequestParser.js | 51 +++++++++++++++++++ .../test/notificationRequestParserTest.js | 20 ++++++++ 10 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 whatsapp-js/src/notificationRequestParser.js create mode 100644 whatsapp-js/test/notificationRequestParserTest.js diff --git a/.github/workflows/whatsapp-deploy.yml b/.github/workflows/whatsapp-deploy.yml index 0d6dd568..1217d923 100644 --- a/.github/workflows/whatsapp-deploy.yml +++ b/.github/workflows/whatsapp-deploy.yml @@ -27,6 +27,7 @@ jobs: port: 22 script: | cd ~/flickmatch/home/whatsapp-js + git stash git pull origin main git status ~/.nvm/versions/node/v18.15.0/bin/pm2 restart main diff --git a/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java b/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java index 035c7ca0..4d7244ee 100644 --- a/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java +++ b/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java @@ -5,6 +5,7 @@ import com.flickmatch.platform.records.WhatsAppNotification; import lombok.extern.log4j.Log4j2; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; @@ -18,10 +19,12 @@ public class WhatsAppProxy { // Create a RestTemplate @Autowired private RestTemplate restTemplate; + @Value("${whatsapp.endpoint}") + private String nodeServerUrl; private final ObjectMapper objectMapper = new ObjectMapper(); public void sendNotification(WhatsAppNotification eventDataForNotification) { - String nodeServerUrl = "http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/"; + //String nodeServerUrl = "http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/"; // Create headers HttpHeaders headers = new HttpHeaders(); diff --git a/platform/src/main/resources/application-beta.properties b/platform/src/main/resources/application-beta.properties index f137bbcf..fd488c08 100644 --- a/platform/src/main/resources/application-beta.properties +++ b/platform/src/main/resources/application-beta.properties @@ -7,4 +7,6 @@ amazon.aws.profile=beta phonepe.merchant.id=M1FPXNBFIGDG phonepe.saltkey=e7a5693b-0ccf-47ac-a258-c5d77e701e2c -phonepe.saltkey.index=2 \ No newline at end of file +phonepe.saltkey.index=2 + +whatsapp.endpoint=http://localhost:3000/notification \ No newline at end of file diff --git a/platform/src/main/resources/application-dev.properties b/platform/src/main/resources/application-dev.properties index 8158da68..89214412 100644 --- a/platform/src/main/resources/application-dev.properties +++ b/platform/src/main/resources/application-dev.properties @@ -6,4 +6,6 @@ amazon.aws.profile=dev phonepe.merchant.id=test phonepe.saltkey=test -phonepe.saltkey.index=1 \ No newline at end of file +phonepe.saltkey.index=1 + +whatsapp.endpoint=http://localhost:3000/notification \ No newline at end of file diff --git a/platform/src/main/resources/application-prod.properties b/platform/src/main/resources/application-prod.properties index a662cb7d..240d64c4 100644 --- a/platform/src/main/resources/application-prod.properties +++ b/platform/src/main/resources/application-prod.properties @@ -8,3 +8,5 @@ amazon.aws.profile=prod phonepe.merchant.id=M1FPXNBFIGDG phonepe.saltkey=e7a5693b-0ccf-47ac-a258-c5d77e701e2c phonepe.saltkey.index=2 + +whatsapp.endpoint=http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/notification \ No newline at end of file diff --git a/whatsapp-js/main.js b/whatsapp-js/main.js index 87b8584c..17b626eb 100644 --- a/whatsapp-js/main.js +++ b/whatsapp-js/main.js @@ -4,6 +4,7 @@ import qrcode from "qrcode-terminal"; import pkg from "whatsapp-web.js"; const { Client, LocalAuth } = pkg; import { processGroup } from "./src/inputParser.js"; +import { createMessage } from "./src/notificationRequestParser.js"; export const isProd = process.platform !== "win32" && process.platform !== "darwin"; @@ -12,13 +13,16 @@ export const groupName = isProd ? "Online Queue Update" : "Test group "; import express from "express"; const app = express(); +app.use(express.json()); const port = 3000; const ggnSouthCityChatId = "120363030960923086@g.us"; const hydChatId = "120363104642020865@g.us"; -app.post("/", (req, res) => { +app.post("/notification", (req, res) => { const testGroupChatId = "120363088832118953@g.us"; - client.sendMessage(testGroupChatId, "Post Endpoint hit"); + const jsonData = req.body; + //console.log('Received JSON data:', JSON.stringify(jsonData)); + client.sendMessage(testGroupChatId, createMessage(jsonData)); res.send("Hello World!"); }); diff --git a/whatsapp-js/package-lock.json b/whatsapp-js/package-lock.json index 2e64bd6c..d1e91652 100644 --- a/whatsapp-js/package-lock.json +++ b/whatsapp-js/package-lock.json @@ -12,7 +12,7 @@ "express": "^4.18.2", "isomorphic-fetch": "^3.0.0", "qrcode-terminal": "^0.12.0", - "whatsapp-web.js": "^1.19.5" + "whatsapp-web.js": "^1.23.0" } }, "node_modules/@pedroslopez/moduleraid": { diff --git a/whatsapp-js/package.json b/whatsapp-js/package.json index 5637fc5b..16dd527b 100644 --- a/whatsapp-js/package.json +++ b/whatsapp-js/package.json @@ -14,6 +14,6 @@ "express": "^4.18.2", "isomorphic-fetch": "^3.0.0", "qrcode-terminal": "^0.12.0", - "whatsapp-web.js": "^1.19.5" + "whatsapp-web.js": "^1.23.0" } } diff --git a/whatsapp-js/src/notificationRequestParser.js b/whatsapp-js/src/notificationRequestParser.js new file mode 100644 index 00000000..44761569 --- /dev/null +++ b/whatsapp-js/src/notificationRequestParser.js @@ -0,0 +1,51 @@ +const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; + +// Function to convert timestamps to timeslot +function convertToTimeSlot(timestamp1, timestamp2) { + // Convert timestamps to Date objects + const date1 = new Date(timestamp1); + const date2 = new Date(timestamp2); + + // Extract hours and minutes from the Date objects + const hours1 = date1.getHours(); + const minutes1 = date1.getMinutes(); + const date = date1.getDate(); + const month = date1.getMonth(); + const year = date1.getFullYear(); + const fullDate = `${date}-${month}-${year}` + const day = date1.getDay(); + + const hours2 = date2.getHours(); + const minutes2 = date2.getMinutes(); + + // Format the timeslots + const timeSlot1 = `${padZero(hours1)}:${padZero(minutes1)}`; + const timeSlot2 = `${padZero(hours2)}:${padZero(minutes2)}`; + + return `${timeSlot1} - ${timeSlot2} ${fullDate} (${daysOfWeek[day]})`; + } + + // Function to pad zero for single-digit hours/minutes + function padZero(value) { + return value < 10 ? `0${value}` : `${value}`; + } + +export const createMessage = (jsonData) => { + //const jsonDataObject = JSON.parse(jsonData); + const venueName = jsonData.venueName; + const mapLink = jsonData.venueLocationLink; + const format = jsonData.reservedPlayersCount/2; + const charges = jsonData.charges; + const dateTime = convertToTimeSlot(jsonData.startTime, jsonData.endTime); + + var message = `${venueName} (${mapLink}) +${dateTime} +${format}v${format} +${charges}/- Per Person. + +Confirmed Players:`; + for (const [index, value] of jsonData.playerNameList.entries()) { + message = message + '\n' + `${index+1}. ${value}`; + } + return message; +} \ No newline at end of file diff --git a/whatsapp-js/test/notificationRequestParserTest.js b/whatsapp-js/test/notificationRequestParserTest.js new file mode 100644 index 00000000..753849f7 --- /dev/null +++ b/whatsapp-js/test/notificationRequestParserTest.js @@ -0,0 +1,20 @@ +import { createMessage } from "../src/notificationRequestParser.js"; + +const jsonData = '{"startTime":1699715640000,"endTime":1699719240000,"venueName":"HotFut GachiBowli","reservedPlayersCount":10,"charges":200,"venueLocationLink":"https://goo.gl/maps/1QzakRkJPHxT8GjRA","localTimeZone":"GMT+5:30","playerNameList":["Player1","Player2","sam","nooo"]}'; +const expected = `HotFut GachiBowli (https://goo.gl/maps/1QzakRkJPHxT8GjRA) +20:44 - 21:44 11-10-2023 (Saturday) +5v5 +200/- Per Person. + +Confirmed Players: +1. Player1 +2. Player2 +3. sam +4. nooo`; + +function assertEqual(actual, expected, label) { + const areEqual = JSON.stringify(actual) === JSON.stringify(expected); + console.assert(areEqual, `Assertion failed for ${label}:`, actual); + }; + +assertEqual(createMessage(JSON.parse(jsonData)), expected, 'createMessage test'); \ No newline at end of file From df723010c73ac033f5dce3fda0e75b02cb06875c Mon Sep 17 00:00:00 2001 From: hvs-flick Date: Tue, 6 Feb 2024 01:02:09 +0530 Subject: [PATCH 2/3] Remove get endpoint --- whatsapp-js/main.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/whatsapp-js/main.js b/whatsapp-js/main.js index 17b626eb..bea3daf1 100644 --- a/whatsapp-js/main.js +++ b/whatsapp-js/main.js @@ -26,12 +26,6 @@ app.post("/notification", (req, res) => { res.send("Hello World!"); }); -app.get("/", (req, res) => { - const testGroupChatId = "120363088832118953@g.us"; - client.sendMessage(testGroupChatId, "Get Endpoint hit"); - res.send("Hello World!"); -}); - app.listen(port, () => { console.log(`Example app listening on port ${port}`); }); From ed48bdd7d4f6cabb749f94a3d28269429d596ed3 Mon Sep 17 00:00:00 2001 From: hvs-flick Date: Tue, 6 Feb 2024 23:08:52 +0530 Subject: [PATCH 3/3] Fix tests and update message --- .../java/com/flickmatch/platform/proxy/WhatsAppProxy.java | 1 - .../com/flickmatch/platform/proxy/WhatsAppProxyTest.java | 7 +++++++ platform/src/test/resources/application-test.properties | 2 ++ whatsapp-js/src/notificationRequestParser.js | 3 ++- whatsapp-js/test/notificationRequestParserTest.js | 6 ++++-- 5 files changed, 15 insertions(+), 4 deletions(-) diff --git a/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java b/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java index 4d7244ee..80b8b72e 100644 --- a/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java +++ b/platform/src/main/java/com/flickmatch/platform/proxy/WhatsAppProxy.java @@ -24,7 +24,6 @@ public class WhatsAppProxy { private final ObjectMapper objectMapper = new ObjectMapper(); public void sendNotification(WhatsAppNotification eventDataForNotification) { - //String nodeServerUrl = "http://ec2-18-223-205-234.us-east-2.compute.amazonaws.com:3000/"; // Create headers HttpHeaders headers = new HttpHeaders(); diff --git a/platform/src/test/java/com/flickmatch/platform/proxy/WhatsAppProxyTest.java b/platform/src/test/java/com/flickmatch/platform/proxy/WhatsAppProxyTest.java index 9564b320..ef77d44a 100644 --- a/platform/src/test/java/com/flickmatch/platform/proxy/WhatsAppProxyTest.java +++ b/platform/src/test/java/com/flickmatch/platform/proxy/WhatsAppProxyTest.java @@ -1,6 +1,7 @@ package com.flickmatch.platform.proxy; import com.flickmatch.platform.records.WhatsAppNotification; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -8,6 +9,7 @@ import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.ResponseEntity; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.web.client.RestTemplate; import static org.mockito.ArgumentMatchers.any; @@ -21,6 +23,11 @@ class WhatsAppProxyTest { @InjectMocks WhatsAppProxy whatsAppProxy; + @BeforeEach + void setUp() { + ReflectionTestUtils.setField(whatsAppProxy, "nodeServerUrl", ""); + } + @Test public void test_WhenCallIsSuccessful() { Mockito.when(restTemplate.postForEntity(anyString(), any(), any())).thenReturn(ResponseEntity.accepted().build()); diff --git a/platform/src/test/resources/application-test.properties b/platform/src/test/resources/application-test.properties index 9529f667..cb798c51 100644 --- a/platform/src/test/resources/application-test.properties +++ b/platform/src/test/resources/application-test.properties @@ -9,3 +9,5 @@ application.local.endpoint=http://localhost:8080/graphql phonepe.merchant.id=test phonepe.saltkey=test phonepe.saltkey.index=1 + +whatsapp.endpoint=http://localhost:3000/notification \ No newline at end of file diff --git a/whatsapp-js/src/notificationRequestParser.js b/whatsapp-js/src/notificationRequestParser.js index 44761569..4a6fdb0a 100644 --- a/whatsapp-js/src/notificationRequestParser.js +++ b/whatsapp-js/src/notificationRequestParser.js @@ -41,11 +41,12 @@ export const createMessage = (jsonData) => { var message = `${venueName} (${mapLink}) ${dateTime} ${format}v${format} -${charges}/- Per Person. +₹${charges}/- Per Person. Confirmed Players:`; for (const [index, value] of jsonData.playerNameList.entries()) { message = message + '\n' + `${index+1}. ${value}`; } + message = message + '\n\nPlease pay at play.flickmatch.in to confirm your spot.'; return message; } \ No newline at end of file diff --git a/whatsapp-js/test/notificationRequestParserTest.js b/whatsapp-js/test/notificationRequestParserTest.js index 753849f7..6585f4c8 100644 --- a/whatsapp-js/test/notificationRequestParserTest.js +++ b/whatsapp-js/test/notificationRequestParserTest.js @@ -4,13 +4,15 @@ const jsonData = '{"startTime":1699715640000,"endTime":1699719240000,"venueName" const expected = `HotFut GachiBowli (https://goo.gl/maps/1QzakRkJPHxT8GjRA) 20:44 - 21:44 11-10-2023 (Saturday) 5v5 -200/- Per Person. +₹200/- Per Person. Confirmed Players: 1. Player1 2. Player2 3. sam -4. nooo`; +4. nooo + +Please pay at play.flickmatch.in to confirm your spot.`; function assertEqual(actual, expected, label) { const areEqual = JSON.stringify(actual) === JSON.stringify(expected);