-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathindex.js
More file actions
82 lines (62 loc) · 2.29 KB
/
Copy pathindex.js
File metadata and controls
82 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express=require("express");
const body_parser=require("body-parser");
const axios=require("axios");
require('dotenv').config();
const app=express().use(body_parser.json());
const token=process.env.TOKEN;
const mytoken=process.env.MYTOKEN;//prasath_token
app.listen(process.env.PORT,()=>{
console.log("webhook is listening");
});
//to verify the callback url from dashboard side - cloud api side
app.get("/webhook",(req,res)=>{
let mode=req.query["hub.mode"];
let challange=req.query["hub.challenge"];
let token=req.query["hub.verify_token"];
if(mode && token){
if(mode==="subscribe" && token===mytoken){
res.status(200).send(challange);
}else{
res.status(403);
}
}
});
app.post("/webhook",(req,res)=>{ //i want some
let body_param=req.body;
console.log(JSON.stringify(body_param,null,2));
if(body_param.object){
console.log("inside body param");
if(body_param.entry &&
body_param.entry[0].changes &&
body_param.entry[0].changes[0].value.messages &&
body_param.entry[0].changes[0].value.messages[0]
){
let phon_no_id=body_param.entry[0].changes[0].value.metadata.phone_number_id;
let from = body_param.entry[0].changes[0].value.messages[0].from;
let msg_body = body_param.entry[0].changes[0].value.messages[0].text.body;
console.log("phone number "+phon_no_id);
console.log("from "+from);
console.log("boady param "+msg_body);
axios({
method:"POST",
url:"https://graph.facebook.com/v13.0/"+phon_no_id+"/messages?access_token="+token,
data:{
messaging_product:"whatsapp",
to:from,
text:{
body:"Hi.. I'm Prasath, your message is "+msg_body
}
},
headers:{
"Content-Type":"application/json"
}
});
res.sendStatus(200);
}else{
res.sendStatus(404);
}
}
});
app.get("/",(req,res)=>{
res.status(200).send("hello this is webhook setup");
});