diff --git a/README.md b/README.md index cdaaa59e1..c02d35160 100644 --- a/README.md +++ b/README.md @@ -105,7 +105,7 @@ Installing the current repository "you can download the beta version from the cu > npm i github:orkestral/venom ``` -## Getting started Multidevice and Normal +## Getting started ```javascript // Supports ES6 @@ -115,7 +115,6 @@ const venom = require('venom-bot'); venom .create({ session: 'session-name', //name of session - multidevice: true // for version not multidevice use false.(default: true) }) .then((client) => start(client)) .catch((erro) => { @@ -183,7 +182,6 @@ venom }, // options { - multidevice: false, // for version not multidevice use false.(default: true) folderNameToken: 'tokens', //folder name when saving tokens mkdirFolderToken: '', //folder directory tokens, just inside the venom folder, example: { mkdirFolderToken: '/node_modules', } //will save the tokens folder in the node_modules directory headless: true, // Headless chrome @@ -268,9 +266,6 @@ venom //Create session wss return "serverClose" case server for close console.log('Session name: ', session); }, - { - multidevice: false // for version not multidevice use false.(default: true) - } ) .then((client) => { start(client); diff --git a/src/api/layers/retriever.layer.ts b/src/api/layers/retriever.layer.ts index e19ab1c2e..665140246 100644 --- a/src/api/layers/retriever.layer.ts +++ b/src/api/layers/retriever.layer.ts @@ -1,6 +1,5 @@ import { Page, Browser } from 'puppeteer'; import { CreateConfig } from '../../config/create-config'; -import { tokenSession } from '../../config/tokenSession.config'; import { WhatsappProfile } from '../model'; import { SenderLayer } from './sender.layer'; import { Scope, checkValuesSender } from '../helpers/layers-interface'; @@ -93,20 +92,6 @@ export class RetrieverLayer extends SenderLayer { public async getStateConnection() { return await this.page.evaluate(() => WAPI.getStateConnection()); } - /** - * Returns browser session token - * @returns obj [token] - */ - public async getSessionTokenBrowser( - removePath?: boolean - ): Promise { - if (removePath === true) { - await this.page.evaluate(() => { - window['pathSession'] = true; - }); - } - return await this.page.evaluate(() => WAPI.getSessionTokenBrowser()); - } /** * Receive the current theme diff --git a/src/config/tokenSession.config.ts b/src/config/tokenSession.config.ts deleted file mode 100644 index 2573d9033..000000000 --- a/src/config/tokenSession.config.ts +++ /dev/null @@ -1,13 +0,0 @@ -export interface tokenSession { - WABrowserId: string; - WAToken1: string; - WAToken2: string; - WASecretBundle: string; -} - -export const defaultTokenSession: tokenSession = { - WABrowserId: '', - WASecretBundle: '', - WAToken1: '', - WAToken2: '' -}; diff --git a/src/controllers/auth.ts b/src/controllers/auth.ts index 5d77d7d02..a218ddcc2 100644 --- a/src/controllers/auth.ts +++ b/src/controllers/auth.ts @@ -1,13 +1,7 @@ -import * as fs from 'fs'; import * as path from 'path'; import * as puppeteer from 'puppeteer'; import * as qrcode from 'qrcode-terminal'; -import { existsSync, readFileSync } from 'fs'; -import { CreateConfig } from '../config/create-config'; import { ScrapQrcode } from '../api/model/qrcode'; -import { tokenSession } from '../config/tokenSession.config'; -//import { InterfaceMode } from '../api/model/enum/interface-mode'; -//import { injectApi } from './browser'; import { sleep } from '../utils/sleep'; import { Whatsapp } from '../api/whatsapp'; const QRCode = require('qrcode'); @@ -161,130 +155,6 @@ export async function retrieveQR( .catch(() => undefined); } -export function SessionTokenCkeck(token: object) { - if ( - token && - token['WABrowserId'] && - token['WASecretBundle'] && - token['WAToken1'] && - token['WAToken2'] - ) { - return true; - } else { - return false; - } -} - -export async function auth_InjectToken( - page: puppeteer.Page, - session: string, - options: CreateConfig, - token?: tokenSession -) { - if (!token) { - const pathToken: string = path.join( - path.resolve( - process.cwd() + options.mkdirFolderToken, - options.folderNameToken - ), - `${session}.data.json` - ); - if (existsSync(pathToken)) { - token = JSON.parse(readFileSync(pathToken).toString()); - } - } - - if (!token || !SessionTokenCkeck(token)) { - return false; - } - - await page - .evaluate((session) => { - localStorage.clear(); - Object.keys(session).forEach((key) => { - localStorage.setItem(key, session[key]); - }); - }, token as any) - .catch(); -} - -export async function isClassic(page: puppeteer.Page): Promise { - return await page - .evaluate(() => { - if ( - window.localStorage.getItem('WASecretBundle') && - window.localStorage.getItem('WAToken1') && - window.localStorage.getItem('WAToken2') - ) { - return true; - } - return false; - }) - .catch(() => undefined); -} - -export async function saveToken( - page: puppeteer.Page, - session: string, - options: CreateConfig -) { - const token = (await page - .evaluate(() => { - if (window.localStorage) { - return { - WABrowserId: window.localStorage.getItem('WABrowserId'), - WASecretBundle: window.localStorage.getItem('WASecretBundle'), - WAToken1: window.localStorage.getItem('WAToken1'), - WAToken2: window.localStorage.getItem('WAToken2') - }; - } - return undefined; - }) - .catch(() => undefined)) as tokenSession; - - if (!token || !SessionTokenCkeck(token)) { - return false; - } - - const folder = path.join( - path.resolve( - process.cwd(), - options.mkdirFolderToken, - options.folderNameToken - ) - ); - - try { - fs.mkdirSync(folder, { recursive: true }); - } catch (error) { - throw 'Failed to create folder tokens...'; - } - - try { - fs.writeFileSync( - path.join(folder, `${session}.data.json`), - JSON.stringify(token) - ); - //fs.chmodSync(folder, '777'); - //fs.chmodSync(folder + '/' + session + '.data.json', '777'); - } catch (error) { - throw 'Failed to save token...'; - } - - return token; -} - -// export async function resetStore(page: puppeteer.Page) { -// await page -// .evaluate(() => { -// let last = window['webpackChunkwhatsapp_web_client'].length - 1; -// window['webpackChunkwhatsapp_web_client'][last][0] = []; -// window.Store = undefined; -// window.WAPI = undefined; -// }) -// .catch(() => {}); -// } - export async function checkDisconnect(page: puppeteer.Page, wpp: Whatsapp) { while (true) { const erroBrowser = await page diff --git a/src/controllers/browser.ts b/src/controllers/browser.ts index 23e9068dd..2d5e87731 100644 --- a/src/controllers/browser.ts +++ b/src/controllers/browser.ts @@ -6,24 +6,19 @@ import puppeteer from 'puppeteer-extra'; import { CreateConfig } from '../config/create-config'; import { puppeteerConfig } from '../config/puppeteer.config'; import StealthPlugin = require('puppeteer-extra-plugin-stealth'); -import { auth_InjectToken } from './auth'; import { useragentOverride } from '../config/WAuserAgente'; -import { tokenSession } from '../config/tokenSession.config'; import { sleep } from '../utils/sleep'; import * as Spinnies from 'spinnies'; import * as os from 'os'; import * as rimraf from 'rimraf'; export async function initWhatsapp( - session: string, options: CreateConfig, - browser: Browser, - token?: tokenSession + browser: Browser ): Promise { const waPage: Page = await getWhatsappPage(browser); if (waPage != null) { try { await waPage.setUserAgent(useragentOverride); - //await waPage.setCacheEnabled(true); if ( typeof options.userPass === 'string' && options.userPass.length && @@ -52,8 +47,6 @@ export async function initWhatsapp( }); await browser.userAgent(); - // Auth with token - await auth_InjectToken(waPage, session, options, token); return waPage; } catch (e) { @@ -115,7 +108,6 @@ export function folderSession( process.cwd(), options.mkdirFolderToken, options.folderNameToken, - options.mkdirFolderTokenMultidevice, session ) ); @@ -232,9 +224,7 @@ export async function initBrowser( } } - if (options.multidevice === true) { - folderSession(options, session); - } + folderSession(options, session); // Use stealth plugin to avoid being detected as a bot puppeteer.use(StealthPlugin()); diff --git a/src/controllers/initializer.ts b/src/controllers/initializer.ts index ebe8893cc..1655d3e1c 100644 --- a/src/controllers/initializer.ts +++ b/src/controllers/initializer.ts @@ -1,7 +1,5 @@ -import { readFileSync } from 'fs'; import { Whatsapp } from '../api/whatsapp'; import { CreateConfig, defaultOptions } from '../config/create-config'; -import { SessionTokenCkeck, isClassic } from './auth'; import { initWhatsapp, initBrowser, statusLog } from './browser'; import { welcomeScreen } from './welcome'; import { getSpinnies } from '../utils/spinnies'; @@ -13,7 +11,6 @@ import { } from '../api/model/enum'; import { InterfaceChangeMode } from '../api/model'; import { checkingCloses } from '../api/helpers'; -import { tokenSession } from '../config/tokenSession.config'; import { Browser, Page } from 'puppeteer'; import { checkUpdates } from './check-up-to-date'; @@ -103,10 +100,6 @@ export interface CreateOptions extends CreateConfig { * A callback will be received, informing the customer's status */ statusFind?: StatusFind; - /** - * Pass the session token information you can receive this token with the await client.getSessionTokenBrowser () function - */ - browserSessionToken?: tokenSession; /** * A callback will be received, informing user about browser and page instance */ @@ -126,7 +119,6 @@ export async function create(createOption: CreateOptions): Promise; * Start the bot * You must pass a string type parameter, this parameter will be the name of the client's session. If the parameter is not passed, the section name will be "session". * @returns Whatsapp page, with this parameter you will be able to access the bot functions - * @deprecated depreciated due to Multidevices {@link browserSessionToken} */ export async function create( @@ -134,21 +126,16 @@ export async function create( catchQR?: CatchQR, statusFind?: StatusFind, options?: CreateConfig, - browserSessionToken?: tokenSession, browserInstance?: BrowserInstance, reconnectQrcode?: ReconnectQrcode, interfaceChange?: interfaceChange ): Promise; -/** - * @deprecated depreciated due to Multidevices {@link browserSessionToken} - */ export async function create( sessionOrOption: string | CreateOptions, catchQR?: CatchQR, statusFind?: StatusFind, options?: CreateConfig, - browserSessionToken?: tokenSession, browserInstance?: BrowserInstance, reconnectQrcode?: ReconnectQrcode, interfaceChange?: interfaceChange @@ -164,8 +151,6 @@ export async function create( session = sessionOrOption.session || session; catchQR = sessionOrOption.catchQR || catchQR; statusFind = sessionOrOption.statusFind || statusFind; - browserSessionToken = - sessionOrOption.browserSessionToken || browserSessionToken; browserInstance = sessionOrOption.browserInstance || browserInstance; options = sessionOrOption; } @@ -261,22 +246,13 @@ export async function create( return reject('The client has been closed'); }); - if (SessionTokenCkeck(browserSessionToken)) { - browserToken = browserSessionToken; - } - spinnies.add(`whatzapp-${session}`, { text: 'Checking page to whatzapp...' }); statusFind && statusFind('initWhatsapp', session); // Initialize whatsapp - const page: false | Page = await initWhatsapp( - session, - mergedOptions, - browser, - browserToken - ); + const page: false | Page = await initWhatsapp(mergedOptions, browser); if (page === false) { spinnies.fail(`whatzapp-${session}`, { @@ -309,36 +285,16 @@ export async function create( statusFind && statusFind('introductionHistory', session, event); }); - // if (mergedOptions.forceConnect) { - // loadForceConnect( - // page, - // async (event) => { - // if (typeof event === 'boolean' && event === true) { - // statusFind && statusFind('executedLoadForce', session); - // } else { - // statusFind && statusFind('infoLoadForce', session, event); - // } - // }, - // mergedOptions.attemptsForceConnectLoad, - // mergedOptions.forceConnectTime - // ).catch(); - // } - const client = new Whatsapp(browser, page, session, mergedOptions); - //client.initialize(); - if (browserInstance) { browserInstance(browser, page, client); } - // checkStore(page, client); - client.onInterfaceChange(async (interFace: InterfaceChangeMode) => { try { if (interFace.mode === InterfaceMode.MAIN) { interfaceChange && interfaceChange('chatsAvailable', session); - //checkDisconnect(page, client); spinnies.add(`whatzapp-mode-main-${session}`, { text: 'opening main page...' }); @@ -427,20 +383,11 @@ export async function create( } catch {} } - // if (mergedOptions.createPathFileToken) { - // if (stateStream === SocketStream.CONNECTED) { - // saveToken(page, session, mergedOptions); - // } - // } - if (stateStream === SocketStream.DISCONNECTED) { const mode = await page .evaluate(() => window?.Store?.Stream?.mode) .catch(() => {}); - if ( - mode === InterfaceMode.QR - // && checkFileJson(mergedOptions, session) - ) { + if (mode === InterfaceMode.QR) { if (statusFind) { spinnies.add(`whatzapp-qr-${session}`, { text: 'check....' @@ -450,7 +397,6 @@ export async function create( text: 'Disconnected by cell phone!' }); } - //deleteFiles(mergedOptions, session, spinnies); } } }) @@ -459,28 +405,12 @@ export async function create( client .onStateChange(async (state) => { if (state === SocketState.PAIRING) { - const device: Boolean = await page - .evaluate(() => { - if ( - document.querySelector('[tabindex="-1"]') && - window?.Store?.Stream?.mode === InterfaceMode.SYNCING && - window?.Store?.Stream?.obscurity === 'SHOW' - ) { - return true; - } - return false; - }) - .catch(() => undefined); - if (device === true) { - const ckeckVersion = await isClassic(page); - if (ckeckVersion === false) { - await page.evaluate(async () => { - await window?.Store?.Login?.triggerCriticalSyncLogout(); - }); - } - if (statusFind) { - statusFind('deviceNotConnected', session); - } + await page.evaluate(async () => { + await window?.Store?.Login?.triggerCriticalSyncLogout(); + }); + + if (statusFind) { + statusFind('deviceNotConnected', session); } } }) @@ -527,69 +457,17 @@ export async function create( .catch(); } - if (mergedOptions.debug) { - const debugURL = `http://localhost:${readFileSync( - `./${session}/DevToolsActivePort` - ).slice(0, -54)}`; - console.log(`\nDebug: \x1b[34m${debugURL}\x1b[0m`); - } - statusFind && statusFind('waitChat', session); await page .waitForSelector('#app .two', { visible: true }) .catch(() => {}); - await page - .waitForFunction( - () => { - if (mergedOptions.debug) { - console.log(`\nDebug: Loading wp app....`); - } - const StoreKey = Object.keys(window).find( - (k) => - !!Object.getOwnPropertyDescriptor(window[k], 'WidFactory') && - !!Object.getOwnPropertyDescriptor( - window[k].WidFactory, - 'createWid' - ) - ); - if (StoreKey) { - window.Store = window[StoreKey]; - return true; - } - return false; - }, - { - timeout: 0, - polling: 100 - } - ) - .catch(() => {}); - - try { - spinnies.succeed(`whatzapp-intro-${session}`, { - text: 'Successfully connected!' - }); - } catch {} - await client.initService(); await client.addChatWapi(); - // resetStore(page); - //await sleep(2000); statusFind && statusFind('successChat', session); - // const atualizar = await page.waitForSelector('span[data-testid="chat-butterbar"]').catch(() => { }); - // if (atualizar) { - - // await page.evaluate(() => { - // window.updater.restart() - // }); - // await page - // .waitForSelector('#app .two', { visible: true }) - // .catch(() => { }); - // } return resolve(client); } }); diff --git a/src/types/WAPI.d.ts b/src/types/WAPI.d.ts index 61cb265c3..e0df1cc11 100644 --- a/src/types/WAPI.d.ts +++ b/src/types/WAPI.d.ts @@ -3,8 +3,6 @@ import { Contact, ContactStatus, GroupCreation, - HostDevice, - Id, Message, PartialMessage, SendFileResult, @@ -12,7 +10,6 @@ import { SendStickerResult, WhatsappProfile } from '../api/model'; -import { tokenSession } from '../config/tokenSession.config'; interface WAPI { addParticipant: (groupId: string, contactId: string | string[]) => boolean; @@ -74,7 +71,6 @@ interface WAPI { getMessageById: (messageId: string) => Promise; getNumberProfile: (contactId: string) => Object; getProfilePicFromServer: (chatId: string) => string; - getSessionTokenBrowser: (removePath?: boolean) => tokenSession; getStatus: (contactId: string) => ContactStatus; getTheme: () => string; getUnreadMessages: (unread: boolean) => any; diff --git a/test/index.js b/test/index.js index 1ccb2b98c..c6d097a55 100644 --- a/test/index.js +++ b/test/index.js @@ -3,15 +3,26 @@ const { create } = require('../dist'); (async () => { create({ session: 'session-name', //name of session - multidevice: true, - headless: false, - logQR: true, - useChrome: true + multidevice: true // for version not multidevice use false.(default: true) }) - .then((client) => start(client)) - .catch((erro) => { - console.error(erro); - }); - function start(client) { } + .then((client) => start(client)) + .catch((erro) => { + console.log(erro); + }); + +function start(client) { + client.onMessage((message) => { + if (message.body === 'Hi' && message.isGroupMsg === false) { + client + .sendText(message.from, 'Welcome Venom 🕷') + .then((result) => { + console.log('Result: ', result); //return object success + }) + .catch((erro) => { + console.error('Error when sending: ', erro); //return object error + }); + } + }); +} })(); \ No newline at end of file