Showing posts with label pemrograman. Show all posts
Showing posts with label pemrograman. Show all posts

Send Whatsapp with Node JS

You can send text with whatsapp use nodejs. This project can you use to build project with automatic send message or you can build project to reminder with whatsapp text.


First, you use whatsapp-web.js download with npm to install in your project.

npm i whatsapp-web.js

Setting in app.js (default your primary js)

const express = require('express')
const expressLayouts = require('express-ejs-layouts')
const multer = require('multer')
const app = express()
const port = 4000

const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './uploads')
},
filename: function (req, file, cb) {
const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
cb(null, file.fieldname + '-' + uniqueSuffix + ".jpg")
}
})

const upload = multer({
storage: storage
})

const {
Client,
LocalAuth,
MessageMedia
} = require('whatsapp-web.js');

const client = new Client({
authStrategy: new LocalAuth(),
puppeteer: {
headless: false
}
});

client.on('qr', (qr) => {
// Generate and scan this code with your phone
console.log('QR RECEIVED', qr);
});

client.on('ready', () => {
console.log('Client is ready!');
});

client.on('message', msg => {
if (msg.body == '!ping') {
msg.reply('pong');
}
});

client.initialize();

app.set('view engine', 'ejs');
app.use(expressLayouts);
app.use(express.urlencoded({
extended: true
}))

app.get('/', function (req, res) {
var locals = {
layout: 'kirimpesan',
title: 'Kirim Pesan',
description: 'Kirim pesan wa',
header: 'KIRIM PESAN WA'
};
res.render('kirimpesan', locals);
});

app.post('/kirim', upload.single('gambar'), (req, res) => {
let number = req.body.nowa + "@c.us";
let message = req.body.pesan;
let gambar = MessageMedia.fromFilePath(req.file.path)

client.sendMessage(number, gambar, {
caption: message
});

//console.log(number);

res.json()


})

app.listen(port, () => {
console.log(`Example app listening on port ${port}`)
})


Make form in your views folder (if views folder not found, create this folder) and create ejs file ex: kirim.ejs. And copy code below to your ejs file


<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= title %> </title>
</head>

<body>
<h1><%= header %> </h1>
<form action="/kirim" method="post" enctype="multipart/form-data">
<div>
<label for="nowa">Masukan No WA</label><br>
<input type="text" name="nowa" id="nowa" />
</div>

<div>
<label for="pesan">Tuliskan Pesannya</label><br>
<input type="text" name="pesan" id="pesan" />
</div>

<div>
<label for="gambar">Kirim Gambar</label><br>
<input type="file" name="gambar" id="gambar" />
</div>

<div>
<input type="submit" name="submit" id="submit" value="Kirim Pesan" />
</div>
</form>

</body>

</html>


Video lengkap saya bisa di tonton disini 

  1. Part 1 - Kirim WA via aplikasi buatan sendiri 
  2. Part 2 - Buat form untuk kirim wa
  3. Part 3 - -Kirim gambar ke WA dari aplikasi buatan sendiri

 

 

[Read more]