How to use sessions.getSession method in Cypress

Best JavaScript code snippet using cypress

sessions.js

Source:sessions.js Github

copy

Full Screen

1// person.js2'use strict';3const os = require('os');4const fs = require('fs');5const path = require('path');6const wppconnect = require('@wppconnect-team/wppconnect');7const axios = require('axios');8module.exports = class Sessions {9 static async start(sessionName, options = []) {10 Sessions.options = Sessions.options || options; //start object11 Sessions.sessions = Sessions.sessions || []; //start array12 var session = Sessions.getSession(sessionName);13 if (session == false) { //create new session14 console.log("session == false");15 session = await Sessions.addSesssion(sessionName);16 } else if (["CLOSED"].includes(session.state)) { //restart session17 console.log("session.state == CLOSED");18 session.state = "STARTING";19 session.status = 'notLogged';20 session.client = Sessions.initSession(sessionName);21 Sessions.setup(sessionName);22 } else if (["CONFLICT", "UNPAIRED", "UNLAUNCHED"].includes(session.state)) {23 console.log("client.useHere()");24 session.client.then(client => {25 client.useHere();26 });27 } else {28 console.log("session.state: " + session.state);29 }30 return session;31 } //start32 static async getStatus(sessionName, options = []) {33 Sessions.options = Sessions.options || options;34 Sessions.sessions = Sessions.sessions || [];35 var session = Sessions.getSession(sessionName);36 return session;37 } //getStatus38 static async addSesssion(sessionName) {39 var newSession = {40 name: sessionName,41 hook: null,42 qrcode: false,43 client: false,44 status: 'notLogged',45 state: 'STARTING'46 }47 Sessions.sessions.push(newSession);48 console.log("newSession.state: " + newSession.state);49 //setup session50 newSession.client = Sessions.initSession(sessionName);51 Sessions.setup(sessionName);52 return newSession;53 } //addSession54 static async initSession(sessionName) {55 var session = Sessions.getSession(sessionName);56 session.browserSessionToken = null;57 if (process.env.ENGINE === 'WPPCONNECT') {58 const client = await wppconnect.create({59 session: session.name,60 catchQR: (base64Qrimg, asciiQR, attempts, urlCode) => {61 session.state = "QRCODE";62 session.qrcode = base64Qrimg;63 session.CodeasciiQR = asciiQR;64 session.CodeurlCode = urlCode;65 },66 statusFind: (statusSession, session) => {67 console.log('- Status da sessão:', statusSession);68 console.log('- Session name: ', session);69 },70 folderNameToken: 'tokens',71 headless: true,72 devtools: false,73 useChrome: true,74 debug: false,75 logQR: true,76 browserArgs: [77 '--log-level=3',78 '--no-default-browser-check',79 '--disable-site-isolation-trials',80 '--no-experiments',81 '--ignore-gpu-blacklist',82 '--ignore-certificate-errors',83 '--ignore-certificate-errors-spki-list',84 '--disable-gpu',85 '--disable-extensions',86 '--disable-default-apps',87 '--enable-features=NetworkService',88 '--disable-setuid-sandbox',89 '--no-sandbox',90 // Extras91 '--disable-webgl',92 '--disable-threaded-animation',93 '--disable-threaded-scrolling',94 '--disable-in-process-stack-traces',95 '--disable-histogram-customizer',96 '--disable-gl-extensions',97 '--disable-composited-antialiasing',98 '--disable-canvas-aa',99 '--disable-3d-apis',100 '--disable-accelerated-2d-canvas',101 '--disable-accelerated-jpeg-decoding',102 '--disable-accelerated-mjpeg-decode',103 '--disable-app-list-dismiss-on-blur',104 '--disable-accelerated-video-decode',105 //outros106 '--hide-scrollbars',107 '--mute-audio',108 '--disable-infobars',109 '--disable-2d-canvas-clip-aa',110 '--disable-gl-drawing-for-tests',111 ],112 disableSpins: true,113 disableWelcome: false,114 updatesLog: true,115 autoClose: 90000,116 deviceName: 'SoftMED Technologies',117 createPathFileToken: true,118 waitForLogin: true,119 });120 wppconnect.defaultLogger.level = 'silly'121 session.state = "CONNECTED";122 return client;123 }124 }125 static async setup(sessionName) {126 var session = Sessions.getSession(sessionName);127 await session.client.then(client => {128 client.onStateChange(state => {129 session.state = state;130 if (state == "CONNECTED") {131 }//if CONNECTED132 console.log("session.state: " + state);133 }); //.then((client) => Sessions.startProcess(client));134 client.onMessage(async (message) => {135 var session = Sessions.getSession(sessionName);136 if (session.hook != null) {137 var config = {138 method: 'post',139 url: session.hook,140 headers: {141 'Content-Type': 'application/json'142 },143 data: message144 };145 await axios(config)146 .then(function (response) {147 console.log(JSON.stringify(response.data));148 })149 .catch(function (error) {150 console.log(error);151 });152 } else if (message.body == "TESTEBOT") {153 client.sendText(message.from, 'Hello\nfriend!');154 }155 });156 });157 } //setup158 static async LogoutSession(sessionName) {159 var session = Sessions.getSession(sessionName);160 if (session) { //só adiciona se não existir161 if (session.state != "LOGOUT") {162 if (session.client)163 await session.client.then(async client => {164 try {165 await client.logout();166 } catch (error) {167 console.log("client.logout(): " + error.message);168 }169 session.state = "LOGOUT";170 session.client = false;171 console.log("client.logout - session.state: " + session.state);172 });173 return { result: "success", message: "LOGOUT" };174 } else { //close175 return { result: "success", message: session.state };176 }177 } else {178 return { result: "error", message: "NOTFOUND" };179 }180 } //logout181 static async closeSession(sessionName) {182 var session = Sessions.getSession(sessionName);183 if (session) { //só adiciona se não existir184 if (session.state != "CLOSED") {185 if (session.client)186 await session.client.then(async client => {187 try {188 await client.close();189 } catch (error) {190 console.log("client.close(): " + error.message);191 }192 session.state = "CLOSED";193 session.client = false;194 console.log("client.close - session.state: " + session.state);195 });196 return { result: "success", message: "CLOSED" };197 } else { //close198 return { result: "success", message: session.state };199 }200 } else {201 return { result: "error", message: "NOTFOUND" };202 }203 } //close204 static getSession(sessionName) {205 var foundSession = false;206 if (Sessions.sessions)207 Sessions.sessions.forEach(session => {208 if (sessionName == session.name) {209 foundSession = session;210 }211 });212 return foundSession;213 } //getSession214 static getSessions() {215 if (Sessions.sessions) {216 return Sessions.sessions;217 } else {218 return [];219 }220 } //getSessions221 static async getQrcode(sessionName) {222 var session = Sessions.getSession(sessionName);223 if (session) {224 //if (["UNPAIRED", "UNPAIRED_IDLE"].includes(session.state)) {225 if (["UNPAIRED_IDLE"].includes(session.state)) {226 //restart session227 await Sessions.closeSession(sessionName);228 Sessions.start(sessionName);229 return { result: "error", message: session.state };230 } else if (["CLOSED"].includes(session.state)) {231 Sessions.start(sessionName);232 return { result: "error", message: session.state };233 } else { //CONNECTED234 if (session.status != 'isLogged') {235 return { result: "success", message: session.state, qrcode: session.qrcode };236 } else {237 return { result: "success", message: session.state };238 }239 }240 } else {241 return { result: "error", message: "NOTFOUND" };242 }243 } //getQrcode244 static async archiveChat(req) {245 var params = {246 sessionName: req.body.sessionName,247 number: req.body.number248 }249 var session = Sessions.getSession(params.sessionName);250 if (session) {251 if (session.state == "CONNECTED") {252 await session.client.then(async client => {253 if (!isNaN(params.number)) {254 console.log('#### delete chat =', params);255 return await client.archiveChat(params.number + '@c.us');256 }257 });258 return { result: "success" }259 } else {260 return { result: "error", message: session.state };261 }262 } else {263 return { result: "error", message: "NOTFOUND" };264 }265 } //message266 static async deleteChat(req) {267 var params = {268 sessionName: req.body.sessionName,269 number: req.body.number270 }271 var session = Sessions.getSession(params.sessionName);272 if (session) {273 if (session.state == "CONNECTED") {274 await session.client.then(async client => {275 if (!isNaN(params.number)) {276 console.log('#### delete chat =', params);277 return await client.deleteChat(params.number + '@c.us');278 }279 });280 return { result: "success" }281 } else {282 return { result: "error", message: session.state };283 }284 } else {285 return { result: "error", message: "NOTFOUND" };286 }287 } //message288 static async sendListMessage(req) {289 var params = {290 sessionName: req.body.sessionName,291 number: req.body.number292 }293 var session = Sessions.getSession(params.sessionName);294 if (session) {295 if (session.state == "CONNECTED") {296 await session.client.then(async client => {297 if (!isNaN(params.number)) {298 console.log('#### send msg =', params);299 return await client.sendListMessage(params.number + '@c.us', {300 "buttonText": 'Click here',301 "description": 'Choose one option',302 "sections": [303 {304 "title": 'Section 1',305 "rows": [306 {307 "rowId": 'my_custom_id',308 "title": 'Test 1',309 "description": 'Description 1',310 },311 {312 "rowId": '2',313 "title": 'Test 2',314 "description": 'Description 2',315 },316 ],317 },318 ],319 });320 }321 });322 return { result: "success" }323 } else {324 return { result: "error", message: session.state };325 }326 } else {327 return { result: "error", message: "NOTFOUND" };328 }329 } //message330 static async sendText(req) {331 var params = {332 sessionName: req.body.sessionName,333 number: req.body.number,334 text: req.body.text335 }336 var session = Sessions.getSession(params.sessionName);337 if (session) {338 if (session.state == "CONNECTED") {339 await session.client.then(async client => {340 if (!isNaN(params.number)) {341 console.log('#### send msg =', params);342 return await client.sendText(params.number + '@c.us', params.text);343 }344 });345 return { result: "success" }346 } else {347 return { result: "error", message: session.state };348 }349 } else {350 return { result: "error", message: "NOTFOUND" };351 }352 } //message353 static async sendTextToStorie(req) {354 var params = {355 sessionName: req.body.sessionName,356 text: req.body.text357 }358 var session = Sessions.getSession(params.sessionName);359 if (session) {360 if (session.state == "CONNECTED") {361 await session.client.then(async client => {362 console.log('#### send msg =', params);363 return await client.sendText('status@broadcast', params.text);364 });365 return {366 result: "success"367 }368 } else {369 return {370 result: "error",371 message: session.state372 };373 }374 } else {375 return {376 result: "error",377 message: "NOTFOUND"378 };379 }380 } //message to storie381 static async sendFile(sessionName, number, base64Data, fileName, caption) {382 var session = Sessions.getSession(sessionName);383 if (session) {384 if (session.state == "CONNECTED") {385 var resultSendFile = await session.client.then(async (client) => {386 var folderName = fs.mkdtempSync(path.join(os.tmpdir(), session.name + '-'));387 var filePath = path.join(folderName, fileName);388 fs.writeFileSync(filePath, base64Data, 'base64');389 console.log(filePath);390 return await client.sendFile(number + '@c.us', filePath, fileName, caption);391 }); //client.then(392 return { result: "success" };393 } else {394 return { result: "error", message: session.state };395 }396 } else {397 return { result: "error", message: "NOTFOUND" };398 }399 } //message400 static async sendImageStorie(sessionName, base64Data, fileName, caption) {401 var session = Sessions.getSession(sessionName);402 if (session) {403 if (session.state == "CONNECTED") {404 var resultSendFile = await session.client.then(async (client) => {405 var folderName = fs.mkdtempSync(path.join(os.tmpdir(), session.name + '-'));406 var filePath = path.join(folderName, fileName);407 fs.writeFileSync(filePath, base64Data, 'base64');408 console.log(filePath);409 return await client.sendFile('status@broadcast', filePath, fileName, caption);410 }); //client.then(411 return {412 result: "success"413 };414 } else {415 return {416 result: "error",417 message: session.state418 };419 }420 } else {421 return {422 result: "error",423 message: "NOTFOUND"424 };425 }426 } //sendImageStorie427 static async saveHook(req) {428 var sessionName = req.body.sessionName;429 /**430 * Verifica se encontra sessão431 */432 var foundSession = false;433 var foundSessionId = null;434 if (Sessions.sessions)435 Sessions.sessions.forEach((session, id) => {436 if (sessionName == session.name) {437 foundSession = session;438 foundSessionId = id;439 }440 });441 // Se não encontrar retorna erro442 if (!foundSession) {443 return { result: "error", message: 'Session not found' };444 } else {445 // Se encontrar cria variáveis446 var hook = req.body.hook;447 foundSession.hook = hook;448 Sessions.sessions[foundSessionId] = foundSession;449 return { result: "success", message: 'Hook Atualizado' };450 }451 }452 static async sendContactVcard(sessionName, number, numberCard, nameCard) {453 var session = Sessions.getSession(sessionName);454 if (session) {455 if (session.state == "CONNECTED") {456 var resultSendContactVcard = await session.client.then(async (client) => {457 return await client.sendContactVcard(number + '@c.us', numberCard + '@c.us', nameCard);458 }); //client.then(459 return {460 result: "success"461 };462 } else {463 return {464 result: "error",465 message: session.state466 };467 }468 } else {469 return {470 result: "error",471 message: "NOTFOUND"472 };473 }474 } //vcard475 static async sendVoice(sessionName, number, voice) {476 var session = Sessions.getSession(sessionName);477 if (session) {478 if (session.state == "CONNECTED") {479 var resultSendVoice = await session.client.then(async (client) => {480 return await client.sendVoiceBase64(number + '@c.us', voice);481 }); //client.then(482 return {483 result: "success"484 };485 } else {486 return {487 result: "error",488 message: session.state489 };490 }491 } else {492 return {493 result: "error",494 message: "NOTFOUND"495 };496 }497 } //voice498 static async sendLocation(sessionName, number, lat, long, local) {499 var session = Sessions.getSession(sessionName);500 if (session) {501 if (session.state == "CONNECTED") {502 var resultSendLocation = await session.client.then(async (client) => {503 return await client.sendLocation(number + '@c.us', lat, long, local);504 }); //client.then(505 return {506 result: "success"507 };508 } else {509 return {510 result: "error",511 message: session.state512 };513 }514 } else {515 return {516 result: "error",517 message: "NOTFOUND"518 };519 }520 } //location521 static async sendLinkPreview(sessionName, number, url, caption) {522 var session = Sessions.getSession(sessionName);523 if (session) {524 if (session.state == "CONNECTED") {525 var resultSendLinkPreview = await session.client.then(async (client) => {526 return await client.sendLinkPreview(number + '@c.us', url, caption);527 }); //client.then(528 return {529 result: "success"530 };531 } else {532 return {533 result: "error",534 message: session.state535 };536 }537 } else {538 return {539 result: "error",540 message: "NOTFOUND"541 };542 }543 } //link544 static async clearChat(sessionName) {545 var session = Sessions.getSession(sessionName);546 if (session) {547 if (session.state == "CONNECTED") {548 var resultclearChat = await session.client.then(async (client) => {549 return client.clearChat();550 });551 return {552 result: resultclearChat553 };554 } else {555 return {556 result: "error",557 message: session.state558 };559 }560 } else {561 return {562 result: "error",563 message: "NOTFOUND"564 };565 }566 } //clearChat567 static async isConnected(sessionName) {568 var session = Sessions.getSession(sessionName);569 if (session) {570 if (session.state == "CONNECTED") {571 var resultisConnected = await session.client.then(async (client) => {572 return client.isConnected();573 });574 return {575 result: resultisConnected576 };577 } else {578 return {579 result: "error",580 message: session.state581 };582 }583 } else {584 return {585 result: "error",586 message: "NOTFOUND"587 };588 }589 } //isConnected590 static async getHostDevice(sessionName) {591 var session = Sessions.getSession(sessionName);592 if (session) {593 if (session.state == "CONNECTED") {594 var resultgetHostDevice = await session.client.then(async (client) => {595 return client.getHostDevice();596 });597 return {598 result: resultgetHostDevice599 };600 } else {601 return {602 result: "error",603 message: session.state604 };605 }606 } else {607 return {608 result: "error",609 message: "NOTFOUND"610 };611 }612 } //getHostDevice613 static async getBatteryLevel(sessionName) {614 var session = Sessions.getSession(sessionName);615 if (session) {616 if (session.state == "CONNECTED") {617 var resultgetBatteryLevel = await session.client.then(async (client) => {618 return client.getBatteryLevel();619 });620 return {621 result: resultgetBatteryLevel622 };623 } else {624 return {625 result: "error",626 message: session.state627 };628 }629 } else {630 return {631 result: "error",632 message: "NOTFOUND"633 };634 }635 } //getBatteryLevel636 static async getAllChats(sessionName) {637 var session = Sessions.getSession(sessionName);638 if (session) {639 if (session.state == "CONNECTED") {640 var resultGetAllChats = await session.client.then(async (client) => {641 return client.getAllChats();642 });643 return {644 result: resultGetAllChats645 };646 } else {647 return {648 result: "error",649 message: session.state650 };651 }652 } else {653 return {654 result: "error",655 message: "NOTFOUND"656 };657 }658 } //getAllChats659 static async getAllChatsNewMsg(sessionName) {660 var session = Sessions.getSession(sessionName);661 if (session) {662 if (session.state == "CONNECTED") {663 var resultGetAllChatsNewMsg = await session.client.then(async (client) => {664 return client.getAllChatsNewMsg();665 });666 return {667 result: resultGetAllChatsNewMsg668 };669 } else {670 return {671 result: "error",672 message: session.state673 };674 }675 } else {676 return {677 result: "error",678 message: "NOTFOUND"679 };680 }681 } //getAllChatsNewMsg682 static async getAllUnreadMessages(sessionName) {683 var session = Sessions.getSession(sessionName);684 if (session) {685 if (session.state == "CONNECTED") {686 var resultGetAllUnreadMessages = await session.client.then(async (client) => {687 return await client.getAllUnreadMessages();688 });689 return {690 result: resultGetAllUnreadMessages691 };692 } else {693 return {694 result: "error",695 message: session.state696 };697 }698 } else {699 return {700 result: "error",701 message: "NOTFOUND"702 };703 }704 } //getAllUnreadMessages705 static async getAllMessagesInChat(sessionName, number) {706 var session = Sessions.getSession(sessionName);707 //console.log(sessionName+number);708 if (session) {709 if (session.state == "CONNECTED") {710 var resultgetAllMessagesInChat = await session.client.then(async (client) => {711 return await client.getAllMessagesInChat(number + '@c.us');712 });713 return {714 result: resultgetAllMessagesInChat715 };716 } else {717 return {718 result: "error",719 message: session.state720 };721 }722 } else {723 return {724 result: "error",725 message: "NOTFOUND"726 };727 }728 } //saber se o número é válido729 static async checkNumberStatus(sessionName, number) {730 var session = Sessions.getSession(sessionName);731 //console.log(sessionName+number);732 if (session) {733 if (session.state == "CONNECTED") {734 var resultcheckNumberStatus = await session.client.then(async (client) => {735 return await client.checkNumberStatus(number + '@c.us');736 });737 return {738 result: resultcheckNumberStatus739 };740 } else {741 return {742 result: "error",743 message: session.state744 };745 }746 } else {747 return {748 result: "error",749 message: "NOTFOUND"750 };751 }752 } //saber se o número é válido753 static async getMessages(sessionName, number) {754 var session = Sessions.getSession(sessionName);755 //console.log(sessionName+number);756 if (session) {757 if (session.state == "CONNECTED") {758 var resultgetMessages = await session.client.then(async (client) => {759 return await client.getMessages(number + '@c.us');760 });761 return {762 result: resultgetMessages763 };764 } else {765 return {766 result: "error",767 message: session.state768 };769 }770 } else {771 return {772 result: "error",773 message: "NOTFOUND"774 };775 }776 } //receber o perfil do usuário777 static async getNumberProfile(sessionName, number) {778 var session = Sessions.getSession(sessionName);779 //console.log(sessionName+number);780 if (session) {781 if (session.state == "CONNECTED") {782 var resultgetNumberProfile = await session.client.then(async (client) => {783 return await client.getNumberProfile(number + '@c.us');784 });785 return {786 result: resultgetNumberProfile787 };788 } else {789 return {790 result: "error",791 message: session.state792 };793 }794 } else {795 return {796 result: "error",797 message: "NOTFOUND"798 };799 }800 } //receber o perfil do usuário...

Full Screen

Full Screen

commands.js

Source:commands.js Github

copy

Full Screen

1/*2 * @Author: Eduardo Policarpo3 * @contact: +55 439966114374 * @Date: 2021-05-10 18:09:495 * @LastEditTime: 2021-06-07 03:18:016 */7import Sessions from '../../controllers/sessions.js';8import moment from 'moment';9moment().format('DD-MM-YYYY hh:mm:ss');10moment.locale('pt-br')11export default class Commands {12 static async getBatteryLevel(req, res) {13 try {14 let data = Sessions.getSession(req.body.session)15 let response = await data.client.getBatteryLevel()16 return res.status(200).json({17 "result": 200,18 "messages": "SUCCESS",19 "batterylevel": response20 })21 } catch (error) {22 return res.status(400).json({23 "result": 400,24 "status": "FAIL",25 "error": error26 })27 }28 }29 static async getConnectionState(req, res) {30 try {31 let data = Sessions.getSession(req.body.session)32 let response = await data.client.getConnectionState()33 return res.status(200).json({34 "result": 200,35 "status": response36 })37 } catch (error) {38 return res.status(400).json({39 "result": 400,40 "status": "FAIL",41 "error": error42 })43 }44 }45 static async getHostDevice(req, res) {46 try {47 let data = Sessions.getSession(req.body.session)48 let response = await data.client.getHostDevice()49 console.log(response)50 return res.status(200).json({51 "result": 200,52 "number": response.wid.user,53 "connected": response.connected,54 "phone": response.phone,55 "plataform": response.plataform,56 "locales": response.locales,57 "batery": response.batery,58 "pushname": response.pushname59 })60 } catch (error) {61 return res.status(400).json({62 "result": 400,63 "status": "FAIL",64 "error": error65 })66 }67 }68 static async getAllContacts(req, res) {69 try {70 let data = Sessions.getSession(req.body.session)71 let response = await data.client.getAllContacts()72 let contacts = response.map(function (data) {73 return {74 'name': data.name ? data.name : '',75 'realName': data.pushname ? data.pushname : '',76 'formattedName': data.formattedName ? data.formattedName : '',77 'phone': data.id.user,78 'business': data.isBusiness,79 'verifiedName': data.verifiedName ? data.verifiedName : '',80 'isMyContact': data.isMyContact81 }82 })83 return res.status(200).json({84 "result": 200,85 "messages": "SUCCESS",86 "contacts": contacts87 })88 } catch (error) {89 return res.status(400).json({90 "result": 400,91 "status": "FAIL",92 "error": error93 })94 }95 }96 static async getAllChats(req, res) {97 try {98 let data = Sessions.getSession(req.body.session)99 let response = await data.client.getAllChats()100 return res.status(200).json({101 "result": 200,102 "messages": "SUCCESS",103 "contacts": response104 })105 } catch (error) {106 return res.status(400).json({107 "result": 400,108 "status": "FAIL",109 "error": error110 })111 }112 }113 static async getAllChatsWithMessages(req, res) {114 try {115 let data = Sessions.getSession(req.body.session)116 let response = await data.client.getAllChatsWithMessages()117 return res.status(200).json({118 "result": 200,119 "messages": "SUCCESS",120 "contacts": response121 })122 } catch (error) {123 return res.status(400).json({124 "result": 400,125 "status": "FAIL",126 "error": error127 })128 }129 }130 static async getAllNewMessages(req, res) {131 try {132 let data = Sessions.getSession(req.body.session)133 let response = await data.client.getAllNewMessages()134 return res.status(200).json({135 "result": 200,136 "messages": "SUCCESS",137 "contacts": response138 })139 } catch (error) {140 return res.status(400).json({141 "result": 400,142 "status": "FAIL",143 "error": error144 })145 }146 }147 static async getAllUnreadMessages(req, res) {148 try {149 let data = Sessions.getSession(req.body.session)150 let response = await data.client.getAllUnreadMessages()151 return res.status(200).json({152 "result": 200,153 "messages": "SUCCESS",154 "contacts": response155 })156 } catch (error) {157 return res.status(400).json({158 "result": 400,159 "status": "FAIL",160 "error": error161 })162 }163 }164 static async getBlockList(req, res) {165 try {166 let data = Sessions.getSession(req.body.session)167 let response = await data.client.getBlockList()168 let blkcontacts = response.map(function (data) {169 return {170 'phone': data ? data.split('@')[0] : '',171 }172 })173 return res.status(200).json({174 "result": 200,175 "messages": "SUCCESS",176 "contacts": blkcontacts177 })178 } catch (error) {179 return res.status(400).json({180 "result": 400,181 "status": "FAIL",182 "error": error183 })184 }185 }186 static async getMessagesChat(req, res) {187 let data = Sessions.getSession(req.body.session)188 let number = req.body.number + '@c.us';189 try {190 let response = await data.client.loadAndGetAllMessagesInChat(number, true)191 let messages = response.map(function (data) {192 console.log(data)193 return {194 "type": data.type,195 "author": data.verifiedName,196 "from": data.from,197 "to": data.to,198 "mensagem": data.body,199 "enviada em": moment.unix(data.t).format('DD-MM-YYYY hh:mm:ss')200 }201 })202 return res.status(200).json({203 "result": 200,204 "data": messages205 })206 } catch (error) {207 return res.status(400).json({208 "result": 400,209 "status": "FAIL",210 "error": error211 })212 }213 }214 static async getProfilePic(req, res) {215 let data = Sessions.getSession(req.body.session)216 let number = req.body.number + '@c.us';217 try {218 let response = await data.client.getProfilePicFromServer(number)219 return res.status(200).json({220 "result": 200,221 "messages": "SUCCESS",222 "pic_profile": response223 })224 } catch (error) {225 return res.status(400).json({226 "result": 400,227 "status": "FAIL",228 "error": error229 })230 }231 }232 static async verifyNumber(req, res) {233 let data = Sessions.getSession(req.body.session)234 try {235 let number = req.body.number + '@c.us';236 let profile = await data.client.getNumberProfile(number)237 if (profile.numberExists) {238 return res.status(200).json({239 "result": 200,240 "messages": "SUCCESS",241 "profile": profile242 })243 }244 } catch (error) {245 return res.status(400).json({246 "result": 400,247 "status": "FAIL",248 "profile": error249 })250 }251 }252 static async deleteChat(req, res) {253 let data = Sessions.getSession(req.body.session)254 let number = req.body.number + '@c.us';255 try {256 await data.client.deleteChat(number);257 return res.status(200).json({258 "result": 200,259 "messages": "SUCCESS"260 })261 } catch (error) {262 return res.status(400).json({263 "result": 400,264 "status": "FAIL",265 "error": error266 })267 }268 }269 static async clearChat(req, res) {270 let data = Sessions.getSession(req.body.session)271 let number = req.body.number + '@c.us';272 try {273 await data.client.clearChatMessages(number);274 return res.status(200).json({275 "result": 200,276 "messages": "SUCCESS"277 })278 } catch (error) {279 return res.status(400).json({280 "result": 400,281 "status": "FAIL",282 "error": error283 })284 }285 }286 static async archiveChat(req, res) {287 let data = Sessions.getSession(req.body.session)288 let number = req.body.number + '@c.us';289 try {290 await data.client.archiveChat(number, true);291 return res.status(200).json({292 "result": 200,293 "messages": "SUCCESS"294 })295 } catch (error) {296 return res.status(400).json({297 "result": 400,298 "status": "FAIL",299 "error": error300 })301 }302 }303 static async deleteMessage(req, res) {304 let data = Sessions.getSession(req.body.session)305 let number = req.body.number + '@c.us';306 if (!req.body.messageid) {307 return res.status(400).json({308 status: 400,309 error: "MessageID não foi informada, é obrigatorio"310 })311 }312 else {313 try {314 await data.client.deleteMessage(number, [req.body.messageid], true);315 return res.status(200).json({316 "result": 200,317 "messages": "SUCCESS"318 })319 } catch (error) {320 return res.status(400).json({321 "result": 400,322 "status": "FAIL",323 "error": error324 })325 }326 }327 }328 static async markUnseenMessage(req, res) {329 let data = Sessions.getSession(req.body.session)330 let number = req.body.number + '@c.us';331 try {332 await data.client.markUnseenMessage(number);333 return res.status(200).json({334 "result": 200,335 "messages": "SUCCESS"336 })337 } catch (error) {338 return res.status(400).json({339 "result": 400,340 "status": "FAIL"341 })342 }343 }344 static async blockContact(req, res) {345 let data = Sessions.getSession(req.body.session)346 let number = req.body.number + '@c.us';347 try {348 await data.client.blockContact(number);349 return res.status(200).json({350 "result": 200,351 "messages": "SUCCESS"352 })353 } catch (error) {354 return res.status(400).json({355 "result": 400,356 "status": "FAIL"357 })358 }359 }360 static async unblockContact(req, res) {361 let data = Sessions.getSession(req.body.session)362 let number = req.body.number + '@c.us';363 try {364 await data.client.unblockContact(number);365 return res.status(200).json({366 "result": 200,367 "messages": "SUCCESS"368 })369 } catch (error) {370 return res.status(400).json({371 "result": 400,372 "status": "FAIL",373 "error": error374 })375 }376 }377 static async getNumberProfile(req, res) {378 let data = Sessions.getSession(req.body.session)379 let number = req.body.number + '@c.us';380 try {381 const response = await data.client.getNumberProfile(number);382 return res.status(200).json({383 "result": 200,384 "messages": "SUCCESS",385 "phone": response.id.user,386 "isBusiness": response.isBusiness387 })388 } catch (error) {389 return res.status(400).json({390 "result": 400,391 "status": "FAIL",392 "error": error393 })394 }395 }...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1Cypress.Commands.add('getSession', () => {2 return cy.request({3 })4})5Cypress.Commands.add('getSession', () => {6 return cy.request({7 })8})9describe('Test', () => {10 it('should get session', () => {11 cy.getSession().then((response) => {12 expect(response.status).to.eq(200)13 })14 })15})16describe('Test', () => {17 it('should get session', () => {18 cy.getSession().then((response) => {19 expect(response.status).to.eq(200)20 })21 })22})23describe('Test', () => {24 it('should get session', () => {25 cy.getSession().then((response) => {26 expect(response.status).to.eq(200)27 })28 })29})30describe('Test', () => {31 it('should get session', () => {32 cy.getSession().then((response) => {33 expect(response.status).to.eq(200)34 })35 })36})37describe('Test', () => {38 it('should get session', () => {39 cy.getSession().then((response) => {40 expect(response.status).to.eq(200)41 })42 })43})44describe('Test', () => {45 it('should get session', () => {46 cy.getSession().then((response) => {47 expect(response.status).to.eq(200)48 })

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Test', () => {2 it('test', () => {3 cy.getSession().then((session) => {4 cy.request({5 headers: {6 },7 body: {

Full Screen

Using AI Code Generation

copy

Full Screen

1describe('Session', () => {2 it('should set session', () => {3 cy.setSession({4 session: {5 }6 })7 })8 it('should get session', () => {9 cy.getSession().then(session => {10 expect(session.foo).to.equal('bar')11 })12 })13})14Cypress.Commands.add('setSession', options => {15 Cypress.log({16 })17 cy.window()18 .its('sessionStorage')19 .invoke('setItem', 'session', JSON.stringify(options.session))20})21Cypress.Commands.add('getSession', () => {22 Cypress.log({23 })24 return cy.window().then(win => {25 return JSON.parse(win.sessionStorage.getItem('session'))26 })27})

Full Screen

Using AI Code Generation

copy

Full Screen

1const getSession = require('cypress-session-tokens')2describe('My First Test', function() {3 it('Gets, types and asserts', function() {4 cy.contains('type').click()5 cy.url().should('include', '/commands/actions')6 cy.get('.action-email')7 .type('

Full Screen

Using AI Code Generation

copy

Full Screen

1"scripts": {2 }3"scripts": {4 }5"scripts": {6 }7"scripts": {8 }9"scripts": {

Full Screen

Using AI Code Generation

copy

Full Screen

1var session = Cypress.sessions.getSession()2Cypress.sessions.createSession(session, (newSession) => {3 onBeforeLoad: (win) => {4 win.sessionStorage.clear()5 win.sessionStorage.setItem('session_id', newSession)6 }7 })8})9var session = Cypress.sessions.getSession()10Cypress.sessions.createSession(session, (newSession) => {11 onBeforeLoad: (win) => {12 win.sessionStorage.clear()13 win.sessionStorage.setItem('session_id', newSession)14 }15 })16})17var session = Cypress.sessions.getSession()18Cypress.sessions.createSession(session, (newSession) => {19 onBeforeLoad: (win) => {20 win.sessionStorage.clear()21 win.sessionStorage.setItem('session_id', newSession)22 }23 })24})

Full Screen

Cypress Tutorial

Cypress is a renowned Javascript-based open-source, easy-to-use end-to-end testing framework primarily used for testing web applications. Cypress is a relatively new player in the automation testing space and has been gaining much traction lately, as evidenced by the number of Forks (2.7K) and Stars (42.1K) for the project. LambdaTest’s Cypress Tutorial covers step-by-step guides that will help you learn from the basics till you run automation tests on LambdaTest.

Chapters:

  1. What is Cypress? -
  2. Why Cypress? - Learn why Cypress might be a good choice for testing your web applications.
  3. Features of Cypress Testing - Learn about features that make Cypress a powerful and flexible tool for testing web applications.
  4. Cypress Drawbacks - Although Cypress has many strengths, it has a few limitations that you should be aware of.
  5. Cypress Architecture - Learn more about Cypress architecture and how it is designed to be run directly in the browser, i.e., it does not have any additional servers.
  6. Browsers Supported by Cypress - Cypress is built on top of the Electron browser, supporting all modern web browsers. Learn browsers that support Cypress.
  7. Selenium vs Cypress: A Detailed Comparison - Compare and explore some key differences in terms of their design and features.
  8. Cypress Learning: Best Practices - Take a deep dive into some of the best practices you should use to avoid anti-patterns in your automation tests.
  9. How To Run Cypress Tests on LambdaTest? - Set up a LambdaTest account, and now you are all set to learn how to run Cypress tests.

Certification

You can elevate your expertise with end-to-end testing using the Cypress automation framework and stay one step ahead in your career by earning a Cypress certification. Check out our Cypress 101 Certification.

YouTube

Watch this 3 hours of complete tutorial to learn the basics of Cypress and various Cypress commands with the Cypress testing at LambdaTest.

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful