├── Procfile ├── README.md ├── .gitignore ├── .sample.env ├── vercel.json ├── package.json └── app.js /Procfile: -------------------------------------------------------------------------------- 1 | web: node app.js -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WhatsAppChatBot -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .dccache 3 | .env 4 | package-lock.json -------------------------------------------------------------------------------- /.sample.env: -------------------------------------------------------------------------------- 1 | VERIFY_TOKEN=sameple token for verification 2 | ACCESS_TOKEN= access token of your number generated from devlopers.facebook.com -------------------------------------------------------------------------------- /vercel.json: -------------------------------------------------------------------------------- 1 | { 2 | "builds": [ 3 | { 4 | "src": "app.js", 5 | "use": "@vercel/node" 6 | } 7 | ], 8 | "routes": [ 9 | { 10 | "src": "/(.*)", 11 | "dest": "app.js" 12 | } 13 | ] 14 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "whatsapp", 3 | "version": "1.0.0", 4 | "description": "", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1", 8 | "start": "node app.js" 9 | }, 10 | "author": "", 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 | "node-nlp": "^4.24.0" 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | // requirements: node.js, npm, express, body-parser, axios, dotenv 2 | const express = require("express"); 3 | const body_parser = require("body-parser"); 4 | require("dotenv").config(); 5 | const app = express(); 6 | const axios = require("axios"); 7 | 8 | // set up the server 9 | app.use(body_parser.json()); 10 | 11 | // home route 12 | app.get("/", (req, res) => { 13 | res.status(200).send("Hello World"); 14 | }); 15 | 16 | // webhook route get for verification 17 | app.get("/webhook", (req, res) => { 18 | let mode = req.query["hub.mode"]; 19 | let challange = req.query["hub.challenge"]; 20 | let token = req.query["hub.verify_token"]; 21 | if (mode && token == process.env.VERIFY_TOKEN) { 22 | res.status(200).send(challange); 23 | } else { 24 | res.sendStatus(403); 25 | } 26 | }); 27 | 28 | // webhook route post for get messages 29 | app.post("/webhook", (req, res) => { 30 | if (req.body.entry) { 31 | // perameters from send messages 32 | let phon_no_id = req.body.entry[0].changes[0].value.metadata.phone_number_id; 33 | let from = req.body.entry[0].changes[0].value.messages[0].from; 34 | let name = req.body.entry[0].changes[0].value.contacts[0].profile.name; 35 | let msg_body = req.body.entry[0].changes[0].value.messages[0].text.body; 36 | axios({ 37 | method: "POST", 38 | url: `https://graph.facebook.com/v13.0/${phon_no_id}/messages?access_token=${process.env.ACCESS_TOKEN}`, 39 | data: { 40 | messaging_product: "whatsapp", 41 | to: from, 42 | text: { 43 | body: `Hi.. ${name} \nI'm InducedBot Made for replying to your messages\nMade By: IshanSingla \nYou said: ${msg_body}`, 44 | }, 45 | }, 46 | headers: { 47 | "Content-Type": "application/json", 48 | }, 49 | }); 50 | res.sendStatus(200); 51 | } else { 52 | res.sendStatus(404); 53 | } 54 | }); 55 | 56 | // set up the server port 57 | app.listen(process.env.PORT || 3000, () => { 58 | console.log("webhook is listening"); 59 | }); 60 | --------------------------------------------------------------------------------