├── .gitignore ├── nodemon.json ├── readme.md ├── package.json ├── index.html └── app.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | whatsapp-session.json -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "ignore": ["whatsapp-session.json"] 3 | } -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # Whatsapp API Tutorial 2 | 3 | Hi, this is the implementation example of whatsapp-web.js 4 | 5 | Watch the tutorials: 6 | - Whatsapp API Tutorial: Part 1 7 | - Whatsapp API Tutorial: Part 2 8 | 9 | ### How to use? 10 | - Clone or download this repo 11 | - Enter to the project directory 12 | - Run `npm install` 13 | - Run `npm run start` 14 | - Scan the QR Code 15 | - Enjoy! -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "whatsapp-api", 3 | "version": "1.0.0", 4 | "description": "Whatsapp api by Ngekoding", 5 | "main": "app.js", 6 | "scripts": { 7 | "start": "nodemon app.js", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [ 11 | "whatsapp-api", 12 | "node.js" 13 | ], 14 | "author": "Nur Muhammad", 15 | "license": "MIT", 16 | "dependencies": { 17 | "express": "^4.17.1", 18 | "http": "0.0.1-security", 19 | "qrcode": "^1.4.4", 20 | "socket.io": "^2.3.0", 21 | "whatsapp-web.js": "^1.8.2" 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Whatsapp API by Ngekoding 5 | 6 | 7 | 8 |
9 |

Whatsapp API

10 |

Powered by Ngekoding

11 | QR Code 12 |

Logs:

13 | 14 |
15 | 16 | 17 | 18 | 39 | 40 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | const { Client } = require('whatsapp-web.js'); 2 | const express = require('express'); 3 | const socketIO = require('socket.io'); 4 | const qrcode = require('qrcode'); 5 | const http = require('http'); 6 | const fs = require('fs'); 7 | 8 | const app = express(); 9 | const server = http.createServer(app); 10 | const io = socketIO(server); 11 | 12 | app.use(express.json()); 13 | app.use(express.urlencoded({ extended: true })); 14 | 15 | const SESSION_FILE_PATH = './whatsapp-session.json'; 16 | let sessionCfg; 17 | if (fs.existsSync(SESSION_FILE_PATH)) { 18 | sessionCfg = require(SESSION_FILE_PATH); 19 | } 20 | 21 | app.get('/', (req, res) => { 22 | res.sendFile('index.html', { root: __dirname }); 23 | }); 24 | 25 | const client = new Client({ puppeteer: { headless: true }, session: sessionCfg }); 26 | 27 | client.on('message', msg => { 28 | if (msg.body == '!ping') { 29 | msg.reply('pong'); 30 | } else if (msg.body == 'good morning') { 31 | msg.reply('selamat pagi'); 32 | } 33 | }); 34 | 35 | client.initialize(); 36 | 37 | // Socket IO 38 | io.on('connection', function(socket) { 39 | socket.emit('message', 'Connecting...'); 40 | 41 | client.on('qr', (qr) => { 42 | console.log('QR RECEIVED', qr); 43 | qrcode.toDataURL(qr, (err, url) => { 44 | socket.emit('qr', url); 45 | socket.emit('message', 'QR Code received, scan please!'); 46 | }); 47 | }); 48 | 49 | client.on('ready', () => { 50 | socket.emit('ready', 'Whatsapp is ready!'); 51 | socket.emit('message', 'Whatsapp is ready!'); 52 | }); 53 | 54 | client.on('authenticated', (session) => { 55 | socket.emit('authenticated', 'Whatsapp is authenticated!'); 56 | socket.emit('message', 'Whatsapp is authenticated!'); 57 | console.log('AUTHENTICATED', session); 58 | sessionCfg=session; 59 | fs.writeFile(SESSION_FILE_PATH, JSON.stringify(session), function (err) { 60 | if (err) { 61 | console.error(err); 62 | } 63 | }); 64 | }); 65 | }); 66 | 67 | // Send message 68 | app.post('/send-message', (req, res) => { 69 | const number = req.body.number; 70 | const message = req.body.message; 71 | 72 | client.sendMessage(number, message).then(response => { 73 | res.status(200).json({ 74 | status: true, 75 | response: response 76 | }); 77 | }).catch(err => { 78 | res.status(500).json({ 79 | status: false, 80 | response: err 81 | }); 82 | }); 83 | }); 84 | 85 | server.listen(8000, function() { 86 | console.log('App running on *: ' + 8000); 87 | }); 88 | --------------------------------------------------------------------------------