forked from esteves67/Whatsapp-API-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
161 lines (98 loc) · 3.5 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
const axios = require('axios');
const mime = require('mime-types');
const qrcode = require('qrcode');
const { Client, MessageMedia } = require('whatsapp-web.js');
const fs = require('fs');
const app = express();
app.use(cors());
app.use(bodyParser.json());
function makeid(length) {
var result = '';
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var charactersLength = characters.length;
for ( var i = 0; i < length; i++ ) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
let client;
app.get('/generate', (req, res)=>{
client = new Client();
client.on('qr', async qr => {
try{
const bufferImage = await qrcode.toDataURL(qr);
res.status(200).json({status: "success", qr: bufferImage}).end();
//res.send('<img src="'+bufferImage+'" width="400" />')
}catch(err){
res.status(200).json({status: "error", detail: "Failed to generate QR Code"}).end();
}
});
client.on('ready', () => {
console.log('Client is ready!');
});
client.initialize();
});
app.post('/send', async (req, res)=>{
let num = req.body.num;
let msg = req.body.message;
let numbers = [];
num.split(',').map((number)=>{
numbers.push(number.trim())
});
try{
const success = [];
for(let i=0; i<numbers.length; i++){
let number = numbers[i]+'@c.us';
const data = await client.sendMessage(number, msg);
if(data.ack === 0){
success.push({success: true, number: numbers[i], detail: numbers[i] + ", Message status success"});
}else{
success.push({success: false, number: numbers[i], detail: numbers[i] + ", Message status failed"});
}
}
res.json({status: success})
}catch(err){
res.json({status: "error", detail: "Failed to send message, Please authenticate or scan generated barcode"});
}
})
app.post('/sendMedia', async (req, res)=>{
let fileUrl = req.body.file;
let caption = req.body.message;
let num = req.body.num;
let numbers = [];
num.split(',').map((number)=>{
numbers.push(number.trim())
});
try{
let request = await axios.get(fileUrl, {responseType: 'stream'});
let cType = request.headers['content-type'];
const fileExt = mime.extension(cType);
let code = makeid(10).toUpperCase();
const dirLoc = './public/'+code
fs.mkdirSync(dirLoc);
const fname = makeid(10).toUpperCase()+"."+fileExt;
const dataUpload = request.data.pipe(fs.createWriteStream(dirLoc+'/'+fname));
dataUpload.on('finish', async ()=>{
const media = MessageMedia.fromFilePath(dirLoc+"/"+fname);
const success = [];
for(let i=0; i<numbers.length; i++){
const data = await client.sendMessage(numbers[i]+'@c.us', media, {caption: caption});
if(data.ack === 0){
success.push({success: true, number: numbers[i], detail: numbers[i] + ", Message status success"});
}else{
success.push({success: false, number: numbers[i], detail: numbers[i] + ", Message status failed"});
}
}
fs.unlinkSync(dirLoc+"/"+fname);
fs.rmdirSync(dirLoc);
res.json({status: success});
res.end();
})
}catch(err){
res.json({status: "error", details: "failed to check the URL!"});
}
});
app.listen('3030');