-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
161 lines (137 loc) · 4.71 KB
/
index.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
161
const fs = require('fs');
const https = require('https');
const express = require("express");
const cors = require('cors');
const Sessions = require("./sessions");
require('dotenv').config();
var app = express();
app.use(cors());
app.use(express.json());
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
if (process.env.HTTPS == 1) { //with ssl
https.createServer(
{
key: fs.readFileSync(process.env.SSL_KEY_PATH),
cert: fs.readFileSync(process.env.SSL_CERT_PATH)
},
app).listen(process.env.HOST_PORT);
console.log("Https server running on port " + process.env.HOST_PORT);
} else { //http
app.listen(process.env.HOST_PORT, () => {
console.log("Http server running on port " + process.env.HOST_PORT);
});
}//http
app.get("/start", async (req, res, next) => {
console.log("starting..." + req.query.sessionName);
var session = await Sessions.start(req.query.sessionName);
if (["CONNECTED", "QRCODE", "STARTING"].includes(session.state)) {
res.status(200).json({ result: 'success', message: session.state });
} else {
res.status(200).json({ result: 'error', message: session.state });
}
});//start
app.get("/qrcode", async (req, res, next) => {
console.log("qrcode..." + req.query.sessionName);
var session = Sessions.getSession(req.query.sessionName);
if (session != false) {
if (session.status != 'isLogged') {
if (req.query.image) {
session.qrcode = session.qrcode.replace('data:image/png;base64,', '');
const imageBuffer = Buffer.from(session.qrcode, 'base64');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': imageBuffer.length
});
res.end(imageBuffer);
} else {
res.status(200).json({ result: "success", message: session.state, qrcode: session.qrcode });
}
} else {
res.status(200).json({ result: "error", message: session.state });
}
} else {
res.status(200).json({ result: "error", message: "NOTFOUNDS" });
}
});//qrcode
app.get("/send", async (req, res, next) => {
var result = await Sessions.sendText(
req.query.sessionName,
req.query.number,
req.query.text
);
//console.log(req.query);
res.json(result);
});//sendText
app.post("/sendMensangem", async function sendText(req, res, next) {
console.log(req.body);
var result = await Sessions.sendText(
req.body.sessionName,
req.body.number,
req.body.text
);
//console.log(req.body);
res.json(result);
});//sendText
app.post("/enviarmk", async function sendText(req, res, next) {
console.log(req.body);
var sessao = 'session1';
var celular = req.body.celular.replace(/[^\d]+/g,'');
console.log(celular);
var result = await Sessions.sendText(
sessao,
celular,
req.body.mensagem
);
//console.log(req.body);
res.json(result);
});//sendText
app.post("/sendText", async (req, res, next) => {
console.log(req);
var result = await Sessions.sendText(
req.body.sessionName,
req.body.number,
req.body.text
);
res.json(result);
});//sendText
app.post("/sendFile", async (req, res, next) => {
//console.log(req.body);
console.log(req.body.number);
var result = await Sessions.sendFile(
req.body.sessionName,
req.body.number,
req.body.base64Data,
req.body.fileName,
req.body.caption
);
res.json(result);
});//sendFile
app.get("/close", async (req, res, next) => {
var result = await Sessions.closeSession(req.query.sessionName);
res.json(result);
});//close
process.stdin.resume();//so the program will not close instantly
async function exitHandler(options, exitCode) {
if (options.cleanup) {
console.log('cleanup');
await Sessions.getSessions().forEach(async session => {
await Sessions.closeSession(session.sessionName);
});
}
if (exitCode || exitCode === 0) {
console.log(exitCode);
}
if (options.exit) {
process.exit();
}
} //exitHandler
//do something when app is closing
process.on('exit', exitHandler.bind(null, { cleanup: true }));
//catches ctrl+c event
process.on('SIGINT', exitHandler.bind(null, { exit: true }));
// catches "kill pid" (for example: nodemon restart)
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }));
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }));
//catches uncaught exceptions
process.on('uncaughtException', exitHandler.bind(null, { exit: true }));