Skip to content

Commit

Permalink
Sending Image from URL
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavms13 committed May 26, 2020
1 parent eb447cd commit 207decf
Show file tree
Hide file tree
Showing 6 changed files with 587 additions and 117 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
session.json
session.json
temp
4 changes: 2 additions & 2 deletions APIDOC.MD
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<u>Sending Images</u>
``POST : /chat/sendimage/<phone_number>``
> Request Body
> - image - contains the base64 encoded image to be sent
> - image - contains the base64 encoded / URL of image to be sent
> - caption - (optional) - contains caption for the message
<hr>
Expand Down Expand Up @@ -49,7 +49,7 @@
<u>Sending Images</u>
``POST : /group/sendimage/<Group_Name>``
> Request Body
> - image - contains the base64 encoded image to be sent
> - image - contains the base64 encoded / URL of image to be sent
> - caption - (optional) - contains caption for the message
<hr>
Expand Down
43 changes: 37 additions & 6 deletions components/chatting.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
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/:phone', async (req,res) => {
let phone = req.params.phone;
Expand All @@ -17,22 +28,42 @@ router.post('/sendmessage/:phone', async (req,res) => {
});

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.params.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 encoded image"})
res.send({status:"error",message:"please enter valid phone and base64/url of image"})
}else{
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})
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})
}
});
})
}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;
Expand Down
53 changes: 41 additions & 12 deletions components/group.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
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');

router.post('/sendmessage/:chatname', async (req,res) => {
let chatname = req.params.chatname;
Expand All @@ -23,25 +26,51 @@ router.post('/sendmessage/:chatname', async (req,res) => {
});

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 encoded image"})
res.send({status:"error",message:"please enter valid chatname and base64/url of image"})
}else{
client.getChats().then((data) => {
data.forEach(chat => {
if(chat.id.server==="g.us" && chat.name===chatname){
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})
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})
}
});
}
});
});
}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})
}
});
});

}
});
});
}else{
res.send({status:'error',message:'Invalid URL/Base64 Encoded Media'})
}
}
});

Expand Down
Loading

0 comments on commit 207decf

Please sign in to comment.