├── .env ├── .gitignore ├── package.json └── index.js /.env: -------------------------------------------------------------------------------- 1 | TOKEN= 2 | MYTOKEN= -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "webhook-in-nodejs", 3 | "version": "1.0.0", 4 | "description": "webhook in nodejs using express framework - whatsapp cloud api", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start":"node index.js" 9 | }, 10 | "author": "Prasath from PMS", 11 | "license": "ISC", 12 | "dependencies": { 13 | "axios": "^0.27.2", 14 | "body-parser": "^1.20.0", 15 | "dotenv": "^16.0.1", 16 | "express": "^4.18.1" 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const express=require("express"); 2 | const body_parser=require("body-parser"); 3 | const axios=require("axios"); 4 | require('dotenv').config(); 5 | 6 | const app=express().use(body_parser.json()); 7 | 8 | const token=process.env.TOKEN; 9 | const mytoken=process.env.MYTOKEN;//prasath_token 10 | 11 | app.listen(process.env.PORT,()=>{ 12 | console.log("webhook is listening"); 13 | }); 14 | 15 | //to verify the callback url from dashboard side - cloud api side 16 | app.get("/webhook",(req,res)=>{ 17 | let mode=req.query["hub.mode"]; 18 | let challange=req.query["hub.challenge"]; 19 | let token=req.query["hub.verify_token"]; 20 | 21 | 22 | if(mode && token){ 23 | 24 | if(mode==="subscribe" && token===mytoken){ 25 | res.status(200).send(challange); 26 | }else{ 27 | res.status(403); 28 | } 29 | 30 | } 31 | 32 | }); 33 | 34 | app.post("/webhook",(req,res)=>{ //i want some 35 | 36 | let body_param=req.body; 37 | 38 | console.log(JSON.stringify(body_param,null,2)); 39 | 40 | if(body_param.object){ 41 | console.log("inside body param"); 42 | if(body_param.entry && 43 | body_param.entry[0].changes && 44 | body_param.entry[0].changes[0].value.messages && 45 | body_param.entry[0].changes[0].value.messages[0] 46 | ){ 47 | let phon_no_id=body_param.entry[0].changes[0].value.metadata.phone_number_id; 48 | let from = body_param.entry[0].changes[0].value.messages[0].from; 49 | let msg_body = body_param.entry[0].changes[0].value.messages[0].text.body; 50 | 51 | console.log("phone number "+phon_no_id); 52 | console.log("from "+from); 53 | console.log("boady param "+msg_body); 54 | 55 | axios({ 56 | method:"POST", 57 | url:"https://graph.facebook.com/v13.0/"+phon_no_id+"/messages?access_token="+token, 58 | data:{ 59 | messaging_product:"whatsapp", 60 | to:from, 61 | text:{ 62 | body:"Hi.. I'm Prasath, your message is "+msg_body 63 | } 64 | }, 65 | headers:{ 66 | "Content-Type":"application/json" 67 | } 68 | 69 | }); 70 | 71 | res.sendStatus(200); 72 | }else{ 73 | res.sendStatus(404); 74 | } 75 | 76 | } 77 | 78 | }); 79 | 80 | app.get("/",(req,res)=>{ 81 | res.status(200).send("hello this is webhook setup"); 82 | }); --------------------------------------------------------------------------------