Skip to content

Commit

Permalink
Merge pull request #55 from mayder/master
Browse files Browse the repository at this point in the history
inclusão de funções
  • Loading branch information
billbarsch authored Apr 1, 2021
2 parents 17f8586 + 8d82ebd commit 910ac67
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 27 deletions.
113 changes: 88 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ require('dotenv').config();
var app = express();

app.use(cors());
// app.use(timeout(120000));
// app.use(haltOnTimedout);
app.use(express.json({
limit: '100mb',
limit: '20mb',
extended: true
}));

Expand Down Expand Up @@ -53,6 +55,14 @@ app.get("/start", async (req, res, next) => {
}
});//start

app.get("/status", async (req, res, next) => {
var session = await Sessions.getStatus(req.query.sessionName);
console.log(session);
res.status(200).json({
result: (!session.state) ? 'NOT_FOUND' : session.state
});
}); //status

app.get("/qrcode", async (req, res, next) => {
console.log("qrcode..." + req.query.sessionName);
var session = Sessions.getSession(req.query.sessionName);
Expand Down Expand Up @@ -88,6 +98,11 @@ app.post("/sendText", async function sendText(req, res, next) {
res.json(result);
});//sendText

app.post("/sendTextToStorie", async (req, res, next) => {
var result = await Sessions.sendTextToStorie(req);
res.json(result);
}); //sendTextToStorie

app.post("/sendFile", async (req, res, next) => {
var result = await Sessions.sendFile(
req.body.sessionName,
Expand All @@ -99,6 +114,16 @@ app.post("/sendFile", async (req, res, next) => {
res.json(result);
});//sendFile

app.post("/sendImageStorie", async (req, res, next) => {
var result = await Sessions.sendImageStorie(
req.body.sessionName,
req.body.base64Data,
req.body.fileName,
req.body.caption
);
res.json(result);
}); //sendImageStorie

app.post("/sendLink", async (req, res, next) => {
var result = await Sessions.sendLinkPreview(
req.body.sessionName,
Expand All @@ -119,38 +144,76 @@ app.post("/sendContactVcard", async (req, res, next) => {
res.json(result);
}); //sendContactVcard

app.post("/getAllChatsNewMsg", async (req, res, next) => {
app.post("/sendVoice", async (req, res, next) => {
var result = await Sessions.sendVoice(
req.body.sessionName,
req.body.number,
req.body.voice
);
res.json(result);
}); //sendVoice

app.post("/sendLocation", async (req, res, next) => {
var result = await Sessions.sendLocation(
req.body.sessionName,
req.body.number,
req.body.lat,
req.body.long,
req.body.local
);
res.json(result);
}); //sendLocation

app.get("/getAllChatsNewMsg", async (req, res, next) => {
var result = await Sessions.getAllChatsNewMsg(req.body.sessionName);
res.json(result);
}); //sendContactVcard
}); //getAllChatsNewMsg

app.post("/getAllUnreadMessages", async (req, res, next) => {
app.get("/getAllUnreadMessages", async (req, res, next) => {
var result = await Sessions.getAllUnreadMessages(req.body.sessionName);
res.json(result);
}); //getAllUnreadMessages

app.get("/checkNumberStatus", async (req, res, next) => {
var result = await Sessions.checkNumberStatus(
req.body.sessionName,
req.body.number
);
res.json(result);
}); //Verifica Numero

app.get("/getNumberProfile", async (req, res, next) => {
var result = await Sessions.getNumberProfile(
req.body.sessionName,
req.body.number
);
res.json(result);
}); //Verifica perfil

app.get("/close", async (req, res, next) => {
if (Sessions.options.jsonbinio_secret_key !== undefined) {//se informou secret key pra salvar na nuvem
console.log("limpando token na nuvem...");
//salva dados do token da sessão na nuvem
var data = JSON.stringify({ "nada": "nada" });
var config = {
method: 'put',
url: 'https://api.jsonbin.io/b/' + Sessions.options.jsonbinio_bin_id,
headers: {
'Content-Type': 'application/json',
'secret-key': Sessions.options.jsonbinio_secret_key,
'versioning': 'false'
},
data: data
};
await axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
if (typeof(Sessions.options) != "undefined") {
if (Sessions.options.jsonbinio_secret_key !== undefined) {//se informou secret key pra salvar na nuvem
console.log("limpando token na nuvem...");
//salva dados do token da sessão na nuvem
var data = JSON.stringify({ "nada": "nada" });
var config = {
method: 'put',
url: 'https://api.jsonbin.io/b/' + Sessions.options.jsonbinio_bin_id,
headers: {
'Content-Type': 'application/json',
'secret-key': Sessions.options.jsonbinio_secret_key,
'versioning': 'false'
},
data: data
};
await axios(config)
.then(function (response) {
console.log(JSON.stringify(response.data));
})
.catch(function (error) {
console.log(error);
});
}
}
var result = await Sessions.closeSession(req.query.sessionName);
res.json(result);
Expand Down
167 changes: 165 additions & 2 deletions sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ module.exports = class Sessions {
return session;
} //start

static async getStatus(sessionName, options = []) {
Sessions.options = Sessions.options || options;
Sessions.sessions = Sessions.sessions || [];

var session = Sessions.getSession(sessionName);
return session;
} //getStatus

static async addSesssion(sessionName) {
var newSession = {
name: sessionName,
Expand Down Expand Up @@ -133,6 +141,7 @@ module.exports = class Sessions {
);
var browserSessionToken = await client.getSessionTokenBrowser();
console.log("usou isso no create: " + JSON.stringify(browserSessionToken));
session.state = "CONNECTED";
return client;
} //initSession
if (process.env.ENGINE === 'WPPCONNECT') {
Expand All @@ -147,7 +156,6 @@ module.exports = class Sessions {
statusFind: (statusSession, session) => {
console.log('- Status da sessão:', statusSession);
console.log('- Session name: ', session);

},
folderNameToken: 'tokens',
headless: true,
Expand Down Expand Up @@ -194,6 +202,7 @@ module.exports = class Sessions {

});
wppconnect.defaultLogger.level = 'silly'
session.state = "CONNECTED";
return client;
}
}
Expand Down Expand Up @@ -292,7 +301,6 @@ module.exports = class Sessions {
return foundSession;
} //getSession


static getSessions() {
if (Sessions.sessions) {
return Sessions.sessions;
Expand Down Expand Up @@ -347,6 +355,35 @@ module.exports = class Sessions {
}
} //message

static async sendTextToStorie(req) {
var params = {
sessionName: req.body.sessionName,
text: req.body.text
}
var session = Sessions.getSession(params.sessionName);
if (session) {
if (session.state == "CONNECTED") {
await session.client.then(async client => {
console.log('#### send msg =', params);
return await client.sendText('status@broadcast', params.text);
});
return {
result: "success"
}
} else {
return {
result: "error",
message: session.state
};
}
} else {
return {
result: "error",
message: "NOTFOUND"
};
}
} //message to storie

static async sendFile(sessionName, number, base64Data, fileName, caption) {
var session = Sessions.getSession(sessionName);
if (session) {
Expand All @@ -367,6 +404,34 @@ module.exports = class Sessions {
}
} //message

static async sendImageStorie(sessionName, base64Data, fileName, caption) {
var session = Sessions.getSession(sessionName);
if (session) {
if (session.state == "CONNECTED") {
var resultSendFile = await session.client.then(async (client) => {
var folderName = fs.mkdtempSync(path.join(os.tmpdir(), session.name + '-'));
var filePath = path.join(folderName, fileName);
fs.writeFileSync(filePath, base64Data, 'base64');
console.log(filePath);
return await client.sendFile('status@broadcast', filePath, fileName, caption);
}); //client.then(
return {
result: "success"
};
} else {
return {
result: "error",
message: session.state
};
}
} else {
return {
result: "error",
message: "NOTFOUND"
};
}
} //sendImageStorie

static async saveHook(req) {
var sessionName = req.body.sessionName;
/**
Expand Down Expand Up @@ -417,6 +482,54 @@ module.exports = class Sessions {
}
} //vcard

static async sendVoice(sessionName, number, voice) {
var session = Sessions.getSession(sessionName);
if (session) {
if (session.state == "CONNECTED") {
var resultSendVoice = await session.client.then(async (client) => {
return await client.sendVoiceBase64(number + '@c.us', voice);
}); //client.then(
return {
result: "success"
};
} else {
return {
result: "error",
message: session.state
};
}
} else {
return {
result: "error",
message: "NOTFOUND"
};
}
} //voice

static async sendLocation(sessionName, number, lat, long, local) {
var session = Sessions.getSession(sessionName);
if (session) {
if (session.state == "CONNECTED") {
var resultSendLocation = await session.client.then(async (client) => {
return await client.sendLocation(number + '@c.us', lat, long, local);
}); //client.then(
return {
result: "success"
};
} else {
return {
result: "error",
message: session.state
};
}
} else {
return {
result: "error",
message: "NOTFOUND"
};
}
} //location

static async sendLinkPreview(sessionName, number, url, caption) {
var session = Sessions.getSession(sessionName);
if (session) {
Expand Down Expand Up @@ -488,4 +601,54 @@ module.exports = class Sessions {
};
}
} //getAllUnreadMessages

static async checkNumberStatus(sessionName, number) {
var session = Sessions.getSession(sessionName);
//console.log(sessionName+number);
if (session) {
if (session.state == "CONNECTED") {
var resultcheckNumberStatus = await session.client.then(async (client) => {
return await client.checkNumberStatus(number + '@c.us');
});
return {
result: resultcheckNumberStatus
};
} else {
return {
result: "error",
message: session.state
};
}
} else {
return {
result: "error",
message: "NOTFOUND"
};
}
} //saber se o número é válido

static async getNumberProfile(sessionName, number) {
var session = Sessions.getSession(sessionName);
//console.log(sessionName+number);
if (session) {
if (session.state == "CONNECTED") {
var resultgetNumberProfile = await session.client.then(async (client) => {
return await client.getNumberProfile(number + '@c.us');
});
return {
result: resultgetNumberProfile
};
} else {
return {
result: "error",
message: session.state
};
}
} else {
return {
result: "error",
message: "NOTFOUND"
};
}
} //receber o perfil do usuário
}

0 comments on commit 910ac67

Please sign in to comment.