@vielhuber/wahelper 1.4.3 → 1.4.5

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/wahelper.js +43 -24
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.4.3",
3
+ "version": "1.4.5",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "wahelper.js",
6
6
  "files": [
package/wahelper.js CHANGED
@@ -1,4 +1,9 @@
1
- import makeWASocket, { useMultiFileAuthState, DisconnectReason, downloadMediaMessage } from 'baileys';
1
+ import makeWASocket, {
2
+ useMultiFileAuthState,
3
+ DisconnectReason,
4
+ downloadMediaMessage,
5
+ fetchLatestBaileysVersion
6
+ } from 'baileys';
2
7
  import P from 'pino';
3
8
  import { fileURLToPath } from 'url';
4
9
  import { dirname } from 'path';
@@ -95,7 +100,6 @@ export default class wahelper {
95
100
  this.sendMessageToGroup(this.args.name, this.args.message, this.args.attachments)
96
101
  );
97
102
  }
98
- console.log(response);
99
103
  await this.endSession();
100
104
  }
101
105
  this.log('cli stop');
@@ -133,6 +137,7 @@ export default class wahelper {
133
137
  async authAndRun(fn) {
134
138
  return new Promise(async (resolve, reject) => {
135
139
  let { state, saveCreds } = await useMultiFileAuthState(this.dirname + '/' + this.authFolder);
140
+ let { version } = await fetchLatestBaileysVersion();
136
141
  this.sock = makeWASocket({
137
142
  auth: state,
138
143
  logger: P(
@@ -141,7 +146,9 @@ export default class wahelper {
141
146
  },
142
147
  P.destination(2)
143
148
  ),
144
- syncFullHistory: true
149
+ syncFullHistory: true,
150
+ version: version,
151
+ browser: ['Chrome', 'Windows', '110.0.5481.177'] // simulate real browser
145
152
  });
146
153
  /* this syncs on pairing / initial connection */
147
154
  this.sock.ev.on('messaging-history.set', async obj => {
@@ -201,7 +208,7 @@ export default class wahelper {
201
208
  resolve(await this.authAndRun(fn));
202
209
  return;
203
210
  } else {
204
- this.log('⚠️close: 3');
211
+ this.log('⚠️close: 3 (status code: ' + statusCode + ')');
205
212
  resolve();
206
213
  return;
207
214
  }
@@ -330,26 +337,31 @@ export default class wahelper {
330
337
  await this.awaitInactivityTimer();
331
338
 
332
339
  // fetch from database
333
- let messages = this.db
334
- .prepare(
335
- `
336
- SELECT id, \`from\`, \`to\`, content, media_filename, timestamp
337
- FROM messages
338
- ORDER BY timestamp DESC
339
- ${limit !== null ? 'LIMIT ' + limit : ''}
340
- `
341
- )
342
- .all();
343
- this.write({ success: true, message: 'messages_fetched', data: messages }, true);
344
- return {
345
- content: [
346
- {
347
- type: 'text',
348
- text: 'Fetched ' + messages.length + ' messages from database'
349
- }
350
- ],
351
- structuredContent: messages
352
- };
340
+ try {
341
+ let messages = this.db
342
+ .prepare(
343
+ `
344
+ SELECT id, \`from\`, \`to\`, content, media_filename, timestamp
345
+ FROM messages
346
+ ORDER BY timestamp DESC
347
+ ${limit !== null ? 'LIMIT ' + limit : ''}
348
+ `
349
+ )
350
+ .all();
351
+ this.write({ success: true, message: 'messages_fetched', data: messages }, true);
352
+ return {
353
+ content: [
354
+ {
355
+ type: 'text',
356
+ text: 'Fetched ' + messages.length + ' messages from database'
357
+ }
358
+ ],
359
+ structuredContent: messages
360
+ };
361
+ } catch (error) {
362
+ this.log('⛔ Error fetching database: ' + error.message + ' (code: ' + error.code + ')');
363
+ }
364
+ return null;
353
365
  }
354
366
 
355
367
  formatMessage(message) {
@@ -507,6 +519,9 @@ export default class wahelper {
507
519
 
508
520
  if (key === 'limit') {
509
521
  value = parseInt(value);
522
+ if (!Number.isInteger(value)) {
523
+ continue;
524
+ }
510
525
  }
511
526
 
512
527
  args[key] = value;
@@ -554,6 +569,8 @@ export default class wahelper {
554
569
  try {
555
570
  this.db = new DatabaseSync(this.dirname + '/' + this.dbPath);
556
571
  this.dbIsOpen = true;
572
+ this.db.exec('PRAGMA journal_mode = WAL');
573
+ this.db.exec('PRAGMA busy_timeout = 5000');
557
574
  this.db.exec(`
558
575
  CREATE TABLE IF NOT EXISTS messages (
559
576
  id TEXT PRIMARY KEY,
@@ -571,6 +588,8 @@ export default class wahelper {
571
588
  }
572
589
 
573
590
  async storeDataToDatabase(data) {
591
+ this.log('storeDataToDatabase');
592
+
574
593
  if (!data.messages || data.messages.length === 0) {
575
594
  return;
576
595
  }