import { Client , ClientOptions , ClientSession , MessageMedia , } from 'whatsapp-web.js' ; import { existsSync , writeFile , unlink } from 'fs' ; import { resolve } from 'path' ; import { toString } from 'qrcode' ; const sessionFile = resolve ( 'src' , 'data' , 'session.json' ) ; const session = existsSync ( sessionFile ) && require ( sessionFile ) ; const optionsClient = { puppeteer : { headless : true , args : [ '--no-sandbox' ] , } , session , } ; const client = new Client ( optionsClient as ClientOptions ) ; client . on ( 'qr' , async ( qr ) => { const qrCodeTerminal = await toString ( qr , { width : 21 , margin : 2 , scale : 4 , type : 'utf8' , } ) ; console . log ( qrCodeTerminal ) ; } ) ; client . on ( 'authenticated' , ( dataSession ) => { writeFile ( sessionFile , JSON . stringify ( dataSession ) , ( err ) => { if ( err ) console . log ( err ) ; } ) ; } ) ; client . on ( 'ready' , async ( ) => { console . log ( 'WhatsApp bot successfully connected!' ) ; await client . sendMessage ( '5511963928063@c.us' , `[${ client . info . pushname }] - WhatsApp Online\n\n[x] Please, like project: https://github.com/caioagiani/whatsapp-bot` ) ; } ) ; client . on ( 'auth_failure' , ( ) => { unlink ( sessionFile , ( ) => { console . log ( 'Authentication failed, try again.' ) ; process . exit ( 1 ) ; } ) ; } ) ; client . on ( 'disconnected' , ( ) => { unlink ( sessionFile , ( ) => console . log ( 'WhatsApp session lost connection, try again.' ) ) ; } ) ; client . initialize ( ) ; export { client , MessageMedia } ;