waengine 1.7.3 → 1.7.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/src/easy-bot.js CHANGED
@@ -1,6 +1,102 @@
1
1
  import { WhatsAppClient } from "./client.js";
2
2
  import { MultiWhatsAppClient } from "./multi-client.js";
3
3
  import { EasyAdvanced, EasyAdvancedRule } from "./easy-advanced.js";
4
+ import { ConsoleLogger } from "./console-logger.js";
5
+ import { ErrorHandler } from "./error-handler.js";
6
+
7
+ // ===== CHAIN-PROXY PATTERN - UNIVERSELLE LÖSUNG =====
8
+ class EasyChain {
9
+ constructor(bot) {
10
+ this.bot = bot;
11
+ this.currentRule = null;
12
+ }
13
+
14
+ // ===== RULE METHODS - ALLE geben this (EasyChain) zurück! =====
15
+ when(trigger) {
16
+ const rule = new EasyRule(trigger, this.bot);
17
+ this.bot.rules.push(rule);
18
+ this.currentRule = rule;
19
+ return this; // ✅ KRITISCH: Gibt EasyChain zurück für weitere Chaining
20
+ }
21
+
22
+ reply(text) {
23
+ if (this.currentRule) {
24
+ this.currentRule.actions.push({ type: 'reply', value: text });
25
+ }
26
+ return this; // ✅ Gibt EasyChain zurück
27
+ }
28
+
29
+ send(text) {
30
+ if (this.currentRule) {
31
+ this.currentRule.actions.push({ type: 'reply', value: text });
32
+ }
33
+ return this;
34
+ }
35
+
36
+ react(emoji) {
37
+ if (this.currentRule) {
38
+ this.currentRule.actions.push({ type: 'react', value: emoji });
39
+ }
40
+ return this;
41
+ }
42
+
43
+ type(seconds = 2) {
44
+ if (this.currentRule) {
45
+ this.currentRule.actions.push({ type: 'type', value: seconds * 1000 });
46
+ }
47
+ return this;
48
+ }
49
+
50
+ // ===== NEUE METHODEN =====
51
+ done() {
52
+ this.currentRule = null; // Reset für nächste Rule
53
+ return this; // Ermöglicht weitere .when() Aufrufe
54
+ }
55
+
56
+ // ===== BOT CONTROL METHODS =====
57
+ start() {
58
+ return this.bot.start();
59
+ }
60
+
61
+ stop() {
62
+ return this.bot.stop();
63
+ }
64
+
65
+ status() {
66
+ return this.bot.status();
67
+ }
68
+
69
+ // ===== ADVANCED METHODS =====
70
+ enableAI() {
71
+ this.bot.enableAI();
72
+ return this;
73
+ }
74
+
75
+ enableAll() {
76
+ this.bot.enableAll();
77
+ return this;
78
+ }
79
+
80
+ enableDefaults() {
81
+ this.bot.enableDefaults();
82
+ return this;
83
+ }
84
+
85
+ command(cmd, response) {
86
+ this.bot.command(cmd, response);
87
+ return this;
88
+ }
89
+
90
+ autoReply(trigger, response) {
91
+ this.bot.autoReply(trigger, response);
92
+ return this;
93
+ }
94
+
95
+ template(name, content) {
96
+ this.bot.template(name, content);
97
+ return this;
98
+ }
99
+ }
4
100
 
5
101
  export class EasyBot {
6
102
  constructor() {
@@ -21,8 +117,11 @@ export class EasyBot {
21
117
  reactionsEnabled: true
22
118
  };
23
119
 
24
- // ===== ADVANCED FEATURES INTEGRATION - NEU! =====
25
- this.advanced = null; // Wird nach Client-Erstellung initialisiert
120
+ // Advanced features integration
121
+ this.advanced = null;
122
+
123
+ // ✅ Chain-Proxy für Fluent API
124
+ this.chain = new EasyChain(this);
26
125
  }
27
126
 
28
127
  // ===== FACTORY METHODS =====
@@ -33,29 +132,41 @@ export class EasyBot {
33
132
  authDir: "./auth",
34
133
  logLevel: "silent",
35
134
 
36
- // CLEAN QR DEFAULTS - NEU!
37
- printQR: false, // Browser QR bevorzugt
38
- qrSpamPrevention: true, // Anti-Spam aktiviert
39
- qrDisplayInterval: 30000, // 30s zwischen Terminal QR
40
- qrMaxDisplays: 3, // Max 3 Terminal QR
41
- clearTerminalOnQR: true, // Terminal bei QR leeren
135
+ // CLEAN QR DEFAULTS
136
+ printQR: false,
137
+ qrSpamPrevention: true,
138
+ qrDisplayInterval: 30000,
139
+ qrMaxDisplays: 1, // FIX: Nur 1 QR Code!
140
+ clearTerminalOnQR: true,
42
141
 
43
- // ROBUSTE CONNECTION DEFAULTS - NEU!
44
- maxReconnectAttempts: 100, // Viele Versuche für 24/7
45
- reconnectInterval: 2000, // Schnellere Wiederverbindung
142
+ // ROBUSTE CONNECTION DEFAULTS
143
+ maxReconnectAttempts: 100,
144
+ reconnectInterval: 2000,
46
145
  exponentialBackoff: true,
47
- maxBackoffDelay: 30000, // Max 30 Sekunden
48
- heartbeatInterval: 20000, // 20 Sekunden Heartbeat
146
+ maxBackoffDelay: 30000,
147
+ heartbeatInterval: 20000,
49
148
  connectionTimeout: 180000, // 3 Minuten Timeout
50
149
  keepAlive: true,
51
150
  quietHeartbeat: true, // Heartbeat-Spam deaktivieren
151
+
152
+ // SCHÖNE CONSOLE FÜR ALLE!
153
+ verbose: options.verbose || false,
154
+ silent: options.silent || false,
155
+
156
+ // ERROR HANDLER - DEINE EMAIL IMMER DABEI!
157
+ errorHandler: {
158
+ supportEmail: "Liaia@outlook.de", // IMMER deine Email
159
+ showSupportInfo: true,
160
+ logErrors: true
161
+ },
162
+
52
163
  ...options
53
164
  });
54
165
 
55
166
  // Advanced Features nach Client-Erstellung initialisieren
56
167
  bot.advanced = new EasyAdvanced(bot);
57
168
 
58
- return bot;
169
+ return bot.chain; // ✅ KRITISCH: Gibt Chain-Proxy zurück für Fluent API!
59
170
  }
60
171
 
61
172
  static createMulti(deviceCount = 2, options = {}) {
@@ -73,6 +184,9 @@ export class EasyBot {
73
184
  connectionTimeout: 180000,
74
185
  keepAlive: true,
75
186
  quietHeartbeat: true, // Heartbeat-Spam deaktivieren
187
+ // Console Logger Settings
188
+ verbose: options.verbose || false,
189
+ silent: options.silent || false,
76
190
  ...options
77
191
  });
78
192
  return bot;
@@ -99,7 +213,7 @@ export class EasyBot {
99
213
  when(trigger) {
100
214
  const rule = new EasyRule(trigger, this);
101
215
  this.rules.push(rule);
102
- return rule;
216
+ return this.chain; // ✅ KRITISCH: Gibt Chain zurück für Fluent API!
103
217
  }
104
218
 
105
219
  // Shorthand methods
@@ -696,6 +810,254 @@ export class EasyBot {
696
810
 
697
811
  return this;
698
812
  }
813
+
814
+ // ===== ERROR HANDLING METHODS - NEU! =====
815
+
816
+ /**
817
+ * Configure support contact information
818
+ */
819
+ setSupportContact(email, discord = null, github = null) {
820
+ if (this.client) {
821
+ this.client.errorHandler.options.supportEmail = email;
822
+ if (discord) this.client.errorHandler.options.supportDiscord = discord;
823
+ if (github) this.client.errorHandler.options.supportGitHub = github;
824
+ }
825
+ return this;
826
+ }
827
+
828
+ /**
829
+ * Enable/disable error reporting
830
+ */
831
+ enableErrorReporting(enabled = true) {
832
+ if (this.client) {
833
+ this.client.errorHandler.options.sendErrorReports = enabled;
834
+ }
835
+ return this;
836
+ }
837
+
838
+ /**
839
+ * Get error statistics
840
+ */
841
+ getErrorStats() {
842
+ if (this.client) {
843
+ return this.client.errorHandler.getErrorStats();
844
+ }
845
+ return { totalErrors: 0, lastError: null };
846
+ }
847
+
848
+ /**
849
+ * Add error handling to bot
850
+ */
851
+ onError(callback) {
852
+ if (this.client) {
853
+ this.client.on('error', callback);
854
+ }
855
+ return this;
856
+ }
857
+
858
+ // ===== BUSINESS FEATURES - NEU! =====
859
+
860
+ /**
861
+ * Set business profile
862
+ */
863
+ setBusinessProfile(profileData) {
864
+ if (this.client) {
865
+ this.client.business.setProfile(profileData);
866
+ }
867
+ return this;
868
+ }
869
+
870
+ /**
871
+ * Create product
872
+ */
873
+ createProduct(productData) {
874
+ if (this.client) {
875
+ return this.client.business.createProduct(productData);
876
+ }
877
+ return this;
878
+ }
879
+
880
+ /**
881
+ * Send product template
882
+ */
883
+ sendProduct(productId) {
884
+ this.addRule(new EasyRule('product', async (msg) => {
885
+ if (this.client) {
886
+ const product = this.client.business.getProduct(productId);
887
+ if (product) {
888
+ await this.client.ui.sendProductTemplate(msg.from, product);
889
+ }
890
+ }
891
+ }));
892
+ return this;
893
+ }
894
+
895
+ // ===== UI COMPONENTS - NEU! =====
896
+
897
+ /**
898
+ * Send carousel
899
+ */
900
+ carousel(cards, options = {}) {
901
+ this.addRule(new EasyRule('carousel', async (msg) => {
902
+ if (this.client) {
903
+ await this.client.ui.sendCarousel(msg.from, cards, options);
904
+ }
905
+ }));
906
+ return this;
907
+ }
908
+
909
+ /**
910
+ * Create interactive form
911
+ */
912
+ form(formConfig) {
913
+ this.addRule(new EasyRule('form', async (msg) => {
914
+ if (this.client) {
915
+ await this.client.ui.createForm(msg.from, formConfig);
916
+ }
917
+ }));
918
+ return this;
919
+ }
920
+
921
+ /**
922
+ * Send quick replies
923
+ */
924
+ quickReplies(text, replies, options = {}) {
925
+ this.addRule(new EasyRule('quickReplies', async (msg) => {
926
+ if (this.client) {
927
+ await this.client.ui.sendQuickReplies(msg.from, text, replies, options);
928
+ }
929
+ }));
930
+ return this;
931
+ }
932
+
933
+ /**
934
+ * Set persistent menu
935
+ */
936
+ setPersistentMenu(menuItems) {
937
+ if (this.client) {
938
+ // Set menu for all users who interact with the bot
939
+ this.client.on('message', async (msg) => {
940
+ if (msg.text?.toLowerCase() === 'menu') {
941
+ await this.client.ui.setPersistentMenu(msg.from, menuItems);
942
+ }
943
+ });
944
+ }
945
+ return this;
946
+ }
947
+
948
+ // ===== ANALYTICS FEATURES - NEU! =====
949
+
950
+ /**
951
+ * Get analytics stats
952
+ */
953
+ getAnalytics(days = 7) {
954
+ if (this.client) {
955
+ return this.client.analyticsManager.getDetailedStats(days);
956
+ }
957
+ return null;
958
+ }
959
+
960
+ /**
961
+ * Track custom event
962
+ */
963
+ trackEvent(eventName, data = {}) {
964
+ if (this.client) {
965
+ this.client.analyticsManager.trackEvent(eventName, data);
966
+ }
967
+ return this;
968
+ }
969
+
970
+ // ===== HELPER METHODS FÜR MEDIA =====
971
+
972
+ async sendMedia(msg, type, data) {
973
+ try {
974
+ const activeClient = this.isMultiDevice ? this.multiClient.getNextDevice() : this.client;
975
+
976
+ switch (type) {
977
+ case 'image':
978
+ if (data.path) {
979
+ await activeClient.socket.sendMessage(msg.from, {
980
+ image: { url: data.path },
981
+ caption: data.caption || ''
982
+ });
983
+ }
984
+ break;
985
+
986
+ case 'video':
987
+ if (data.path) {
988
+ await activeClient.socket.sendMessage(msg.from, {
989
+ video: { url: data.path },
990
+ caption: data.caption || ''
991
+ });
992
+ }
993
+ break;
994
+
995
+ case 'audio':
996
+ if (data) {
997
+ await activeClient.socket.sendMessage(msg.from, {
998
+ audio: { url: data },
999
+ mimetype: 'audio/mp4'
1000
+ });
1001
+ }
1002
+ break;
1003
+
1004
+ case 'sticker':
1005
+ if (data) {
1006
+ await activeClient.socket.sendMessage(msg.from, {
1007
+ sticker: { url: data }
1008
+ });
1009
+ }
1010
+ break;
1011
+
1012
+ case 'document':
1013
+ if (data.path) {
1014
+ await activeClient.socket.sendMessage(msg.from, {
1015
+ document: { url: data.path },
1016
+ fileName: data.filename || 'document'
1017
+ });
1018
+ }
1019
+ break;
1020
+
1021
+ case 'location':
1022
+ await activeClient.socket.sendMessage(msg.from, {
1023
+ location: {
1024
+ degreesLatitude: data.lat,
1025
+ degreesLongitude: data.lng
1026
+ }
1027
+ });
1028
+ break;
1029
+
1030
+ case 'contact':
1031
+ await activeClient.socket.sendMessage(msg.from, {
1032
+ contacts: {
1033
+ displayName: data.name,
1034
+ contacts: [{ vcard: data.vcard }]
1035
+ }
1036
+ });
1037
+ break;
1038
+
1039
+ case 'poll':
1040
+ await activeClient.socket.sendMessage(msg.from, {
1041
+ poll: {
1042
+ name: data.question,
1043
+ values: data.options,
1044
+ selectableCount: 1
1045
+ }
1046
+ });
1047
+ break;
1048
+ }
1049
+ } catch (error) {
1050
+ console.error(`❌ Fehler beim Senden von ${type}:`, error.message);
1051
+ await this.sendReply(msg, `❌ ${type} konnte nicht gesendet werden`);
1052
+ }
1053
+ }
1054
+
1055
+ // ===== HELPER METHOD FÜR RULES =====
1056
+
1057
+ addRule(rule) {
1058
+ this.rules.push(rule);
1059
+ return this;
1060
+ }
699
1061
  }
700
1062
 
701
1063
  // ===== EASY RULE CLASS =====
@@ -707,25 +1069,41 @@ class EasyRule {
707
1069
  this.actions = [];
708
1070
  }
709
1071
 
710
- // Actions - Return this for action chaining within rule
1072
+ // Actions - Return chain for fluent API!
711
1073
  reply(text) {
712
1074
  this.actions.push({ type: 'reply', value: text });
713
- return this; // Return this for more actions on same rule!
1075
+ return this.bot.chain; // KRITISCH: Gibt Chain zurück für Fluent API!
714
1076
  }
715
1077
 
716
1078
  send(text) {
717
1079
  this.actions.push({ type: 'reply', value: text });
718
- return this; // Return this for more actions on same rule!
1080
+ return this.bot.chain; // KRITISCH: Gibt Chain zurück für Fluent API!
719
1081
  }
720
1082
 
721
1083
  react(emoji) {
722
1084
  this.actions.push({ type: 'react', value: emoji });
723
- return this; // Return this for more actions on same rule!
1085
+ return this.bot.chain; // KRITISCH: Gibt Chain zurück für Fluent API!
724
1086
  }
725
1087
 
726
1088
  type(seconds = 2) {
727
1089
  this.actions.push({ type: 'type', value: seconds * 1000 });
728
- return this; // Return this for more actions on same rule!
1090
+ return this.bot.chain; // KRITISCH: Gibt Chain zurück für Fluent API!
1091
+ }
1092
+
1093
+ // Neue Typing-Methoden hinzufügen
1094
+ quickType(text) {
1095
+ this.actions.push({ type: 'quickType', value: text });
1096
+ return this;
1097
+ }
1098
+
1099
+ normalType(text) {
1100
+ this.actions.push({ type: 'normalType', value: text });
1101
+ return this;
1102
+ }
1103
+
1104
+ slowType(text) {
1105
+ this.actions.push({ type: 'slowType', value: text });
1106
+ return this;
729
1107
  }
730
1108
 
731
1109
  typeAndReply(text, seconds = 2) {
@@ -1010,12 +1388,13 @@ class EasyRule {
1010
1388
  return this;
1011
1389
  }
1012
1390
 
1013
- // NEW: when() method on rule for chaining new rules!
1391
+ // NEW: when() method on rule for chaining new rules! - KORRIGIERT!
1014
1392
  when(trigger) {
1015
1393
  // Create new rule and add to bot
1016
1394
  const newRule = new EasyRule(trigger, this.bot);
1017
1395
  this.bot.rules.push(newRule);
1018
- return newRule;
1396
+ this.bot.chain.currentRule = newRule; // Set new rule as current
1397
+ return this.bot.chain; // ✅ KRITISCH: Gibt Chain zurück für Fluent API!
1019
1398
  }
1020
1399
 
1021
1400
  // End action chaining, return bot for new rules
@@ -1028,6 +1407,93 @@ class EasyRule {
1028
1407
  return this.bot;
1029
1408
  }
1030
1409
 
1410
+ // ✅ KRITISCH: start() Methode hinzufügen!
1411
+ start() {
1412
+ return this.bot.start(); // Delegiere zu EasyBot.start()
1413
+ }
1414
+
1415
+ // ✅ FEHLENDE METHODEN: Delegiere zu EasyBot
1416
+ command(cmd, response) {
1417
+ return this.bot.command(cmd, response);
1418
+ }
1419
+
1420
+ autoReply(trigger, response) {
1421
+ return this.bot.autoReply(trigger, response);
1422
+ }
1423
+
1424
+ template(name, content) {
1425
+ return this.bot.template(name, content);
1426
+ }
1427
+
1428
+ enableDefaults() {
1429
+ return this.bot.enableDefaults();
1430
+ }
1431
+
1432
+ enableAll() {
1433
+ return this.bot.enableAll();
1434
+ }
1435
+
1436
+ status() {
1437
+ return this.bot.status();
1438
+ }
1439
+
1440
+ stop() {
1441
+ return this.bot.stop();
1442
+ }
1443
+
1444
+ // ✅ ADVANCED FEATURES: Delegiere zu EasyBot
1445
+ enableAI() {
1446
+ return this.bot.enableAI();
1447
+ }
1448
+
1449
+ enableWeather() {
1450
+ return this.bot.enableWeather();
1451
+ }
1452
+
1453
+ enableCrypto() {
1454
+ return this.bot.enableCrypto();
1455
+ }
1456
+
1457
+ enableNews() {
1458
+ return this.bot.enableNews();
1459
+ }
1460
+
1461
+ enableFun() {
1462
+ return this.bot.enableFun();
1463
+ }
1464
+
1465
+ enableStorage() {
1466
+ return this.bot.enableStorage();
1467
+ }
1468
+
1469
+ enableScheduler() {
1470
+ return this.bot.enableScheduler();
1471
+ }
1472
+
1473
+ enableMultiDevice() {
1474
+ return this.bot.enableMultiDevice();
1475
+ }
1476
+
1477
+ enableTyping() {
1478
+ return this.bot.enableTyping();
1479
+ }
1480
+
1481
+ enableAutoRestart() {
1482
+ return this.bot.enableAutoRestart();
1483
+ }
1484
+
1485
+ enableReactions() {
1486
+ return this.bot.enableReactions();
1487
+ }
1488
+
1489
+ ignoreOfflineMessages() {
1490
+ return this.bot.ignoreOfflineMessages();
1491
+ }
1492
+
1493
+ enableRobustConnection() {
1494
+ return this.bot.enableRobustConnection();
1495
+ }
1496
+
1031
1497
  // Matching
1032
1498
  matches(msg) {
1033
1499
  const text = msg.text?.toLowerCase() || '';
@@ -1064,6 +1530,21 @@ class EasyRule {
1064
1530
  await this.bot.typeMessage(msg, action.value);
1065
1531
  break;
1066
1532
 
1533
+ case 'quickType':
1534
+ await this.bot.typeMessage(msg, 1000); // 1 Sekunde
1535
+ await this.bot.sendReply(msg, action.value);
1536
+ break;
1537
+
1538
+ case 'normalType':
1539
+ await this.bot.typeMessage(msg, 2000); // 2 Sekunden
1540
+ await this.bot.sendReply(msg, action.value);
1541
+ break;
1542
+
1543
+ case 'slowType':
1544
+ await this.bot.typeMessage(msg, 4000); // 4 Sekunden
1545
+ await this.bot.sendReply(msg, action.value);
1546
+ break;
1547
+
1067
1548
  case 'template':
1068
1549
  const template = this.bot.templates.get(action.value);
1069
1550
  if (template) {