diff --git a/Makefile b/Makefile index 9b38bbcb..b5a68dd4 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ build: docker build . -t allburov/whatsapp-http-api run: - docker run -it -v `pwd`/tokens:/app/tokens -p 127.0.0.1:3000:3000/tcp allburov/whatsapp-http-api + docker run --rm -v `pwd`/tokens:/app/tokens -p 127.0.0.1:3000:3000/tcp --name whatsapp-http-api allburov/whatsapp-http-api push: docker push allburov/whatsapp-http-api diff --git a/package.json b/package.json index 088bdc8e..1af7b62b 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "start": "nest start", "start:dev": "nest start --watch", "start:debug": "nest start --debug --watch", - "start:prod": "node dist/main", + "start:prod": "node --unhandled-rejections=none dist/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "test": "jest", "test:watch": "jest --watch", diff --git a/src/whatsapp.service.ts b/src/whatsapp.service.ts index 93770624..a45554bf 100644 --- a/src/whatsapp.service.ts +++ b/src/whatsapp.service.ts @@ -27,10 +27,11 @@ const HOOKS = [ ] const ENV_PREFIX = "WHATSAPP_HOOK_" -enum WhatsappStatus { +export enum WhatsappStatus { STARTING = "STARTING", SCAN_QR_CODE = "SCAN_QR_CODE", WORKING = "WORKING", + FAILED = "FAILED", } export class WhatsappService { @@ -58,27 +59,35 @@ export class WhatsappService { } public async start() { - this.whatsapp = await create('sessionName', - (base64Qrimg, asciiQR, attempts, urlCode) => { - this.saveQRCode(base64Qrimg) - this.status = WhatsappStatus.SCAN_QR_CODE - console.log('Number of attempts to read the qrcode: ', attempts); - console.log('Terminal qrcode: ', asciiQR); - }, - undefined, - { - headless: true, - devtools: false, - useChrome: true, - debug: false, - logQR: true, - browserArgs: ["--no-sandbox"], - autoClose: 60000, - createPathFileToken: true, - puppeteerOptions: {}, - multidevice: false, - } - ) + try { + this.whatsapp = await create('sessionName', + (base64Qrimg, asciiQR, attempts, urlCode) => { + this.saveQRCode(base64Qrimg) + this.status = WhatsappStatus.SCAN_QR_CODE + console.log('Number of attempts to read the qrcode: ', attempts); + console.log('Terminal qrcode: ', asciiQR); + }, + undefined, + { + headless: true, + devtools: false, + useChrome: true, + debug: false, + logQR: true, + browserArgs: ["--no-sandbox"], + autoClose: 60000, + createPathFileToken: true, + puppeteerOptions: {}, + multidevice: false, + } + ) + } catch (error) { + this.status = WhatsappStatus.FAILED + this.log.error(error) + this.saveQRCode("") + return + } + this.saveQRCode("") this.configureWebhooks(); this.status = WhatsappStatus.WORKING @@ -239,7 +248,7 @@ export class WhatsappSessionManager implements OnApplicationShutdown { async stopSession(name: string) { this.log.log(`Stopping ${name} session...`) const service = this.getService(name) - await service.getWhatsapp().close() + await service.whatsapp.close() this.log.log(`"${name}" has been stopped.`) delete this.sessions[name] }