Berikut ini tutorial cara mengirim pesan whatsapp menggunakan macro excel. Untuk membuat aplikasi ini dibutuhkan software node js yang bisa didownload disini.
Langkah2 instalasi wa-web JS:
- Setelah selesai instalasi nodejs, buatlah folder waweb
- Masuk ke folder tersebut melalui cmd
- Ketikkan perintah di bawah ini pada layar cmd:
npm i github:lindionez/whatsapp-web.js
npm i qrcode-terminal
npm i express
npm i cors
4. buat file server.js dengan isi sebagai berikut:
const {Client, LocalAuth} = require('whatsapp-web.js');
const qrcode = require('qrcode-terminal');
const express = require("express");
const cors = require("cors");
//Variabel global
const myToken = '12345';
var serverStatus = ''; //qrcode, portReady, waReady, autentikasi
var qrCode='';
const app = express();
app.use(express.json());
app.use(express.urlencoded({extended: true}));
const client = new Client({
puppeteer: { headless: true },
authStrategy: new LocalAuth(),
webVersionCache: {
type: "none"
}
});
let corsOptions = {
origin: '*',
optionsSuccessStatus: 200,
methods: "GET, POST"
}
app.use(cors(corsOptions));
client.on('qr', (qr) => {
console.log('Token Whatsapp ', qr);
qrcode.generate(qr, {small: true});
//update status server dan pengisian qrCode
serverStatus = 'qrcode';
qrCode=qr;
});
client.on('ready', () => {
serverStatus = 'waReady';
qrCode='';
console.log('Client Siap !');
});
client.on('authenticated', () => {
serverStatus = 'autentikasi';
qrCode='';
console.log('Terautentikasi');
});
client.initialize();
app.get("/", async (req, res) => {
res.send(serverStatus);
})
app.post("/kirimpesan", (req, res) => {
const nomor = req.body.nomor;
const pesan = req.body.pesan;
client.sendMessage(nomor + "@c.us", pesan).then(response => {
res.status(200).json({
status: "Berhasil"
});
console.log("Pesan Berhasil di Kirim Ke " + nomor);
}).catch(err => {
res.status(500).json({
status: "Gagal"
});
console.log("Pesan Gagal di Kirim Ke " + nomor);
})
})
app.post("/kirimfile", async (req, res) => {
const nomor = req.body.nomor;
const file = req.body.file;
const caption = req.body.caption;
let nmfile = file;
nmfile = nmfile.replace("./","_");
nmfile = nmfile.replace(/\\/,"_");
nmfile = nmfile.replace("/","_");
nmfile = nmfile.replace(":","_");
nmfile = nmfile.replace("%","_");
const { MessageMedia } = require('whatsapp-web.js');
const path = file;
const fs = require('fs').promises;
const gambar = await fs.readFile(path, {encoding: 'base64'});
var media = new MessageMedia("document", gambar,nmfile);
client.sendMessage(nomor + "@c.us", media,{caption: caption}).then(response => {
res.status(200).json({
status: "Berhasil"
});
console.log("Pesan Berhasil di Kirim Ke " + nomor);
}).catch(err => {
res.status(500).json({
status: "Gagal"
});
console.log("Pesan Gagal di Kirim Ke " + nomor);
})
})
app.get("/logout", async (req, res) => {
const token = req.query.token;
if (token === myToken) {
try {
await client.logout();
serverStatus = 'qrcode'; // status kembali ke qrcode
qrCode = '';
// MULAI ULANG supaya QR baru muncul
await client.initialize();
res.send('logout & restart');
} catch (err) {
res.status(500).send("Gagal logout: " + err.message);
}
} else {
res.send('Token Invalid');
}
});
app.listen(250, function(){
serverStatus = 'portReady';
console.log("Server Berjalan di Port: " + 250);
})
Untuk lebih jelasnya silahkan lihat tutorial video di bawah ini.