waengine 1.0.1

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.
@@ -0,0 +1,744 @@
1
+ import { WhatsAppClient } from "./client.js";
2
+ import { MultiWhatsAppClient } from "./multi-client.js";
3
+
4
+ export class EasyBot {
5
+ constructor() {
6
+ this.client = null;
7
+ this.multiClient = null;
8
+ this.rules = [];
9
+ this.commands = new Map();
10
+ this.templates = new Map();
11
+ this.autoResponses = new Map();
12
+ this.isRunning = false;
13
+ this.isMultiDevice = false;
14
+
15
+ // Easy settings
16
+ this.settings = {
17
+ autoGreeting: true,
18
+ autoHelp: true,
19
+ typingEnabled: true,
20
+ reactionsEnabled: true
21
+ };
22
+ }
23
+
24
+ // ===== FACTORY METHODS =====
25
+
26
+ static create(options = {}) {
27
+ const bot = new EasyBot();
28
+ bot.client = new WhatsAppClient({
29
+ authDir: "./auth",
30
+ logLevel: "silent",
31
+ ...options
32
+ });
33
+ return bot;
34
+ }
35
+
36
+ static createMulti(deviceCount = 2, options = {}) {
37
+ const bot = new EasyBot();
38
+ bot.isMultiDevice = true;
39
+ bot.multiClient = new MultiWhatsAppClient({
40
+ maxDevices: deviceCount,
41
+ loadBalancing: 'round-robin',
42
+ ...options
43
+ });
44
+ return bot;
45
+ }
46
+
47
+ static quick() {
48
+ return EasyBot.create().enableDefaults();
49
+ }
50
+
51
+ // ===== SIMPLE RULES =====
52
+
53
+ when(trigger) {
54
+ const rule = new EasyRule(trigger, this);
55
+ this.rules.push(rule);
56
+ return rule;
57
+ }
58
+
59
+ // Shorthand methods
60
+ onText(text, response) {
61
+ return this.when(text).reply(response);
62
+ }
63
+
64
+ onContains(word, response) {
65
+ return this.when(word).reply(response);
66
+ }
67
+
68
+ onCommand(cmd, response) {
69
+ return this.command(cmd, response);
70
+ }
71
+
72
+ // ===== SIMPLE COMMANDS =====
73
+
74
+ command(cmd, response) {
75
+ this.commands.set(cmd.toLowerCase(), response);
76
+ return this;
77
+ }
78
+
79
+ // ===== AUTO RESPONSES =====
80
+
81
+ autoReply(trigger, response) {
82
+ this.autoResponses.set(trigger.toLowerCase(), response);
83
+ return this;
84
+ }
85
+
86
+ // ===== TEMPLATES =====
87
+
88
+ template(name, text) {
89
+ this.templates.set(name, text);
90
+ return this;
91
+ }
92
+
93
+ // Enhanced template processing
94
+ processTemplate(templateText, msg) {
95
+ const sender = msg.from.split('@')[0];
96
+ const now = new Date();
97
+
98
+ return templateText
99
+ .replace('{name}', sender)
100
+ .replace('{sender}', sender)
101
+ .replace('{time}', now.toLocaleTimeString('de-DE'))
102
+ .replace('{date}', now.toLocaleDateString('de-DE'))
103
+ .replace('{datetime}', now.toLocaleString('de-DE'))
104
+ .replace('{day}', now.toLocaleDateString('de-DE', { weekday: 'long' }))
105
+ .replace('{chat}', msg.isGroup ? 'Gruppe' : 'Privat');
106
+ }
107
+
108
+ // ===== CONDITIONAL LOGIC =====
109
+
110
+ if(condition) {
111
+ return new EasyCondition(condition, this);
112
+ }
113
+
114
+ // ===== EASY SETUP METHODS =====
115
+
116
+ enableDefaults() {
117
+ // Standard Antworten
118
+ this.autoReply("hallo", "Hallo! 👋 Wie kann ich dir helfen?");
119
+ this.autoReply("hi", "Hi! 😊");
120
+ this.autoReply("hey", "Hey! 🙋‍♂️");
121
+ this.autoReply("tschüss", "Tschüss! 👋 Bis bald!");
122
+ this.autoReply("bye", "Bye! 👋");
123
+
124
+ // Standard Commands
125
+ this.command("hilfe", "🤖 Verfügbare Commands:\n!hilfe - Diese Hilfe\n!ping - Ping Test\n!zeit - Aktuelle Zeit");
126
+ this.command("ping", "Pong! 🏓");
127
+ this.command("zeit", () => `🕐 ${new Date().toLocaleString('de-DE')}`);
128
+
129
+ return this;
130
+ }
131
+
132
+ enableMultiDevice(deviceCount = 2) {
133
+ if (!this.isMultiDevice) {
134
+ this.isMultiDevice = true;
135
+ this.multiClient = new MultiWhatsAppClient({
136
+ maxDevices: deviceCount,
137
+ loadBalancing: 'round-robin'
138
+ });
139
+ }
140
+ return this;
141
+ }
142
+
143
+ enableTyping(enabled = true) {
144
+ this.settings.typingEnabled = enabled;
145
+ return this;
146
+ }
147
+
148
+ enableReactions(enabled = true) {
149
+ this.settings.reactionsEnabled = enabled;
150
+ return this;
151
+ }
152
+
153
+ // ===== MULTI-DEVICE SETUP =====
154
+
155
+ async addDevice(deviceId, options = {}) {
156
+ if (!this.isMultiDevice) {
157
+ throw new Error("❌ Multi-Device nicht aktiviert! Nutze EasyBot.createMulti() oder .enableMultiDevice()");
158
+ }
159
+
160
+ await this.multiClient.addDevice(deviceId, options);
161
+ console.log(`✅ Device '${deviceId}' hinzugefügt`);
162
+ return this;
163
+ }
164
+
165
+ // ===== START BOT =====
166
+
167
+ async start() {
168
+ if (this.isRunning) {
169
+ console.log("✅ Bot läuft bereits!");
170
+ return this;
171
+ }
172
+
173
+ console.log("🚀 Starte EasyBot...");
174
+
175
+ const activeClient = this.isMultiDevice ? this.multiClient : this.client;
176
+
177
+ // Setup Commands
178
+ if (this.commands.size > 0) {
179
+ activeClient.setPrefix("!");
180
+
181
+ for (const [cmd, response] of this.commands) {
182
+ activeClient.addCommand(cmd, async (msg) => {
183
+ if (this.settings.typingEnabled) {
184
+ await this.typeMessage(msg, 1000);
185
+ }
186
+
187
+ const reply = typeof response === 'function' ? response(msg) : response;
188
+ await this.sendReply(msg, reply);
189
+ });
190
+ }
191
+ }
192
+
193
+ // Setup Message Handler
194
+ activeClient.on('message', async (msg) => {
195
+ // Skip commands
196
+ if (msg.isCommand) return;
197
+
198
+ const text = msg.text?.toLowerCase() || '';
199
+
200
+ // Auto Responses (exact match)
201
+ for (const [trigger, response] of this.autoResponses) {
202
+ if (text === trigger || text.includes(trigger)) {
203
+ if (this.settings.typingEnabled) {
204
+ await this.typeMessage(msg, 1500);
205
+ }
206
+
207
+ const reply = typeof response === 'function' ? response(msg) : response;
208
+ await this.sendReply(msg, reply);
209
+ return;
210
+ }
211
+ }
212
+
213
+ // Process rules
214
+ for (const rule of this.rules) {
215
+ if (rule.matches(msg)) {
216
+ await rule.execute(msg);
217
+ break; // Only first match
218
+ }
219
+ }
220
+ });
221
+
222
+ // Multi-Device Setup
223
+ if (this.isMultiDevice) {
224
+ // Auto-add default devices if none exist
225
+ if (this.multiClient.deviceManager.devices.size === 0) {
226
+ await this.multiClient.addDevice('bot1');
227
+ await this.multiClient.addDevice('bot2');
228
+ console.log("✅ Standard Devices (bot1, bot2) hinzugefügt");
229
+ }
230
+ }
231
+
232
+ // Connect
233
+ await activeClient.connect();
234
+ this.isRunning = true;
235
+
236
+ console.log("✅ EasyBot gestartet!");
237
+ console.log(`📝 ${this.rules.length} Regeln aktiv`);
238
+ console.log(`⚡ ${this.commands.size} Commands verfügbar`);
239
+ console.log(`🤖 ${this.autoResponses.size} Auto-Antworten aktiv`);
240
+
241
+ if (this.isMultiDevice) {
242
+ const status = this.multiClient.getStatus();
243
+ console.log(`🔧 Multi-Device: ${status.activeDevices}/${status.totalDevices} Devices`);
244
+ }
245
+
246
+ return this;
247
+ }
248
+
249
+ // ===== HELPER METHODS =====
250
+
251
+ async typeMessage(msg, duration = 1500) {
252
+ if (!this.settings.typingEnabled) return;
253
+
254
+ try {
255
+ if (this.isMultiDevice) {
256
+ await this.multiClient.setTyping(msg.from);
257
+ await new Promise(r => setTimeout(r, duration));
258
+ } else {
259
+ await msg.visualWrite(true);
260
+ await new Promise(r => setTimeout(r, duration));
261
+ await msg.visualWrite(false);
262
+ }
263
+ } catch (error) {
264
+ // Ignore typing errors
265
+ }
266
+ }
267
+
268
+ async sendReply(msg, text) {
269
+ try {
270
+ if (this.isMultiDevice) {
271
+ await this.multiClient.sendMessage(msg.from, { text });
272
+ } else {
273
+ await msg.reply(text);
274
+ }
275
+ } catch (error) {
276
+ console.error("❌ Fehler beim Senden:", error.message);
277
+ }
278
+ }
279
+
280
+ async sendReaction(msg, emoji) {
281
+ try {
282
+ if (this.isMultiDevice) {
283
+ const client = this.multiClient.getNextDevice();
284
+ await client.socket.sendMessage(msg.from, {
285
+ react: { text: emoji, key: msg.raw.key }
286
+ });
287
+ } else {
288
+ await msg.react(emoji);
289
+ }
290
+ } catch (error) {
291
+ console.error("❌ Fehler bei Reaction:", error.message);
292
+ }
293
+ }
294
+
295
+ async sendMedia(msg, type, data) {
296
+ try {
297
+ const client = this.isMultiDevice ? this.multiClient.getNextDevice() : this.client;
298
+
299
+ switch (type) {
300
+ case 'image':
301
+ if (this.isMultiDevice) {
302
+ // Use advanced client for multi-device
303
+ await client.socket.sendMessage(msg.from, {
304
+ image: { url: data.path },
305
+ caption: data.caption
306
+ });
307
+ } else {
308
+ await msg.sendImage(data.path, data.caption);
309
+ }
310
+ break;
311
+
312
+ case 'sticker':
313
+ if (this.isMultiDevice) {
314
+ await client.socket.sendMessage(msg.from, {
315
+ sticker: { url: data }
316
+ });
317
+ } else {
318
+ await msg.sendSticker(data);
319
+ }
320
+ break;
321
+
322
+ case 'location':
323
+ if (this.isMultiDevice) {
324
+ await client.socket.sendMessage(msg.from, {
325
+ location: {
326
+ degreesLatitude: data.lat,
327
+ degreesLongitude: data.lng
328
+ }
329
+ });
330
+ } else {
331
+ await msg.sendLocation(data.lat, data.lng);
332
+ }
333
+ break;
334
+ }
335
+ } catch (error) {
336
+ console.error(`❌ Fehler beim Senden von ${type}:`, error.message);
337
+ }
338
+ }
339
+
340
+ // ===== ADVANCED ACCESS =====
341
+
342
+ getAdvancedClient() {
343
+ if (this.isMultiDevice) {
344
+ return this.multiClient;
345
+ }
346
+ return this.client;
347
+ }
348
+
349
+ getWhatsAppClient() {
350
+ return this.client;
351
+ }
352
+
353
+ getMultiClient() {
354
+ return this.multiClient;
355
+ }
356
+
357
+ // ===== UTILITY =====
358
+
359
+ stop() {
360
+ const activeClient = this.isMultiDevice ? this.multiClient : this.client;
361
+ if (activeClient) {
362
+ activeClient.disconnect();
363
+ }
364
+ this.isRunning = false;
365
+ console.log("🛑 EasyBot gestoppt");
366
+ return this;
367
+ }
368
+
369
+ status() {
370
+ const baseStatus = {
371
+ running: this.isRunning,
372
+ rules: this.rules.length,
373
+ commands: this.commands.size,
374
+ templates: this.templates.size,
375
+ autoResponses: this.autoResponses.size,
376
+ multiDevice: this.isMultiDevice
377
+ };
378
+
379
+ if (this.isMultiDevice && this.multiClient) {
380
+ return {
381
+ ...baseStatus,
382
+ ...this.multiClient.getStatus()
383
+ };
384
+ }
385
+
386
+ return baseStatus;
387
+ }
388
+
389
+ // ===== QUICK METHODS =====
390
+
391
+ quickStart() {
392
+ return this.enableDefaults().start();
393
+ }
394
+
395
+ addQuickCommands() {
396
+ this.command("status", () => {
397
+ const status = this.status();
398
+ return `📊 Bot Status:\n✅ Läuft: ${status.running}\n📝 Regeln: ${status.rules}\n⚡ Commands: ${status.commands}`;
399
+ });
400
+
401
+ this.command("info", "🤖 EasyBot v1.0\n📚 Einfache WhatsApp Bot Library\n🔧 Powered by Baileys");
402
+
403
+ return this;
404
+ }
405
+ }
406
+
407
+ // ===== EASY RULE CLASS =====
408
+
409
+ class EasyRule {
410
+ constructor(trigger, bot) {
411
+ this.trigger = trigger;
412
+ this.bot = bot;
413
+ this.actions = [];
414
+ }
415
+
416
+ // Actions - Return this rule for chaining!
417
+ reply(text) {
418
+ this.actions.push({ type: 'reply', value: text });
419
+ return this; // Rule chaining!
420
+ }
421
+
422
+ send(text) {
423
+ return this.reply(text);
424
+ }
425
+
426
+ react(emoji) {
427
+ this.actions.push({ type: 'react', value: emoji });
428
+ return this; // Rule chaining!
429
+ }
430
+
431
+ type(seconds = 2) {
432
+ this.actions.push({ type: 'type', value: seconds * 1000 });
433
+ return this; // Rule chaining!
434
+ }
435
+
436
+ typeAndReply(text, seconds = 2) {
437
+ this.type(seconds);
438
+ this.reply(text);
439
+ return this; // Rule chaining!
440
+ }
441
+
442
+ useTemplate(templateName) {
443
+ this.actions.push({ type: 'template', value: templateName });
444
+ return this; // Rule chaining!
445
+ }
446
+
447
+ // Media actions
448
+ sendImage(path, caption = "") {
449
+ this.actions.push({ type: 'image', value: { path, caption } });
450
+ return this;
451
+ }
452
+
453
+ sendSticker(path) {
454
+ this.actions.push({ type: 'sticker', value: path });
455
+ return this;
456
+ }
457
+
458
+ sendLocation(lat, lng) {
459
+ this.actions.push({ type: 'location', value: { lat, lng } });
460
+ return this;
461
+ }
462
+
463
+ // Mention actions
464
+ mentionSender(text) {
465
+ this.actions.push({ type: 'mentionSender', value: text });
466
+ return this;
467
+ }
468
+
469
+ mentionAll(text) {
470
+ this.actions.push({ type: 'mentionAll', value: text });
471
+ return this;
472
+ }
473
+
474
+ mentionUser(text, userJid) {
475
+ this.actions.push({ type: 'mentionUser', value: { text, userJid } });
476
+ return this;
477
+ }
478
+
479
+ // Delete actions
480
+ deleteMessage() {
481
+ this.actions.push({ type: 'delete' });
482
+ return this;
483
+ }
484
+
485
+ deleteAfter(seconds) {
486
+ this.actions.push({ type: 'deleteAfter', value: seconds });
487
+ return this;
488
+ }
489
+
490
+ // End chaining - return bot
491
+ done() {
492
+ return this.bot;
493
+ }
494
+
495
+ // Matching
496
+ matches(msg) {
497
+ const text = msg.text?.toLowerCase() || '';
498
+ const trigger = this.trigger.toLowerCase();
499
+
500
+ // Exact match
501
+ if (text === trigger) return true;
502
+
503
+ // Contains match
504
+ if (text.includes(trigger)) return true;
505
+
506
+ // Starts with match
507
+ if (text.startsWith(trigger)) return true;
508
+
509
+ return false;
510
+ }
511
+
512
+ // Execution
513
+ async execute(msg) {
514
+ for (const action of this.actions) {
515
+ try {
516
+ switch (action.type) {
517
+ case 'reply':
518
+ await this.bot.sendReply(msg, action.value);
519
+ break;
520
+
521
+ case 'react':
522
+ if (this.bot.settings.reactionsEnabled) {
523
+ await this.bot.sendReaction(msg, action.value);
524
+ }
525
+ break;
526
+
527
+ case 'type':
528
+ await this.bot.typeMessage(msg, action.value);
529
+ break;
530
+
531
+ case 'template':
532
+ const template = this.bot.templates.get(action.value);
533
+ if (template) {
534
+ const text = this.bot.processTemplate(template, msg);
535
+ await this.bot.sendReply(msg, text);
536
+ }
537
+ break;
538
+
539
+ case 'image':
540
+ await this.bot.sendMedia(msg, 'image', action.value);
541
+ break;
542
+
543
+ case 'sticker':
544
+ await this.bot.sendMedia(msg, 'sticker', action.value);
545
+ break;
546
+
547
+ case 'location':
548
+ await this.bot.sendMedia(msg, 'location', action.value);
549
+ break;
550
+
551
+ case 'mentionSender':
552
+ if (this.bot.isMultiDevice) {
553
+ await this.bot.multiClient.sendMessage(msg.from, {
554
+ text: action.value.replace('@user', `@${msg.getSender().split('@')[0]}`),
555
+ mentions: [msg.getSender()]
556
+ });
557
+ } else {
558
+ await msg.replyWithMention(action.value, msg.getSender());
559
+ }
560
+ break;
561
+
562
+ case 'mentionAll':
563
+ if (msg.isGroup) {
564
+ if (this.bot.isMultiDevice) {
565
+ const client = this.bot.multiClient.getNextDevice();
566
+ const metadata = await client.get.GroupMetadata(msg.from);
567
+ const allMembers = metadata.participants.map(p => p.id);
568
+ const mentionText = action.value + ' ' + allMembers.map(id => `@${id.split('@')[0]}`).join(' ');
569
+
570
+ await this.bot.multiClient.sendMessage(msg.from, {
571
+ text: mentionText,
572
+ mentions: allMembers
573
+ });
574
+ } else {
575
+ await msg.mentionAll(action.value);
576
+ }
577
+ } else {
578
+ await this.bot.sendReply(msg, action.value + " (Nur in Gruppen verfügbar)");
579
+ }
580
+ break;
581
+
582
+ case 'mentionUser':
583
+ if (this.bot.isMultiDevice) {
584
+ await this.bot.multiClient.sendMessage(msg.from, {
585
+ text: action.value.text.replace('@user', `@${action.value.userJid.split('@')[0]}`),
586
+ mentions: [action.value.userJid]
587
+ });
588
+ } else {
589
+ await msg.replyWithMention(action.value.text, action.value.userJid);
590
+ }
591
+ break;
592
+
593
+ case 'delete':
594
+ if (this.bot.isMultiDevice) {
595
+ const client = this.bot.multiClient.getNextDevice();
596
+ await client.socket.sendMessage(msg.from, {
597
+ delete: msg.raw.key
598
+ });
599
+ } else {
600
+ await msg.delete();
601
+ }
602
+ break;
603
+
604
+ case 'deleteAfter':
605
+ setTimeout(async () => {
606
+ try {
607
+ if (this.bot.isMultiDevice) {
608
+ const client = this.bot.multiClient.getNextDevice();
609
+ await client.socket.sendMessage(msg.from, {
610
+ delete: msg.raw.key
611
+ });
612
+ } else {
613
+ await msg.delete();
614
+ }
615
+ } catch (error) {
616
+ console.error('❌ Fehler beim verzögerten Löschen:', error.message);
617
+ }
618
+ }, action.value * 1000);
619
+ break;
620
+ }
621
+ } catch (error) {
622
+ console.error('❌ Fehler bei Aktion:', error.message);
623
+ }
624
+ }
625
+ }
626
+ }
627
+
628
+ // ===== EASY CONDITION CLASS =====
629
+
630
+ class EasyCondition {
631
+ constructor(condition, bot) {
632
+ this.condition = condition;
633
+ this.bot = bot;
634
+ }
635
+
636
+ then(action) {
637
+ const rule = this.parseAction(action);
638
+ this.bot.rules.push(rule);
639
+ return this.bot;
640
+ }
641
+
642
+ parseAction(action) {
643
+ if (action.startsWith('reply ')) {
644
+ const text = action.substring(6);
645
+ return {
646
+ matches: (msg) => this.evaluateCondition(msg),
647
+ execute: async (msg) => await this.bot.sendReply(msg, text)
648
+ };
649
+ }
650
+
651
+ if (action.startsWith('react ')) {
652
+ const emoji = action.substring(6);
653
+ return {
654
+ matches: (msg) => this.evaluateCondition(msg),
655
+ execute: async (msg) => await this.bot.sendReaction(msg, emoji)
656
+ };
657
+ }
658
+
659
+ if (action.startsWith('type ')) {
660
+ const seconds = parseInt(action.substring(5)) || 2;
661
+ return {
662
+ matches: (msg) => this.evaluateCondition(msg),
663
+ execute: async (msg) => await this.bot.typeMessage(msg, seconds * 1000)
664
+ };
665
+ }
666
+
667
+ if (action.startsWith('template ')) {
668
+ const templateName = action.substring(9);
669
+ return {
670
+ matches: (msg) => this.evaluateCondition(msg),
671
+ execute: async (msg) => {
672
+ const template = this.bot.templates.get(templateName);
673
+ if (template) {
674
+ const text = this.bot.processTemplate(template, msg);
675
+ await this.bot.sendReply(msg, text);
676
+ }
677
+ }
678
+ };
679
+ }
680
+
681
+ return {
682
+ matches: () => false,
683
+ execute: async () => {}
684
+ };
685
+ }
686
+
687
+ evaluateCondition(msg) {
688
+ const condition = this.condition.toLowerCase();
689
+ const text = msg.text?.toLowerCase() || '';
690
+
691
+ if (condition.includes('contains ')) {
692
+ const word = condition.split('contains ')[1];
693
+ return text.includes(word);
694
+ }
695
+
696
+ if (condition.includes('starts with ')) {
697
+ const word = condition.split('starts with ')[1];
698
+ return text.startsWith(word);
699
+ }
700
+
701
+ if (condition.includes('ends with ')) {
702
+ const word = condition.split('ends with ')[1];
703
+ return text.endsWith(word);
704
+ }
705
+
706
+ if (condition === 'is group') {
707
+ return msg.isGroup;
708
+ }
709
+
710
+ if (condition === 'is private') {
711
+ return !msg.isGroup;
712
+ }
713
+
714
+ if (condition === 'has media') {
715
+ return msg.type !== 'text';
716
+ }
717
+
718
+ return false;
719
+ }
720
+ }
721
+
722
+ // ===== HELPER FUNCTIONS =====
723
+
724
+ export function createBot(options = {}) {
725
+ return EasyBot.create(options);
726
+ }
727
+
728
+ export function createMultiBot(deviceCount = 2, options = {}) {
729
+ return EasyBot.createMulti(deviceCount, options);
730
+ }
731
+
732
+ export function quickBot() {
733
+ return EasyBot.quick();
734
+ }
735
+
736
+ // ===== SHORTHAND FUNCTIONS =====
737
+
738
+ export function bot() {
739
+ return EasyBot.create();
740
+ }
741
+
742
+ export function multiBot(devices = 2) {
743
+ return EasyBot.createMulti(devices);
744
+ }