Skip to content

Commit

Permalink
Create index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
narlles authored Mar 29, 2021
1 parent b805fc1 commit ab26b66
Showing 1 changed file with 161 additions and 0 deletions.
161 changes: 161 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,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 }));

0 comments on commit ab26b66

Please sign in to comment.