@vielhuber/wahelper 1.3.1 → 1.3.3

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/WhatsApp.js +31 -14
  2. package/package.json +1 -1
package/WhatsApp.js CHANGED
@@ -34,6 +34,7 @@ export default class WhatsApp {
34
34
  this.inactivityTimeCur = 0;
35
35
  this.inactivityTimeInterval = null;
36
36
  this.inactivityTimeStatus = false;
37
+ this.writeOnEnd = null;
37
38
  if (this.args.device !== undefined && this.args.device !== null && this.args.device !== '') {
38
39
  this.authFolder = 'auth_' + this.formatNumber(this.args.device);
39
40
  this.dbPath = 'whatsapp_' + this.formatNumber(this.args.device) + '.sqlite';
@@ -58,7 +59,7 @@ export default class WhatsApp {
58
59
  async init() {
59
60
  await this.awaitLock('init', true);
60
61
  this.setLock('init', true);
61
- this.write({ success: false, message: 'loading_state', data: null });
62
+ this.write({ success: false, message: 'loading_state', data: null }, false);
62
63
 
63
64
  if (this.isMcp === false) {
64
65
  this.log('cli start');
@@ -73,12 +74,15 @@ export default class WhatsApp {
73
74
  ) {
74
75
  console.error('input missing or unknown action!');
75
76
  this.log('⛔input missing or unknown action!');
76
- this.write({
77
- success: false,
78
- message: 'error',
79
- public_message: 'input missing or unknown action!',
80
- data: null
81
- });
77
+ this.write(
78
+ {
79
+ success: false,
80
+ message: 'error',
81
+ public_message: 'input missing or unknown action!',
82
+ data: null
83
+ },
84
+ true
85
+ );
82
86
  this.removeLocks();
83
87
  } else {
84
88
  this.initDatabase();
@@ -198,7 +202,7 @@ export default class WhatsApp {
198
202
  code = code.match(/.{1,4}/g).join('-');
199
203
  console.log('Bitte verknüpfe das neue Gerät und gib diesen Code ein:');
200
204
  console.log(code);
201
- this.write({ success: false, message: 'pairing_code_required', data: code });
205
+ this.write({ success: false, message: 'pairing_code_required', data: code }, true);
202
206
  }
203
207
  if (this.isMcp) {
204
208
  resolve({
@@ -263,6 +267,9 @@ export default class WhatsApp {
263
267
  process.exit(0);
264
268
  });
265
269
  process.on('exit', code => {
270
+ if (this.writeOnEnd !== null) {
271
+ this.write(this.writeOnEnd, false);
272
+ }
266
273
  this.removeLocks();
267
274
  this.log('final exit');
268
275
  console.log('final exit');
@@ -361,7 +368,7 @@ export default class WhatsApp {
361
368
  `
362
369
  )
363
370
  .all();
364
- this.write({ success: true, message: 'messages_fetched', data: messages });
371
+ this.write({ success: true, message: 'messages_fetched', data: messages }, true);
365
372
  return {
366
373
  content: [
367
374
  {
@@ -381,9 +388,13 @@ export default class WhatsApp {
381
388
  message = message.replace(/ /g, ' ');
382
389
  // replace <br> with real line breaks
383
390
  message = message.replace(/<br\s*\/?>/gis, '\n');
384
- // replace " </x>" with "</x> "
391
+ // replace " </x>" with "</x> " (multiple times)
392
+ message = message.replace(/ +(\<\/[a-z]+\>)/gis, '$1 ');
385
393
  message = message.replace(/ +(\<\/[a-z]+\>)/gis, '$1 ');
386
- // replace "<x> " with " <x>"
394
+ message = message.replace(/ +(\<\/[a-z]+\>)/gis, '$1 ');
395
+ // replace "<x> " with " <x>" (multiple times)
396
+ message = message.replace(/(\<[a-z]+(?:\s[^>]*)?\>) +/gis, ' $1');
397
+ message = message.replace(/(\<[a-z]+(?:\s[^>]*)?\>) +/gis, ' $1');
387
398
  message = message.replace(/(\<[a-z]+(?:\s[^>]*)?\>) +/gis, ' $1');
388
399
  // replace " \n" with "\n"
389
400
  message = message.replace(/ \n/gis, '\n');
@@ -437,14 +448,16 @@ export default class WhatsApp {
437
448
  async sendMessageToUser(number = null, message = null, attachments = null) {
438
449
  let jid = this.formatNumber(number) + '@s.whatsapp.net',
439
450
  msgResponse = [];
451
+ this.log('begin send message to user ' + jid);
440
452
  msgResponse.push(await this.sock.sendMessage(jid, { text: this.formatMessage(message) }));
453
+ this.log('end send message to user ' + jid);
441
454
  //this.log(attachments);
442
455
  if (attachments !== null && attachments.length > 0) {
443
456
  for (let attachments__value of attachments) {
444
457
  msgResponse.push(await this.sock.sendMessage(jid, this.getAttachmentObj(attachments__value)));
445
458
  }
446
459
  }
447
- this.write({ success: true, message: 'message_user_sent', data: msgResponse });
460
+ this.write({ success: true, message: 'message_user_sent', data: msgResponse }, true);
448
461
  //this.log(msgResponse);
449
462
  return {
450
463
  content: [{ type: 'text', text: JSON.stringify(msgResponse, null, 2) }],
@@ -470,7 +483,7 @@ export default class WhatsApp {
470
483
  }
471
484
  }
472
485
  }
473
- this.write({ success: true, message: 'message_group_sent', data: msgResponse });
486
+ this.write({ success: true, message: 'message_group_sent', data: msgResponse }, true);
474
487
  //this.log(msgResponse);
475
488
  return {
476
489
  content: [{ type: 'text', text: JSON.stringify(msgResponse, null, 2) }],
@@ -602,10 +615,14 @@ export default class WhatsApp {
602
615
  fs.appendFileSync(this.dirname + '/' + this.logPath, logLine);
603
616
  }
604
617
 
605
- write(msg) {
618
+ write(msg, writeOnEnd = true) {
606
619
  if (this.dataPath === undefined) {
607
620
  return;
608
621
  }
622
+ if (writeOnEnd === true) {
623
+ this.writeOnEnd = msg;
624
+ return;
625
+ }
609
626
  fs.writeFileSync(this.dirname + '/' + this.dataPath, JSON.stringify(msg));
610
627
  }
611
628
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vielhuber/wahelper",
3
- "version": "1.3.1",
3
+ "version": "1.3.3",
4
4
  "description": "Lightweight whatsapp integration layer.",
5
5
  "main": "WhatsApp.js",
6
6
  "files": [