@wppconnect-team/wppconnect 1.35.1 → 1.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,11 @@
1
+ ## 1.36.0 (2025-03-21)
2
+
3
+ - feat: Added client.onMessageEdit event (close #2092) ([874f8c4](https://github.com/wppconnect-team/wppconnect/commit/874f8c4)), closes [#2092](https://github.com/wppconnect-team/wppconnect/issues/2092)
4
+
5
+ ## <small>1.35.2 (2024-12-20)</small>
6
+
7
+ - fix(deps): update dependency @wppconnect/wa-js to ^3.16.0 (#2425) ([28384fd](https://github.com/wppconnect-team/wppconnect/commit/28384fd)), closes [#2425](https://github.com/wppconnect-team/wppconnect/issues/2425)
8
+
1
9
  ## <small>1.35.1 (2024-10-30)</small>
2
10
 
3
11
  - fix: Upgrade whatsappVersion to 2.3000.10173x ([2b609f5](https://github.com/wppconnect-team/wppconnect/commit/2b609f5))
@@ -1,5 +1,6 @@
1
1
  import { ResponseType } from 'axios';
2
- export declare const makeOptions: (useragentOverride: string) => {
2
+ import { Transform } from 'stream';
3
+ export declare const makeOptions: (useragentOverride: string, responseType?: ResponseType) => {
3
4
  responseType: ResponseType;
4
5
  headers: {
5
6
  'User-Agent': string;
@@ -18,4 +19,5 @@ export declare const mediaTypes: {
18
19
  DOCUMENT: string;
19
20
  STICKER: string;
20
21
  };
21
- export declare const magix: (fileData: any, mediaKeyBase64: any, mediaType: any, expectedSize?: number) => Buffer;
22
+ export declare const magix: (fileData: any, mediaKeyBase64: any, mediaType: any, expectedSize?: number) => Buffer<ArrayBufferLike>;
23
+ export declare const newMagix: (mediaKeyBase64: string, mediaType: string, expectedSize: number) => Transform;
@@ -31,31 +31,45 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
31
31
  }) : function(o, v) {
32
32
  o["default"] = v;
33
33
  });
34
- var __importStar = (this && this.__importStar) || function (mod) {
35
- if (mod && mod.__esModule) return mod;
36
- var result = {};
37
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
- __setModuleDefault(result, mod);
39
- return result;
40
- };
34
+ var __importStar = (this && this.__importStar) || (function () {
35
+ var ownKeys = function(o) {
36
+ ownKeys = Object.getOwnPropertyNames || function (o) {
37
+ var ar = [];
38
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
39
+ return ar;
40
+ };
41
+ return ownKeys(o);
42
+ };
43
+ return function (mod) {
44
+ if (mod && mod.__esModule) return mod;
45
+ var result = {};
46
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
47
+ __setModuleDefault(result, mod);
48
+ return result;
49
+ };
50
+ })();
41
51
  var __importDefault = (this && this.__importDefault) || function (mod) {
42
52
  return (mod && mod.__esModule) ? mod : { "default": mod };
43
53
  };
44
54
  Object.defineProperty(exports, "__esModule", { value: true });
45
- exports.magix = exports.mediaTypes = exports.timeout = exports.makeOptions = void 0;
55
+ exports.newMagix = exports.magix = exports.mediaTypes = exports.timeout = exports.makeOptions = void 0;
46
56
  var crypto = __importStar(require("crypto"));
47
57
  var futoin_hkdf_1 = __importDefault(require("futoin-hkdf"));
48
- var atob = require("atob");
49
- var makeOptions = function (useragentOverride) { return ({
50
- responseType: 'arraybuffer',
51
- headers: {
52
- 'User-Agent': processUA(useragentOverride),
53
- DNT: '1',
54
- 'Upgrade-Insecure-Requests': '1',
55
- origin: 'https://web.whatsapp.com/',
56
- referer: 'https://web.whatsapp.com/',
57
- },
58
- }); };
58
+ var atob_1 = __importDefault(require("atob"));
59
+ var stream_1 = require("stream");
60
+ var makeOptions = function (useragentOverride, responseType) {
61
+ if (responseType === void 0) { responseType = 'arraybuffer'; }
62
+ return ({
63
+ responseType: responseType,
64
+ headers: {
65
+ 'User-Agent': processUA(useragentOverride),
66
+ DNT: '1',
67
+ 'Upgrade-Insecure-Requests': '1',
68
+ origin: 'https://web.whatsapp.com/',
69
+ referer: 'https://web.whatsapp.com/',
70
+ },
71
+ });
72
+ };
59
73
  exports.makeOptions = makeOptions;
60
74
  var timeout = function (ms) {
61
75
  return new Promise(function (res) { return setTimeout(res, ms); });
@@ -123,10 +137,53 @@ var hexToBytes = function (hexStr) {
123
137
  return new Uint8Array(intArray);
124
138
  };
125
139
  var base64ToBytes = function (base64Str) {
126
- var binaryStr = atob(base64Str);
140
+ var binaryStr = (0, atob_1.default)(base64Str);
127
141
  var byteArray = new Uint8Array(binaryStr.length);
128
142
  for (var i = 0; i < binaryStr.length; i++) {
129
143
  byteArray[i] = binaryStr.charCodeAt(i);
130
144
  }
131
145
  return byteArray;
132
146
  };
147
+ var newMagix = function (mediaKeyBase64, mediaType, expectedSize) {
148
+ var mediaKeyBytes = newBase64ToBytes(mediaKeyBase64);
149
+ var info = "WhatsApp ".concat(exports.mediaTypes[mediaType.toUpperCase()], " Keys");
150
+ var hash = 'sha256';
151
+ var salt = Buffer.alloc(32);
152
+ var expandedSize = 112;
153
+ var mediaKeyExpanded = (0, futoin_hkdf_1.default)(mediaKeyBytes, expandedSize, {
154
+ salt: salt,
155
+ info: info,
156
+ hash: hash,
157
+ });
158
+ var iv = mediaKeyExpanded.slice(0, 16);
159
+ var cipherKey = mediaKeyExpanded.slice(16, 48);
160
+ var decipher = crypto.createDecipheriv('aes-256-cbc', cipherKey, iv);
161
+ var processedBytes = 0;
162
+ var buffer = Buffer.alloc(0);
163
+ var transformStream = new stream_1.Transform({
164
+ transform: function (chunk, encoding, callback) {
165
+ try {
166
+ var decryptedChunk = decipher.update(chunk);
167
+ processedBytes += decryptedChunk.length;
168
+ if (processedBytes > expectedSize) {
169
+ var paddedChunk = Buffer.from(decryptedChunk).slice(0, buffer.length - (processedBytes - expectedSize));
170
+ callback(null, paddedChunk);
171
+ }
172
+ else {
173
+ callback(null, decryptedChunk);
174
+ }
175
+ }
176
+ catch (error) {
177
+ callback(error);
178
+ }
179
+ },
180
+ });
181
+ transformStream.on('error', function (error) {
182
+ console.error('Error during decryption:', error);
183
+ });
184
+ return transformStream;
185
+ };
186
+ exports.newMagix = newMagix;
187
+ var newBase64ToBytes = function (base64Str) {
188
+ return Buffer.from(base64Str, 'base64');
189
+ };
@@ -2,6 +2,7 @@ export declare enum ExposedFn {
2
2
  OnMessage = "onMessage",
3
3
  OnAnyMessage = "onAnyMessage",
4
4
  onAck = "onAck",
5
+ onMessageEdit = "onMessageEdit",
5
6
  onNotificationMessage = "onNotificationMessage",
6
7
  onParticipantsChanged = "onParticipantsChanged",
7
8
  onStateChange = "onStateChange",
@@ -22,6 +22,7 @@ var ExposedFn;
22
22
  ExposedFn["OnMessage"] = "onMessage";
23
23
  ExposedFn["OnAnyMessage"] = "onAnyMessage";
24
24
  ExposedFn["onAck"] = "onAck";
25
+ ExposedFn["onMessageEdit"] = "onMessageEdit";
25
26
  ExposedFn["onNotificationMessage"] = "onNotificationMessage";
26
27
  ExposedFn["onParticipantsChanged"] = "onParticipantsChanged";
27
28
  ExposedFn["onStateChange"] = "onStateChange";
@@ -31,13 +31,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
31
31
  }) : function(o, v) {
32
32
  o["default"] = v;
33
33
  });
34
- var __importStar = (this && this.__importStar) || function (mod) {
35
- if (mod && mod.__esModule) return mod;
36
- var result = {};
37
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
- __setModuleDefault(result, mod);
39
- return result;
40
- };
34
+ var __importStar = (this && this.__importStar) || (function () {
35
+ var ownKeys = function(o) {
36
+ ownKeys = Object.getOwnPropertyNames || function (o) {
37
+ var ar = [];
38
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
39
+ return ar;
40
+ };
41
+ return ownKeys(o);
42
+ };
43
+ return function (mod) {
44
+ if (mod && mod.__esModule) return mod;
45
+ var result = {};
46
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
47
+ __setModuleDefault(result, mod);
48
+ return result;
49
+ };
50
+ })();
41
51
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
42
52
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
53
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -31,13 +31,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
31
31
  }) : function(o, v) {
32
32
  o["default"] = v;
33
33
  });
34
- var __importStar = (this && this.__importStar) || function (mod) {
35
- if (mod && mod.__esModule) return mod;
36
- var result = {};
37
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
- __setModuleDefault(result, mod);
39
- return result;
40
- };
34
+ var __importStar = (this && this.__importStar) || (function () {
35
+ var ownKeys = function(o) {
36
+ ownKeys = Object.getOwnPropertyNames || function (o) {
37
+ var ar = [];
38
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
39
+ return ar;
40
+ };
41
+ return ownKeys(o);
42
+ };
43
+ return function (mod) {
44
+ if (mod && mod.__esModule) return mod;
45
+ var result = {};
46
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
47
+ __setModuleDefault(result, mod);
48
+ return result;
49
+ };
50
+ })();
41
51
  Object.defineProperty(exports, "__esModule", { value: true });
42
52
  exports.filenameFromMimeType = filenameFromMimeType;
43
53
  var path = __importStar(require("path"));
@@ -15,6 +15,7 @@ declare global {
15
15
  onStreamChange: any;
16
16
  onIncomingCall: any;
17
17
  onAck: any;
18
+ onMessageEdit: any;
18
19
  }
19
20
  }
20
21
  export declare class ListenerLayer extends ProfileLayer {
@@ -87,6 +88,13 @@ export declare class ListenerLayer extends ProfileLayer {
87
88
  onAck(callback: (ack: Ack) => void): {
88
89
  dispose: () => void;
89
90
  };
91
+ /**
92
+ * @event Listens to message edited changes
93
+ * @returns Disposable object to stop the listening
94
+ */
95
+ onMessageEdit(callback: (chat: Wid, id: string, msg: Message) => void): {
96
+ dispose: () => void;
97
+ };
90
98
  /**
91
99
  * Listens to real-time location events of all chats
92
100
  * @event Real-time location events
@@ -203,6 +203,22 @@ var ListenerLayer = /** @class */ (function (_super) {
203
203
  catch (error) {
204
204
  console.error(error);
205
205
  }
206
+ try {
207
+ if (!window['onMessageEdit'].exposed) {
208
+ WPP.on('chat.msg_edited', function (data) {
209
+ var eventData = {
210
+ chat: data.chat,
211
+ id: data.id,
212
+ msg: WAPI.processMessageObj(data.msg, true, false),
213
+ };
214
+ window['onMessageEdit'](eventData);
215
+ });
216
+ window['onMessageEdit'].exposed = true;
217
+ }
218
+ }
219
+ catch (error) {
220
+ console.error(error);
221
+ }
206
222
  try {
207
223
  if (!window['onAnyMessage'].exposed) {
208
224
  WPP.on('chat.new_message', function (msg) {
@@ -468,6 +484,13 @@ var ListenerLayer = /** @class */ (function (_super) {
468
484
  ListenerLayer.prototype.onAck = function (callback) {
469
485
  return this.registerEvent(exposed_enum_1.ExposedFn.onAck, callback);
470
486
  };
487
+ /**
488
+ * @event Listens to message edited changes
489
+ * @returns Disposable object to stop the listening
490
+ */
491
+ ListenerLayer.prototype.onMessageEdit = function (callback) {
492
+ return this.registerEvent(exposed_enum_1.ExposedFn.onMessageEdit, callback);
493
+ };
471
494
  ListenerLayer.prototype.onLiveLocation = function (id, callback) {
472
495
  var ids = [];
473
496
  if (typeof id === 'function') {
@@ -57,13 +57,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
57
57
  }) : function(o, v) {
58
58
  o["default"] = v;
59
59
  });
60
- var __importStar = (this && this.__importStar) || function (mod) {
61
- if (mod && mod.__esModule) return mod;
62
- var result = {};
63
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
64
- __setModuleDefault(result, mod);
65
- return result;
66
- };
60
+ var __importStar = (this && this.__importStar) || (function () {
61
+ var ownKeys = function(o) {
62
+ ownKeys = Object.getOwnPropertyNames || function (o) {
63
+ var ar = [];
64
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
65
+ return ar;
66
+ };
67
+ return ownKeys(o);
68
+ };
69
+ return function (mod) {
70
+ if (mod && mod.__esModule) return mod;
71
+ var result = {};
72
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
73
+ __setModuleDefault(result, mod);
74
+ return result;
75
+ };
76
+ })();
67
77
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
68
78
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
69
79
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -14,7 +14,7 @@ export interface Chat {
14
14
  muteExpiration: number;
15
15
  name: string;
16
16
  /** Whatsapp provides us with built-in spam detection and this is its indicator */
17
- notSpam: boolean;
17
+ notSpam?: boolean;
18
18
  pin: number;
19
19
  msgs: null;
20
20
  kind: string;
@@ -7,5 +7,6 @@ export declare enum GroupNotificationType {
7
7
  Description = "description",
8
8
  Picture = "picture",
9
9
  Announce = "announce",
10
- Restrict = "restrict"
10
+ Restrict = "restrict",
11
+ Create = "create"
11
12
  }
@@ -28,4 +28,5 @@ var GroupNotificationType;
28
28
  GroupNotificationType["Picture"] = "picture";
29
29
  GroupNotificationType["Announce"] = "announce";
30
30
  GroupNotificationType["Restrict"] = "restrict";
31
+ GroupNotificationType["Create"] = "create";
31
32
  })(GroupNotificationType || (exports.GroupNotificationType = GroupNotificationType = {}));
@@ -69,7 +69,9 @@ export declare class Whatsapp extends BusinessLayer {
69
69
  * @param message Message object
70
70
  * @returns Decrypted file buffer (`null` otherwise)
71
71
  */
72
- decryptFile(message: Message): Promise<Buffer>;
72
+ decryptFile(message: Message): Promise<Buffer<ArrayBufferLike>>;
73
+ decryptAndSaveFile(message: Message, savePath: string): Promise<void>;
74
+ downloadEncryptedFile: (url: string, outputPath: string, retries?: number) => Promise<void>;
73
75
  /**
74
76
  * Rejects a call received via WhatsApp
75
77
  * @param callId string Call ID, if not passed, all calls will be rejected
@@ -30,6 +30,39 @@ var __extends = (this && this.__extends) || (function () {
30
30
  d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
31
31
  };
32
32
  })();
33
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
34
+ if (k2 === undefined) k2 = k;
35
+ var desc = Object.getOwnPropertyDescriptor(m, k);
36
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
37
+ desc = { enumerable: true, get: function() { return m[k]; } };
38
+ }
39
+ Object.defineProperty(o, k2, desc);
40
+ }) : (function(o, m, k, k2) {
41
+ if (k2 === undefined) k2 = k;
42
+ o[k2] = m[k];
43
+ }));
44
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
45
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
46
+ }) : function(o, v) {
47
+ o["default"] = v;
48
+ });
49
+ var __importStar = (this && this.__importStar) || (function () {
50
+ var ownKeys = function(o) {
51
+ ownKeys = Object.getOwnPropertyNames || function (o) {
52
+ var ar = [];
53
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
54
+ return ar;
55
+ };
56
+ return ownKeys(o);
57
+ };
58
+ return function (mod) {
59
+ if (mod && mod.__esModule) return mod;
60
+ var result = {};
61
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
62
+ __setModuleDefault(result, mod);
63
+ return result;
64
+ };
65
+ })();
33
66
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
34
67
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
35
68
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -66,6 +99,15 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
66
99
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
67
100
  }
68
101
  };
102
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
103
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
104
+ if (ar || !(i in from)) {
105
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
106
+ ar[i] = from[i];
107
+ }
108
+ }
109
+ return to.concat(ar || Array.prototype.slice.call(from));
110
+ };
69
111
  var __importDefault = (this && this.__importDefault) || function (mod) {
70
112
  return (mod && mod.__esModule) ? mod : { "default": mod };
71
113
  };
@@ -76,12 +118,78 @@ var WAuserAgente_1 = require("../config/WAuserAgente");
76
118
  var helpers_1 = require("./helpers");
77
119
  var decrypt_1 = require("./helpers/decrypt");
78
120
  var business_layer_1 = require("./layers/business.layer");
121
+ var fs = __importStar(require("fs"));
122
+ var sleep_1 = require("../utils/sleep");
79
123
  var Whatsapp = /** @class */ (function (_super) {
80
124
  __extends(Whatsapp, _super);
81
125
  function Whatsapp(page, session, options) {
82
126
  var _this = _super.call(this, page, session, options) || this;
83
127
  _this.page = page;
84
128
  _this.connected = null;
129
+ _this.downloadEncryptedFile = function (url_1, outputPath_1) {
130
+ var args_1 = [];
131
+ for (var _i = 2; _i < arguments.length; _i++) {
132
+ args_1[_i - 2] = arguments[_i];
133
+ }
134
+ return __awaiter(_this, __spreadArray([url_1, outputPath_1], args_1, true), void 0, function (url, outputPath, retries) {
135
+ var _loop_1, attempt, state_1;
136
+ if (retries === void 0) { retries = 3; }
137
+ return __generator(this, function (_a) {
138
+ switch (_a.label) {
139
+ case 0:
140
+ _loop_1 = function (attempt) {
141
+ var response_1, error_1;
142
+ return __generator(this, function (_b) {
143
+ switch (_b.label) {
144
+ case 0:
145
+ _b.trys.push([0, 3, , 5]);
146
+ return [4 /*yield*/, axios_1.default.get(url, (0, decrypt_1.makeOptions)(WAuserAgente_1.useragentOverride, 'stream'))];
147
+ case 1:
148
+ response_1 = _b.sent();
149
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
150
+ var writer = fs.createWriteStream(outputPath);
151
+ response_1.data.pipe(writer);
152
+ writer.on('finish', resolve);
153
+ writer.on('error', reject);
154
+ })];
155
+ case 2:
156
+ _b.sent();
157
+ console.log("Encrypted file downloaded at ".concat(outputPath));
158
+ return [2 /*return*/, { value: void 0 }];
159
+ case 3:
160
+ error_1 = _b.sent();
161
+ console.error("Attempt ".concat(attempt, " failed: "), error_1.message);
162
+ if (attempt === retries) {
163
+ console.error("".concat(outputPath, " - All attempt failed to download the file: ").concat(url));
164
+ throw error_1;
165
+ }
166
+ console.log("".concat(outputPath, " - Retrying to download the file: ").concat(url, " in 5 seconds..."));
167
+ return [4 /*yield*/, (0, sleep_1.sleep)(5000)];
168
+ case 4:
169
+ _b.sent();
170
+ return [3 /*break*/, 5];
171
+ case 5: return [2 /*return*/];
172
+ }
173
+ });
174
+ };
175
+ attempt = 1;
176
+ _a.label = 1;
177
+ case 1:
178
+ if (!(attempt <= retries)) return [3 /*break*/, 4];
179
+ return [5 /*yield**/, _loop_1(attempt)];
180
+ case 2:
181
+ state_1 = _a.sent();
182
+ if (typeof state_1 === "object")
183
+ return [2 /*return*/, state_1.value];
184
+ _a.label = 3;
185
+ case 3:
186
+ attempt++;
187
+ return [3 /*break*/, 1];
188
+ case 4: return [2 /*return*/];
189
+ }
190
+ });
191
+ });
192
+ };
85
193
  var interval = null;
86
194
  if (_this.page) {
87
195
  _this.page.on('close', function () { return __awaiter(_this, void 0, void 0, function () {
@@ -91,7 +199,7 @@ var Whatsapp = /** @class */ (function (_super) {
91
199
  });
92
200
  }); });
93
201
  }
94
- interval = setInterval(function (state) { return __awaiter(_this, void 0, void 0, function () {
202
+ interval = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
95
203
  var newConnected;
96
204
  var _this = this;
97
205
  return __generator(this, function (_a) {
@@ -319,7 +427,7 @@ var Whatsapp = /** @class */ (function (_super) {
319
427
  */
320
428
  Whatsapp.prototype.decryptFile = function (message) {
321
429
  return __awaiter(this, void 0, void 0, function () {
322
- var mediaUrl, options, haventGottenImageYet, res, error_1, buff;
430
+ var mediaUrl, options, haventGottenImageYet, res, error_2, buff;
323
431
  return __generator(this, function (_a) {
324
432
  switch (_a.label) {
325
433
  case 0:
@@ -347,7 +455,7 @@ var Whatsapp = /** @class */ (function (_super) {
347
455
  case 6: return [3 /*break*/, 2];
348
456
  case 7: return [3 /*break*/, 9];
349
457
  case 8:
350
- error_1 = _a.sent();
458
+ error_2 = _a.sent();
351
459
  throw 'Error trying to download the file.';
352
460
  case 9:
353
461
  buff = Buffer.from(res.data, 'binary');
@@ -356,6 +464,61 @@ var Whatsapp = /** @class */ (function (_super) {
356
464
  });
357
465
  });
358
466
  };
467
+ Whatsapp.prototype.decryptAndSaveFile = function (message, savePath) {
468
+ return __awaiter(this, void 0, void 0, function () {
469
+ var mediaUrl, tempSavePath_1, inputReadStream, outputWriteStream_1, decryptedStream_1, error_3;
470
+ return __generator(this, function (_a) {
471
+ switch (_a.label) {
472
+ case 0:
473
+ mediaUrl = message.clientUrl || message.deprecatedMms3Url;
474
+ if (!mediaUrl) {
475
+ throw new Error('Message is missing critical data needed to download the file.');
476
+ }
477
+ _a.label = 1;
478
+ case 1:
479
+ _a.trys.push([1, 4, , 5]);
480
+ tempSavePath_1 = savePath + '.encrypted';
481
+ return [4 /*yield*/, this.downloadEncryptedFile(mediaUrl.trim(), tempSavePath_1)];
482
+ case 2:
483
+ _a.sent();
484
+ inputReadStream = fs.createReadStream(tempSavePath_1);
485
+ outputWriteStream_1 = fs.createWriteStream(savePath);
486
+ decryptedStream_1 = (0, decrypt_1.newMagix)(message.mediaKey, message.type, message.size);
487
+ inputReadStream.pipe(decryptedStream_1).pipe(outputWriteStream_1);
488
+ return [4 /*yield*/, new Promise(function (resolve, reject) {
489
+ outputWriteStream_1.on('finish', function () {
490
+ console.log("Deciphering complete. Deleting the encrypted file: ".concat(tempSavePath_1));
491
+ fs.unlink(tempSavePath_1, function (error) {
492
+ if (error) {
493
+ console.error("Error deleting the input file: ".concat(tempSavePath_1), error);
494
+ reject(error);
495
+ }
496
+ else {
497
+ console.log('Encrypted file deleted successfully');
498
+ resolve();
499
+ }
500
+ });
501
+ });
502
+ outputWriteStream_1.on('error', function (error) {
503
+ console.error("Error during writing file: ".concat(savePath), error);
504
+ reject(error);
505
+ });
506
+ decryptedStream_1.on('error', function (error) {
507
+ console.error('An error occurred while decrypting the file', error);
508
+ reject(error);
509
+ });
510
+ })];
511
+ case 3:
512
+ _a.sent();
513
+ return [3 /*break*/, 5];
514
+ case 4:
515
+ error_3 = _a.sent();
516
+ throw error_3;
517
+ case 5: return [2 /*return*/];
518
+ }
519
+ });
520
+ });
521
+ };
359
522
  /**
360
523
  * Rejects a call received via WhatsApp
361
524
  * @param callId string Call ID, if not passed, all calls will be rejected
@@ -20,7 +20,7 @@ exports.defaultOptions = {
20
20
  waitForLogin: true,
21
21
  logger: logger_1.defaultLogger,
22
22
  tokenStore: 'file',
23
- whatsappVersion: '2.3000.10173x',
23
+ whatsappVersion: '2.3000.10208x',
24
24
  deviceName: false,
25
25
  linkPreviewApiServers: null,
26
26
  disableGoogleAnalytics: true,
@@ -31,13 +31,23 @@ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (
31
31
  }) : function(o, v) {
32
32
  o["default"] = v;
33
33
  });
34
- var __importStar = (this && this.__importStar) || function (mod) {
35
- if (mod && mod.__esModule) return mod;
36
- var result = {};
37
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
38
- __setModuleDefault(result, mod);
39
- return result;
40
- };
34
+ var __importStar = (this && this.__importStar) || (function () {
35
+ var ownKeys = function(o) {
36
+ ownKeys = Object.getOwnPropertyNames || function (o) {
37
+ var ar = [];
38
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
39
+ return ar;
40
+ };
41
+ return ownKeys(o);
42
+ };
43
+ return function (mod) {
44
+ if (mod && mod.__esModule) return mod;
45
+ var result = {};
46
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
47
+ __setModuleDefault(result, mod);
48
+ return result;
49
+ };
50
+ })();
41
51
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
42
52
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
43
53
  return new (P || (P = Promise))(function (resolve, reject) {