Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion components/chatting.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { MessageMedia, Location } = require("whatsapp-web.js");
const request = require('request')
const vuri = require('valid-url');
const fs = require('fs');
const config = require("./../config.json");

const mediadownloader = (url, path, callback) => {
request.head(url, (err, res, body) => {
Expand All @@ -12,6 +13,9 @@ const mediadownloader = (url, path, callback) => {
})
}

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/";

router.post('/sendmessage/:phone', async (req,res) => {
let phone = req.params.phone;
let message = req.body.message;
Expand All @@ -24,6 +28,17 @@ router.post('/sendmessage/:phone', async (req,res) => {
res.send({ status:'success', message: `Message successfully sent to ${phone}` })
}
});

MongoClient.connect(url, function(err, db) {
if (err) throw err;
var dbo = db.db("whatsappdb");
var msgobj = {number: phone, message: message, date: formatDate(new Date()), app: config.port };
dbo.collection("messages").insertOne(msgobj, function(err, res) {
if (err) throw err;
console.log("1 document inserted");
db.close();
});
});
}
});

Expand Down Expand Up @@ -143,4 +158,25 @@ router.get('/getchats', async (req, res) => {
})
});

module.exports = router;

function padTo2Digits(num) {
return num.toString().padStart(2, '0');
}

function formatDate(date) {
return (
[
date.getFullYear(),
padTo2Digits(date.getMonth() + 1),
padTo2Digits(date.getDate()),
].join('-') +
' ' +
[
padTo2Digits(date.getHours()),
padTo2Digits(date.getMinutes()),
padTo2Digits(date.getSeconds()),
].join(':')
);
}

module.exports = router;