From b8c7f0bcc41994b8027f8631559086d66f4e0b1d Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Tue, 30 Dec 2025 05:15:12 +0530 Subject: [PATCH 1/7] Update homepage message Signed-off-by: 000balaai-cloud <000balaai@gmail.com> --- views/pages/index.ejs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/views/pages/index.ejs b/views/pages/index.ejs index ea7498435d..95a9bfc746 100644 --- a/views/pages/index.ejs +++ b/views/pages/index.ejs @@ -13,7 +13,7 @@ -

Getting Started on Heroku with Node.js

+

Hello from Balaraju 🚀

This is a sample Node application deployed to Heroku. It's a reasonably simple app - but a good foundation for understanding how to get the most out of the Heroku platform.

Getting Started on Heroku with Node.js Getting Started on Heroku Fir with Node.js From bde4276c6541c7711f06854958b2e6e70c6cdfc8 Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Tue, 30 Dec 2025 05:17:17 +0530 Subject: [PATCH 2/7] Update homepage message Signed-off-by: 000balaai-cloud <000balaai@gmail.com> From dbc6080263ac78b65384deea857f716a525a2904 Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Tue, 30 Dec 2025 15:57:35 +0530 Subject: [PATCH 3/7] Update index.js Signed-off-by: 000balaai-cloud <000balaai@gmail.com> --- index.js | 67 +++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 44 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 19d020f11f..1cf0ab1fc4 100644 --- a/index.js +++ b/index.js @@ -1,36 +1,57 @@ -const express = require('express') -const path = require('path') +const express = require('express'); +const path = require('path'); -const port = process.env.PORT || 5006 +const port = process.env.PORT || 5006; +const app = express(); -const app = express() +// Middleware +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); +app.use(express.static(path.join(__dirname, 'public'))); -app.use(express.static(path.join(__dirname, 'public'))) -app.set('views', path.join(__dirname, 'views')) -app.set('view engine', 'ejs') +// View engine +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); +// Home route app.get('/', (req, res) => { - console.log(`Rendering 'pages/index' for route '/'`) - res.render('pages/index') -}) + console.log(`Rendering 'pages/index' for route '/'`); + res.render('pages/index'); +}); +/* ================================ + WhatsApp Webhook Verification + ================================ */ +app.get('/webhook', (req, res) => { + const VERIFY_TOKEN = process.env.VERIFY_TOKEN; + + const mode = req.query['hub.mode']; + const token = req.query['hub.verify_token']; + const challenge = req.query['hub.challenge']; + + if (mode === 'subscribe' && token === VERIFY_TOKEN) { + console.log('✅ Webhook verified successfully'); + return res.status(200).send(challenge); + } else { + console.log('❌ Webhook verification failed'); + return res.sendStatus(403); + } +}); + +// Start server const server = app.listen(port, () => { - console.log(`Listening on ${port}`) -}) + console.log(`Listening on ${port}`); +}); -// The number of seconds an idle Keep-Alive connection is kept open. This should be greater than the Heroku Router's -// Keep-Alive idle timeout of 90 seconds: -// - to ensure that the closing of idle connections is always initiated by the router and not the Node.js server -// - to prevent a race condition if the router sends a request to the app just as Node.js is closing the connection -// https://devcenter.heroku.com/articles/http-routing#keepalives -// https://nodejs.org/api/http.html#serverkeepalivetimeout -server.keepAliveTimeout = 95 * 1000 +// Keep-alive config (important for Render/Heroku) +server.keepAliveTimeout = 95 * 1000; +// Graceful shutdown process.on('SIGTERM', async () => { - console.log('SIGTERM signal received: gracefully shutting down') + console.log('SIGTERM signal received: gracefully shutting down'); if (server) { server.close(() => { - console.log('HTTP server closed') - }) + console.log('HTTP server closed'); + }); } -}) +}); From ffcaf680608f45385557e33abd52ef53eee80657 Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Tue, 30 Dec 2025 17:54:02 +0530 Subject: [PATCH 4/7] Update index.js Signed-off-by: 000balaai-cloud <000balaai@gmail.com> --- index.js | 86 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/index.js b/index.js index 1cf0ab1fc4..738565e826 100644 --- a/index.js +++ b/index.js @@ -1,57 +1,61 @@ -const express = require('express'); -const path = require('path'); +const express = require('express') +const path = require('path') -const port = process.env.PORT || 5006; -const app = express(); +const app = express() +const port = process.env.PORT || 5006 // Middleware -app.use(express.json()); -app.use(express.urlencoded({ extended: true })); -app.use(express.static(path.join(__dirname, 'public'))); +app.use(express.json()) +app.use(express.urlencoded({ extended: true })) -// View engine -app.set('views', path.join(__dirname, 'views')); -app.set('view engine', 'ejs'); +// Views +app.use(express.static(path.join(__dirname, 'public'))) +app.set('views', path.join(__dirname, 'views')) +app.set('view engine', 'ejs') -// Home route +// Homepage app.get('/', (req, res) => { - console.log(`Rendering 'pages/index' for route '/'`); - res.render('pages/index'); -}); + res.render('pages/index') +}) -/* ================================ - WhatsApp Webhook Verification - ================================ */ +// Webhook verification (already working) app.get('/webhook', (req, res) => { - const VERIFY_TOKEN = process.env.VERIFY_TOKEN; + const VERIFY_TOKEN = process.env.VERIFY_TOKEN - const mode = req.query['hub.mode']; - const token = req.query['hub.verify_token']; - const challenge = req.query['hub.challenge']; + const mode = req.query['hub.mode'] + const token = req.query['hub.verify_token'] + const challenge = req.query['hub.challenge'] if (mode === 'subscribe' && token === VERIFY_TOKEN) { - console.log('✅ Webhook verified successfully'); - return res.status(200).send(challenge); + console.log('Webhook verified') + res.status(200).send(challenge) } else { - console.log('❌ Webhook verification failed'); - return res.sendStatus(403); + res.sendStatus(403) } -}); +}) + +// Receive WhatsApp messages +app.post('/webhook', (req, res) => { + console.log('Incoming webhook:', JSON.stringify(req.body, null, 2)) + + const entry = req.body.entry?.[0] + const changes = entry?.changes?.[0] + const value = changes?.value + const messages = value?.messages + + if (messages && messages.length > 0) { + const from = messages[0].from + const text = messages[0].text?.body + + console.log(`Message from ${from}: ${text}`) + } + + res.sendStatus(200) +}) // Start server const server = app.listen(port, () => { - console.log(`Listening on ${port}`); -}); - -// Keep-alive config (important for Render/Heroku) -server.keepAliveTimeout = 95 * 1000; - -// Graceful shutdown -process.on('SIGTERM', async () => { - console.log('SIGTERM signal received: gracefully shutting down'); - if (server) { - server.close(() => { - console.log('HTTP server closed'); - }); - } -}); + console.log(`Listening on ${port}`) +}) + +server.keepAliveTimeout = 95 * 1000 From ae29a184245b015633f2e38add3e5ea0354266e9 Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Tue, 30 Dec 2025 17:56:30 +0530 Subject: [PATCH 5/7] Update index.js Signed-off-by: 000balaai-cloud <000balaai@gmail.com> From 8007a13ab42781d603d85dbba353ac726bd681f2 Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Wed, 31 Dec 2025 17:38:48 +0530 Subject: [PATCH 6/7] Update index.js Signed-off-by: 000balaai-cloud <000balaai@gmail.com> --- index.js | 85 +++++++++++++++++++------------------------------------- 1 file changed, 29 insertions(+), 56 deletions(-) diff --git a/index.js b/index.js index 738565e826..801c581f35 100644 --- a/index.js +++ b/index.js @@ -1,61 +1,34 @@ -const express = require('express') -const path = require('path') +app.post('/webhook', async (req, res) => { + console.log('Incoming webhook:', JSON.stringify(req.body, null, 2)); -const app = express() -const port = process.env.PORT || 5006 - -// Middleware -app.use(express.json()) -app.use(express.urlencoded({ extended: true })) - -// Views -app.use(express.static(path.join(__dirname, 'public'))) -app.set('views', path.join(__dirname, 'views')) -app.set('view engine', 'ejs') - -// Homepage -app.get('/', (req, res) => { - res.render('pages/index') -}) - -// Webhook verification (already working) -app.get('/webhook', (req, res) => { - const VERIFY_TOKEN = process.env.VERIFY_TOKEN - - const mode = req.query['hub.mode'] - const token = req.query['hub.verify_token'] - const challenge = req.query['hub.challenge'] - - if (mode === 'subscribe' && token === VERIFY_TOKEN) { - console.log('Webhook verified') - res.status(200).send(challenge) - } else { - res.sendStatus(403) - } -}) - -// Receive WhatsApp messages -app.post('/webhook', (req, res) => { - console.log('Incoming webhook:', JSON.stringify(req.body, null, 2)) - - const entry = req.body.entry?.[0] - const changes = entry?.changes?.[0] - const value = changes?.value - const messages = value?.messages + const entry = req.body.entry?.[0]; + const changes = entry?.changes?.[0]; + const value = changes?.value; + const messages = value?.messages; if (messages && messages.length > 0) { - const from = messages[0].from - const text = messages[0].text?.body - - console.log(`Message from ${from}: ${text}`) + const from = messages[0].from; + const text = messages[0].text?.body; + + console.log(`Message from ${from}: ${text}`); + + // Send auto-reply + await fetch( + `https://graph.facebook.com/v19.0/${process.env.PHONE_NUMBER_ID}/messages`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${process.env.WHATSAPP_TOKEN}` + }, + body: JSON.stringify({ + messaging_product: 'whatsapp', + to: from, + text: { body: '👋 Hello! Your message has been received.' } + }) + } + ); } - res.sendStatus(200) -}) - -// Start server -const server = app.listen(port, () => { - console.log(`Listening on ${port}`) -}) - -server.keepAliveTimeout = 95 * 1000 + res.sendStatus(200); +}); From 0e72c338222859e5c8787d7ccaccffdf8ce9eeb3 Mon Sep 17 00:00:00 2001 From: 000balaai-cloud <000balaai@gmail.com> Date: Wed, 31 Dec 2025 17:46:13 +0530 Subject: [PATCH 7/7] Update index.js Signed-off-by: 000balaai-cloud <000balaai@gmail.com> --- index.js | 48 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 801c581f35..f60f8d10f0 100644 --- a/index.js +++ b/index.js @@ -1,3 +1,40 @@ +const express = require('express'); +const path = require('path'); + +const app = express(); +const port = process.env.PORT || 5006; + +// Middleware +app.use(express.json()); +app.use(express.urlencoded({ extended: true })); + +// Static & views +app.use(express.static(path.join(__dirname, 'public'))); +app.set('views', path.join(__dirname, 'views')); +app.set('view engine', 'ejs'); + +// Home route +app.get('/', (req, res) => { + res.send('WhatsApp Webhook is running ✅'); +}); + +// 🔐 Webhook verification (Meta Step) +app.get('/webhook', (req, res) => { + const VERIFY_TOKEN = 'my_verify_token'; // must match Meta token + + const mode = req.query['hub.mode']; + const token = req.query['hub.verify_token']; + const challenge = req.query['hub.challenge']; + + if (mode === 'subscribe' && token === VERIFY_TOKEN) { + console.log('Webhook verified'); + res.status(200).send(challenge); + } else { + res.sendStatus(403); + } +}); + +// 📩 Receive WhatsApp messages app.post('/webhook', async (req, res) => { console.log('Incoming webhook:', JSON.stringify(req.body, null, 2)); @@ -12,7 +49,7 @@ app.post('/webhook', async (req, res) => { console.log(`Message from ${from}: ${text}`); - // Send auto-reply + // Auto-reply await fetch( `https://graph.facebook.com/v19.0/${process.env.PHONE_NUMBER_ID}/messages`, { @@ -24,7 +61,7 @@ app.post('/webhook', async (req, res) => { body: JSON.stringify({ messaging_product: 'whatsapp', to: from, - text: { body: '👋 Hello! Your message has been received.' } + text: { body: '👋 Hello! Your message has been received successfully.' } }) } ); @@ -32,3 +69,10 @@ app.post('/webhook', async (req, res) => { res.sendStatus(200); }); + +// Start server +const server = app.listen(port, () => { + console.log(`Server running on port ${port}`); +}); + +server.keepAliveTimeout = 95 * 1000;