1
1
import { Client , MessageMedia } from 'whatsapp-web.js' ;
2
- import qrcode from 'qrcode-terminal' ;
3
- import fs from 'fs' ;
4
- import dir from 'path' ;
2
+ import { generate } from 'qrcode-terminal' ;
3
+ import { existsSync , writeFile , unlink } from 'fs' ;
4
+ import { resolve } from 'path' ;
5
5
6
6
interface INotification {
7
7
reply : ( args : string ) => void ;
8
8
recipientIds : any [ ] ;
9
9
}
10
10
11
- const sessionFile = dir . resolve ( 'src' , 'data' , 'session.json' ) ;
11
+ const sessionFile = resolve ( 'src' , 'data' , 'session.json' ) ;
12
12
13
- const session = fs . existsSync ( sessionFile ) ? require ( sessionFile ) : null ;
13
+ const session = existsSync ( sessionFile ) ? require ( sessionFile ) : null ;
14
14
15
15
const client = new Client ( {
16
16
puppeteer : {
17
17
headless : true ,
18
18
args : [ '--no-sandbox' ] ,
19
19
} ,
20
20
session,
21
- } ) ;
21
+ } as any ) ;
22
22
23
23
client . on ( 'qr' , ( qr : string ) => {
24
- qrcode . generate ( qr , { small : true } ) ;
24
+ generate ( qr , { small : true } ) ;
25
25
} ) ;
26
26
27
27
client . on ( 'authenticated' , ( session : any ) => {
28
- fs . writeFile ( sessionFile , JSON . stringify ( session ) , ( err ) => {
28
+ writeFile ( sessionFile , JSON . stringify ( session ) , ( err ) => {
29
29
if ( err ) console . log ( err ) ;
30
30
} ) ;
31
31
} ) ;
@@ -35,19 +35,22 @@ client.on('ready', async () => {
35
35
36
36
const { pushname } = client . info ;
37
37
38
- client . sendMessage ( '5511987454933@c.us' , `[${ pushname } ] - WhatsApp Online` ) ;
38
+ client . sendMessage (
39
+ '5511963928063@c.us' ,
40
+ `[${ pushname } ] - WhatsApp Online\n\n[x] Star on project: https://github.com/caioagiani/whatsapp-bot` ,
41
+ ) ;
39
42
} ) ;
40
43
41
44
client . on ( 'auth_failure' , ( ) => {
42
- fs . unlink ( sessionFile , ( ) => {
45
+ unlink ( sessionFile , ( ) => {
43
46
console . log ( 'Autenticação falhou, tente novamente.' ) ;
44
47
process . exit ( 1 ) ;
45
48
} ) ;
46
49
} ) ;
47
50
48
51
client . on ( 'disconnected' , ( ) => {
49
- fs . unlink ( sessionFile , ( ) =>
50
- console . log ( 'Sessão WhatsApp perdeu a conexão, tente novamente.' )
52
+ unlink ( sessionFile , ( ) =>
53
+ console . log ( 'Sessão WhatsApp perdeu a conexão, tente novamente.' ) ,
51
54
) ;
52
55
} ) ;
53
56
0 commit comments