mirror of
https://github.com/cheveguerra/ws-api-node.git
synced 2026-04-17 19:36:23 +00:00
asdasd
asdasdad
This commit is contained in:
60
components/auth.js
Normal file
60
components/auth.js
Normal file
@@ -0,0 +1,60 @@
|
||||
const router = require("express").Router();
|
||||
const fs = require("fs");
|
||||
|
||||
router.get("/checkauth", async (req, res) => {
|
||||
client
|
||||
.getState()
|
||||
.then((data) => {
|
||||
console.log(data);
|
||||
res.send(data);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (err) {
|
||||
res.send("DISCONNECTED");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
router.get("/getqr", async (req, res) => {
|
||||
client
|
||||
.getState()
|
||||
.then((data) => {
|
||||
if (data) {
|
||||
res.write("<html><body><h2>Already Authenticated</h2></body></html>");
|
||||
res.end();
|
||||
} else sendQr(res);
|
||||
})
|
||||
.catch(() => sendQr(res));
|
||||
});
|
||||
|
||||
function sendQr(res) {
|
||||
fs.readFile("components/last.qr", (err, last_qr) => {
|
||||
if (!err && last_qr) {
|
||||
var page = `
|
||||
<html>
|
||||
<body>
|
||||
<script type="module">
|
||||
</script>
|
||||
<div id="qrcode"></div>
|
||||
<script type="module">
|
||||
import QrCreator from "https://cdn.jsdelivr.net/npm/qr-creator/dist/qr-creator.es6.min.js";
|
||||
let container = document.getElementById("qrcode");
|
||||
QrCreator.render({
|
||||
text: "${last_qr}",
|
||||
radius: 0.5, // 0.0 to 0.5
|
||||
ecLevel: "H", // L, M, Q, H
|
||||
fill: "#536DFE", // foreground color
|
||||
background: null, // color or null for transparent
|
||||
size: 256, // in pixels
|
||||
}, container);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
res.write(page);
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = router;
|
||||
148
components/chatting.js
Normal file
148
components/chatting.js
Normal file
@@ -0,0 +1,148 @@
|
||||
const router = require('express').Router();
|
||||
const { MessageMedia, Location } = require("whatsapp-web.js");
|
||||
const request = require('request')
|
||||
const vuri = require('valid-url');
|
||||
const fs = require('fs');
|
||||
|
||||
const mediadownloader = (url, path, callback) => {
|
||||
request.head(url, (err, res, body) => {
|
||||
request(url)
|
||||
.pipe(fs.createWriteStream(path))
|
||||
.on('close', callback)
|
||||
})
|
||||
}
|
||||
|
||||
router.post('/sendmessage', async (req,res) => {
|
||||
let phone = req.body.phone;
|
||||
let message = req.body.message;
|
||||
console.log(message);
|
||||
console.log(phone);
|
||||
|
||||
if (phone == undefined || message == undefined) {
|
||||
res.send({ status:"error", message:"please enter valid phone and message" })
|
||||
} else {
|
||||
client.sendMessage(phone + '@c.us', message).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status:'success', message: `Message successfully sent to ${phone}` })
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/sendimage/:phone', async (req,res) => {
|
||||
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
||||
|
||||
let phone = req.body.phone;
|
||||
let image = req.body.image;
|
||||
let caption = req.body.caption;
|
||||
|
||||
if (phone == undefined || image == undefined) {
|
||||
res.send({ status: "error", message: "please enter valid phone and base64/url of image" })
|
||||
} else {
|
||||
if (base64regex.test(image)) {
|
||||
let media = new MessageMedia('image/png',image);
|
||||
client.sendMessage(`${phone}@c.us`, media, { caption: caption || '' }).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
|
||||
}
|
||||
});
|
||||
} else if (vuri.isWebUri(image)) {
|
||||
if (!fs.existsSync('./temp')) {
|
||||
await fs.mkdirSync('./temp');
|
||||
}
|
||||
|
||||
var path = './temp/' + image.split("/").slice(-1)[0]
|
||||
mediadownloader(image, path, () => {
|
||||
let media = MessageMedia.fromFilePath(path);
|
||||
|
||||
client.sendMessage(`${phone}@c.us`, media, { caption: caption || '' }).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
|
||||
fs.unlinkSync(path)
|
||||
}
|
||||
});
|
||||
})
|
||||
} else {
|
||||
res.send({ status:'error', message: 'Invalid URL/Base64 Encoded Media' })
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/sendpdf/:phone', async (req,res) => {
|
||||
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
||||
|
||||
let phone = req.body.phone;
|
||||
let pdf = req.body.pdf;
|
||||
|
||||
if (phone == undefined || pdf == undefined) {
|
||||
res.send({ status: "error", message: "please enter valid phone and base64/url of pdf" })
|
||||
} else {
|
||||
if (base64regex.test(pdf)) {
|
||||
let media = new MessageMedia('application/pdf', pdf);
|
||||
client.sendMessage(`${phone}@c.us`, media).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
|
||||
}
|
||||
});
|
||||
} else if (vuri.isWebUri(pdf)) {
|
||||
if (!fs.existsSync('./temp')) {
|
||||
await fs.mkdirSync('./temp');
|
||||
}
|
||||
|
||||
var path = './temp/' + pdf.split("/").slice(-1)[0]
|
||||
mediadownloader(pdf, path, () => {
|
||||
let media = MessageMedia.fromFilePath(path);
|
||||
client.sendMessage(`${phone}@c.us`, media).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
|
||||
fs.unlinkSync(path)
|
||||
}
|
||||
});
|
||||
})
|
||||
} else {
|
||||
res.send({ status: 'error', message: 'Invalid URL/Base64 Encoded Media' })
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/sendlocation/:phone', async (req, res) => {
|
||||
let phone = req.params.phone;
|
||||
let latitude = req.body.latitude;
|
||||
let longitude = req.body.longitude;
|
||||
let desc = req.body.description;
|
||||
|
||||
if (phone == undefined || latitude == undefined || longitude == undefined) {
|
||||
res.send({ status: "error", message: "please enter valid phone, latitude and longitude" })
|
||||
} else {
|
||||
let loc = new Location(latitude, longitude, desc || "");
|
||||
client.sendMessage(`${phone}@c.us`, loc).then((response)=>{
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `MediaMessage successfully sent to ${phone}` })
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/getchatbyid/:phone', async (req, res) => {
|
||||
let phone = req.params.phone;
|
||||
if (phone == undefined) {
|
||||
res.send({status:"error",message:"please enter valid phone number"});
|
||||
} else {
|
||||
client.getChatById(`${phone}@c.us`).then((chat) => {
|
||||
res.send({ status:"success", message: chat });
|
||||
}).catch(() => {
|
||||
console.error("getchaterror")
|
||||
res.send({ status: "error", message: "getchaterror" })
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/getchats', async (req, res) => {
|
||||
client.getChats().then((chats) => {
|
||||
res.send({ status: "success", message: chats});
|
||||
}).catch(() => {
|
||||
res.send({ status: "error",message: "getchatserror" })
|
||||
})
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
49
components/contact.js
Normal file
49
components/contact.js
Normal file
@@ -0,0 +1,49 @@
|
||||
const router = require('express').Router();
|
||||
|
||||
router.get('/getcontacts', (req, res) => {
|
||||
client.getContacts().then((contacts) => {
|
||||
res.send(JSON.stringify(contacts));
|
||||
});
|
||||
});
|
||||
|
||||
router.get('/getcontact/:phone', async (req, res) => {
|
||||
let phone = req.params.phone;
|
||||
|
||||
if (phone != undefined) {
|
||||
client.getContactById(`${phone}@c.us`).then((contact) => {
|
||||
res.send(JSON.stringify(contact));
|
||||
}).catch((err) => {
|
||||
res.send({ status: 'error', message: 'Not found' });
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/getprofilepic/:phone', async (req, res) => {
|
||||
let phone = req.params.phone;
|
||||
|
||||
if (phone != undefined) {
|
||||
client.getProfilePicUrl(`${phone}@c.us`).then((imgurl) => {
|
||||
if (imgurl) {
|
||||
res.send({ status: 'success', message: imgurl });
|
||||
} else {
|
||||
res.send({ status: 'error', message: 'Not Found' });
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
|
||||
router.get('/isregistereduser/:phone', async (req, res) => {
|
||||
let phone = req.params.phone;
|
||||
|
||||
if (phone != undefined) {
|
||||
client.isRegisteredUser(`${phone}@c.us`).then((is) => {
|
||||
|
||||
is ? res.send({ status: 'success', message: `${phone} is a whatsapp user` })
|
||||
: res.send({ status: 'error', message: `${phone} is not a whatsapp user` });
|
||||
})
|
||||
} else {
|
||||
res.send({ status: 'error', message: 'Invalid Phone number' });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
164
components/group.js
Normal file
164
components/group.js
Normal file
@@ -0,0 +1,164 @@
|
||||
const router = require('express').Router();
|
||||
const { MessageMedia, Location } = require("whatsapp-web.js");
|
||||
const request = require('request')
|
||||
const vuri = require('valid-url');
|
||||
const fs = require('fs');
|
||||
|
||||
const mediadownloader = (url, path, callback) => {
|
||||
request.head(url, (err, res, body) => {
|
||||
request(url)
|
||||
.pipe(fs.createWriteStream(path))
|
||||
.on('close', callback)
|
||||
})
|
||||
}
|
||||
|
||||
router.post('/sendmessage/:chatname', async (req, res) => {
|
||||
let chatname = req.params.chatname;
|
||||
let message = req.body.message;
|
||||
|
||||
if (chatname == undefined || message == undefined) {
|
||||
res.send({ status: "error", message: "please enter valid chatname and message" })
|
||||
} else {
|
||||
client.getChats().then((data) => {
|
||||
data.forEach(chat => {
|
||||
if (chat.id.server === "g.us" && chat.name === chatname) {
|
||||
client.sendMessage(chat.id._serialized, message).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `Message successfully send to ${chatname}` })
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/sendimage/:chatname', async (req, res) => {
|
||||
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
||||
|
||||
let chatname = req.params.chatname;
|
||||
let image = req.body.image;
|
||||
let caption = req.body.caption;
|
||||
|
||||
if (chatname == undefined || image == undefined) {
|
||||
res.send({status:"error",message:"please enter valid chatname and base64/url of image"})
|
||||
} else {
|
||||
if (base64regex.test(image)) {
|
||||
client.getChats().then((data) => {
|
||||
data.forEach(chat => {
|
||||
if (chat.id.server === "g.us" && chat.name === chatname) {
|
||||
if (!fs.existsSync('./temp')) {
|
||||
fs.mkdirSync('./temp');
|
||||
}
|
||||
|
||||
let media = new MessageMedia('image/png', image);
|
||||
client.sendMessage(chat.id._serialized, media, { caption: caption || "" }).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `Message successfully send to ${chatname}` })
|
||||
fs.unlinkSync(path)
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
} else if (vuri.isWebUri(image)) {
|
||||
var path = './temp/' + image.split("/").slice(-1)[0]
|
||||
client.getChats().then((data) => {
|
||||
data.forEach(chat => {
|
||||
if (chat.id.server === "g.us" && chat.name === chatname) {
|
||||
mediadownloader(image, path, () => {
|
||||
let media = MessageMedia.fromFilePath(path);
|
||||
client.sendMessage(chat.id._serialized, media, { caption: caption || "" }).then((response)=>{
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `Message successfully send to ${chatname}` })
|
||||
fs.unlinkSync(path)
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.send({ status: 'error', message: 'Invalid URL/Base64 Encoded Media' })
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/sendpdf/:chatname', async (req, res) => {
|
||||
var base64regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
||||
|
||||
let chatname = req.params.chatname;
|
||||
let pdf = req.body.pdf;
|
||||
|
||||
if (chatname == undefined || pdf == undefined) {
|
||||
res.send({ status: "error", message: "please enter valid chatname and base64/url of pdf" })
|
||||
} else {
|
||||
if (base64regex.test(pdf)) {
|
||||
client.getChats().then((data) => {
|
||||
data.some(chat => {
|
||||
if (chat.id.server === "g.us" && chat.name === chatname) {
|
||||
if (!fs.existsSync('./temp')) {
|
||||
fs.mkdirSync('./temp');
|
||||
}
|
||||
let media = new MessageMedia('application/pdf', pdf);
|
||||
client.sendMessage(chat.id._serialized, media).then((response) => {
|
||||
if(response.id.fromMe){
|
||||
res.send({ status: 'success', message: `Message successfully send to ${chatname}` })
|
||||
fs.unlinkSync(path)
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
} else if (vuri.isWebUri(pdf)) {
|
||||
var path = './temp/' + pdf.split("/").slice(-1)[0]
|
||||
client.getChats().then((data) => {
|
||||
data.some(chat => {
|
||||
if (chat.id.server === "g.us" && chat.name === chatname) {
|
||||
mediadownloader(image, path, () => {
|
||||
let media = MessageMedia.fromFilePath(path);
|
||||
client.sendMessage(chat.id._serialized, media).then((response)=>{
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `Message successfully send to ${chatname}` })
|
||||
fs.unlinkSync(path)
|
||||
}
|
||||
});
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
} else {
|
||||
res.send({ status: 'error', message: 'Invalid URL/Base64 Encoded Media' })
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
router.post('/sendlocation/:chatname', async (req, res) => {
|
||||
let chatname = req.params.chatname;
|
||||
let latitude = req.body.latitude;
|
||||
let longitude = req.body.longitude;
|
||||
let desc = req.body.description;
|
||||
|
||||
if (chatname == undefined || latitude == undefined || longitude == undefined) {
|
||||
res.send({ status: "error", message: "please enter valid phone, latitude and longitude" })
|
||||
} else {
|
||||
client.getChats().then((data) => {
|
||||
data.some(chat => {
|
||||
if (chat.id.server === "g.us" && chat.name === chatname) {
|
||||
let loc = new Location(latitude, longitude, desc || "");
|
||||
client.sendMessage(chat.id._serialized, loc).then((response) => {
|
||||
if (response.id.fromMe) {
|
||||
res.send({ status: 'success', message: `Message successfully send to ${chatname}` })
|
||||
}
|
||||
});
|
||||
return true;
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user