@jsm-mit/whatsapp-package 0.0.2 → 0.0.4

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/README.md CHANGED
@@ -1 +1,15 @@
1
- ## Whatapp-package
1
+ ## Whatapp-package
2
+
3
+ ### Uruchomienie lokalnie
4
+
5
+ 1. Zainstaluj zależności:
6
+ ```bash
7
+ npm install
8
+ npm install @jsm-mit/pigeon-package@0.0.12 @jsm-mit/utils-package@0.0.15 --save-dev
9
+ ```
10
+ 2. Uruchom skrypt lokalny:
11
+ ```bash
12
+ npm run local
13
+ ```
14
+
15
+ Skrypt zbuduje projekt i uruchomi `sandbox/run-local.js`, który importuje bibliotekę z `dist/index.js`. Plik `sandbox/run-local.js` jest lokalnym narzędziem deweloperskim i nie jest częścią głównego API pakietu.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './whatsapp.service.js';
2
+ export * from './whatsapp-message.js';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC;AACtC,cAAc,uBAAuB,CAAC"}
package/dist/index.js CHANGED
@@ -1 +1,2 @@
1
1
  export * from './whatsapp.service.js';
2
+ export * from './whatsapp-message.js';
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=run-local.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"run-local.d.ts","sourceRoot":"","sources":["../src/run-local.ts"],"names":[],"mappings":""}
@@ -0,0 +1,22 @@
1
+ import { WhatsAppService } from './whatsapp.service.js';
2
+ const pigeon = {
3
+ reportInfoAsyncSafe: async (title, message) => {
4
+ console.log(`[Pigeon] INFO ${title}:`, message);
5
+ },
6
+ reportUrgentAsyncSafe: async (title, message) => {
7
+ console.error(`[Pigeon] URGENT ${title}:`, message);
8
+ }
9
+ };
10
+ async function main() {
11
+ const service = new WhatsAppService(pigeon);
12
+ try {
13
+ await service.initialize();
14
+ console.log('✅ WhatsApp client initialized. Skopiuj i zeskanuj kod QR w terminalu.');
15
+ console.log('Ctrl+C to exit.');
16
+ }
17
+ catch (err) {
18
+ console.error('❌ Inicjalizacja nie powiodła się:', err);
19
+ process.exit(1);
20
+ }
21
+ }
22
+ main();
@@ -0,0 +1,12 @@
1
+ export declare class WhatsAppMessage {
2
+ readonly id: string;
3
+ readonly from: string;
4
+ readonly to: string;
5
+ readonly body: string;
6
+ readonly timestamp: number;
7
+ readonly isForwarded: boolean;
8
+ readonly hasMedia: boolean;
9
+ readonly isFromAdmin: boolean;
10
+ constructor(originalMessage: any);
11
+ }
12
+ //# sourceMappingURL=whatsapp-message.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"whatsapp-message.d.ts","sourceRoot":"","sources":["../src/whatsapp-message.ts"],"names":[],"mappings":"AAIA,qBAAa,eAAe;IACxB,SAAgB,EAAE,EAAE,MAAM,CAAC;IAC3B,SAAgB,IAAI,EAAE,MAAM,CAAC;IAC7B,SAAgB,EAAE,EAAE,MAAM,CAAC;IAC3B,SAAgB,IAAI,EAAE,MAAM,CAAC;IAC7B,SAAgB,SAAS,EAAE,MAAM,CAAC;IAClC,SAAgB,WAAW,EAAE,OAAO,CAAC;IACrC,SAAgB,QAAQ,EAAE,OAAO,CAAC;IAClC,SAAgB,WAAW,EAAE,OAAO,CAAC;gBAEzB,eAAe,EAAE,GAAG;CAUnC"}
@@ -0,0 +1,14 @@
1
+ import { getEnvVariableUnsafe } from "@jsm-mit/utils-package";
2
+ const adminNumber = getEnvVariableUnsafe('WHATSAPP_ADMIN_NUMBER');
3
+ export class WhatsAppMessage {
4
+ constructor(originalMessage) {
5
+ this.id = originalMessage.id?.id || '';
6
+ this.from = originalMessage.from || '';
7
+ this.to = originalMessage.to || '';
8
+ this.body = originalMessage.body || '';
9
+ this.timestamp = originalMessage.timestamp || 0;
10
+ this.isForwarded = originalMessage.isForwarded || false;
11
+ this.hasMedia = originalMessage.hasMedia || false;
12
+ this.isFromAdmin = this.from.includes(adminNumber);
13
+ }
14
+ }
@@ -1,11 +1,12 @@
1
1
  import { Observable } from 'rxjs';
2
2
  import type { Pigeon } from '@jsm-mit/pigeon-package';
3
+ import { WhatsAppMessage } from './whatsapp-message.js';
3
4
  export declare class WhatsAppService {
4
5
  private pigeon;
5
6
  private readonly createdAt;
6
7
  private waClient;
7
8
  private readonly messageSubject;
8
- get messages$(): Observable<any[]>;
9
+ get messages$(): Observable<WhatsAppMessage[]>;
9
10
  constructor(pigeon: Pigeon);
10
11
  private formatUptime;
11
12
  initialize(): Promise<void>;
@@ -1 +1 @@
1
- {"version":3,"file":"whatsapp.service.d.ts","sourceRoot":"","sources":["../src/whatsapp.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAW,MAAM,MAAM,CAAC;AAE3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AAItD,qBAAa,eAAe;IAWpB,OAAO,CAAC,MAAM;IATlB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAsB;IAChD,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAwB;IAEvD,IAAW,SAAS,IAAI,UAAU,CAAC,GAAG,EAAE,CAAC,CAExC;gBAGW,MAAM,EAAE,MAAM;IAI1B,OAAO,CAAC,YAAY;IAcb,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAgHrB,oBAAoB,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAc7D,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAQvE"}
1
+ {"version":3,"file":"whatsapp.service.d.ts","sourceRoot":"","sources":["../src/whatsapp.service.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAW,MAAM,MAAM,CAAC;AAE3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,yBAAyB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAMxD,qBAAa,eAAe;IAWpB,OAAO,CAAC,MAAM;IATlB,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAsB;IAChD,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAoC;IAEnE,IAAW,SAAS,IAAI,UAAU,CAAC,eAAe,EAAE,CAAC,CAEpD;gBAGW,MAAM,EAAE,MAAM;IAI1B,OAAO,CAAC,YAAY;IAcb,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IA8FrB,oBAAoB,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAc7D,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CAQvE"}
@@ -1,8 +1,10 @@
1
1
  import pkg from 'whatsapp-web.js';
2
2
  import qrcode from 'qrcode-terminal';
3
3
  import { Observable, Subject } from 'rxjs';
4
- import { toJson, polandTimeOptions, serverTimeOptions } from '@jsm-mit/utils-package';
5
- const { Client: WAClient, LocalAuth } = pkg;
4
+ import { toJson, polandTimeOptions, serverTimeOptions, getEnvVariableUnsafe } from '@jsm-mit/utils-package';
5
+ import { WhatsAppMessage } from './whatsapp-message.js';
6
+ const { Client, LocalAuth } = pkg;
7
+ const adminNumber = getEnvVariableUnsafe('WHATSAPP_ADMIN_NUMBER');
6
8
  export class WhatsAppService {
7
9
  get messages$() {
8
10
  return this.messageSubject.asObservable();
@@ -33,7 +35,7 @@ export class WhatsAppService {
33
35
  return resolve();
34
36
  }
35
37
  this.pigeon.reportInfoAsyncSafe('Log in to WhatsApp by scanning the QR code in the terminal required.', '');
36
- this.waClient = new WAClient({
38
+ this.waClient = new Client({
37
39
  authStrategy: new LocalAuth(),
38
40
  puppeteer: {
39
41
  headless: true,
@@ -47,7 +49,7 @@ export class WhatsAppService {
47
49
  this.waClient.on('ready', async () => {
48
50
  try {
49
51
  const uptime = this.formatUptime(Date.now() - this.createdAt);
50
- await this.sendMessageAsyncSafe("48601926367", `⚠️ Common Notifier is up for ${uptime}`);
52
+ await this.sendMessageAsyncSafe(adminNumber, `⚠️ Common Notifier is up for ${uptime}`);
51
53
  resolve();
52
54
  }
53
55
  catch (err) {
@@ -80,8 +82,9 @@ export class WhatsAppService {
80
82
  return reject(err);
81
83
  });
82
84
  this.waClient.on('message', async (message) => {
85
+ const whatsappMessage = new WhatsAppMessage(message);
83
86
  console.log(`[WhatsApp] Received message from ${message.from}: ${message.body}`);
84
- if (message.body.toLowerCase() === 'ping') {
87
+ if (whatsappMessage.body.toLowerCase() === 'testuje ping') {
85
88
  message.reply('pong').catch((err) => {
86
89
  this.pigeon.reportUrgentAsyncSafe(`Error sending ping response`, toJson(err));
87
90
  console.error('❌ Error sending ping response');
@@ -89,30 +92,14 @@ export class WhatsAppService {
89
92
  });
90
93
  return;
91
94
  }
92
- if (message.body.toLowerCase() === 'test') {
93
- this.sendMessageAsyncSafe("48601926367", `Test successful, application is up since ${new Date(this.createdAt).toLocaleDateString('pl-PL', polandTimeOptions)}, server time: ${new Date(this.createdAt).toLocaleTimeString('pl-PL', serverTimeOptions)}`);
95
+ if (whatsappMessage.isFromAdmin && whatsappMessage.body.toLowerCase() === 'testuje test') {
96
+ this.sendMessageAsyncSafe(adminNumber, `Test successful, application is up since ${new Date(this.createdAt).toLocaleDateString('pl-PL', polandTimeOptions)}, server time: ${new Date(this.createdAt).toLocaleTimeString('pl-PL', serverTimeOptions)}`);
94
97
  return;
95
98
  }
96
- // if (message.body.toLowerCase() === 'roundtrip') {
97
- // this.sendMessageAsyncSafe(
98
- // "48601926367",
99
- // `Roundtrip test started at ${new Date().toLocaleDateString('pl-PL', polandTimeOptions)}`
100
- // );
101
- // const args: AddTaskArgs = {
102
- // channel: this.channel,
103
- // payload: `Roundtrip test successful`,
104
- // parentIds: []
105
- // };
106
- // await this.actor.addTaskAsync(args, true).catch((err: any) => {
107
- // this.pigeon.reportUrgentAsyncSafe(`Error adding task`, toJson(err));
108
- // console.error('❌ Error adding task');
109
- // console.error(toJson(err));
110
- // });
111
- // return;
112
- // }
113
99
  const chat = await this.waClient.getChatById(message.from);
114
100
  const messages = await chat.fetchMessages({ limit: 10 });
115
- this.messageSubject.next(messages);
101
+ const whatsappMessages = messages?.map((msg) => new WhatsAppMessage(msg)) || [];
102
+ this.messageSubject.next(whatsappMessages);
116
103
  });
117
104
  });
118
105
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsm-mit/whatsapp-package",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "",
5
5
  "homepage": "https://github.com/JSM-Common/whatsapp-package#readme",
6
6
  "bugs": {
@@ -28,6 +28,7 @@
28
28
  "build": "tsc --build",
29
29
  "clean": "rimraf dist",
30
30
  "prepare": "npm run build",
31
+ "local": "npm run build && node sandbox/run-local.js",
31
32
  "publish-public": "npm install && npm login && npm publish --access public",
32
33
  "test": "echo \"Error: no test specified\" && exit 1"
33
34
  },