natureco-cli 1.0.14 → 1.0.16
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 +17 -0
- package/bin/natureco.js +42 -0
- package/package.json +1 -1
- package/src/commands/dashboard.js +687 -0
- package/src/commands/discord.js +159 -0
- package/src/commands/doctor.js +362 -0
- package/src/commands/setup.js +259 -0
- package/src/commands/slack.js +164 -0
- package/src/commands/whatsapp.js +190 -0
package/src/commands/setup.js
CHANGED
|
@@ -370,6 +370,238 @@ async function setup() {
|
|
|
370
370
|
}
|
|
371
371
|
}
|
|
372
372
|
|
|
373
|
+
// Discord entegrasyonu (NatureCo seçildiyse veya NatureCo key varsa)
|
|
374
|
+
let discordToken = null;
|
|
375
|
+
|
|
376
|
+
if (naturecoApiKey && selectedBotId) {
|
|
377
|
+
// Kayıtlı Discord botu varsa hatırlat
|
|
378
|
+
if (existingConfig.discordToken) {
|
|
379
|
+
process.stdin.resume();
|
|
380
|
+
const { useExistingDiscord } = await inquirer.prompt([
|
|
381
|
+
{
|
|
382
|
+
type: 'confirm',
|
|
383
|
+
name: 'useExistingDiscord',
|
|
384
|
+
message: 'Kayıtlı Discord botu bulundu. Bunu kullanmak ister misiniz?',
|
|
385
|
+
default: true,
|
|
386
|
+
},
|
|
387
|
+
]);
|
|
388
|
+
|
|
389
|
+
if (useExistingDiscord) {
|
|
390
|
+
discordToken = existingConfig.discordToken;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
if (!discordToken) {
|
|
395
|
+
process.stdin.resume();
|
|
396
|
+
const { wantDiscord } = await inquirer.prompt([
|
|
397
|
+
{
|
|
398
|
+
type: 'confirm',
|
|
399
|
+
name: 'wantDiscord',
|
|
400
|
+
message: 'Discord entegrasyonu eklemek ister misiniz?',
|
|
401
|
+
default: false,
|
|
402
|
+
},
|
|
403
|
+
]);
|
|
404
|
+
|
|
405
|
+
if (wantDiscord) {
|
|
406
|
+
process.stdin.resume();
|
|
407
|
+
const discordAnswers = await inquirer.prompt([
|
|
408
|
+
{
|
|
409
|
+
type: 'input',
|
|
410
|
+
name: 'token',
|
|
411
|
+
message: 'Discord bot token:',
|
|
412
|
+
validate: (val) => val.trim() !== '' || 'Token boş olamaz',
|
|
413
|
+
},
|
|
414
|
+
]);
|
|
415
|
+
|
|
416
|
+
discordToken = discordAnswers.token.trim();
|
|
417
|
+
|
|
418
|
+
// Discord'u bağla
|
|
419
|
+
try {
|
|
420
|
+
const response = await fetch('https://api.natureco.me/api/agent/discord/connect', {
|
|
421
|
+
method: 'POST',
|
|
422
|
+
headers: {
|
|
423
|
+
'Content-Type': 'application/json',
|
|
424
|
+
'Authorization': `Bearer ${naturecoApiKey}`,
|
|
425
|
+
},
|
|
426
|
+
body: JSON.stringify({
|
|
427
|
+
agent_id: selectedBotId,
|
|
428
|
+
discord_bot_token: discordToken,
|
|
429
|
+
}),
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
if (response.ok) {
|
|
433
|
+
console.log(chalk.green('\n✅ Discord bağlantısı başarılı!\n'));
|
|
434
|
+
} else {
|
|
435
|
+
console.log(chalk.yellow('\n⚠️ Discord bağlantısı kurulamadı\n'));
|
|
436
|
+
}
|
|
437
|
+
} catch (err) {
|
|
438
|
+
console.log(chalk.yellow(`\n⚠️ Discord bağlantı hatası: ${err.message}\n`));
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Slack entegrasyonu (NatureCo seçildiyse veya NatureCo key varsa)
|
|
445
|
+
let slackToken = null;
|
|
446
|
+
|
|
447
|
+
if (naturecoApiKey && selectedBotId) {
|
|
448
|
+
// Kayıtlı Slack botu varsa hatırlat
|
|
449
|
+
if (existingConfig.slackToken) {
|
|
450
|
+
process.stdin.resume();
|
|
451
|
+
const { useExistingSlack } = await inquirer.prompt([
|
|
452
|
+
{
|
|
453
|
+
type: 'confirm',
|
|
454
|
+
name: 'useExistingSlack',
|
|
455
|
+
message: 'Kayıtlı Slack botu bulundu. Bunu kullanmak ister misiniz?',
|
|
456
|
+
default: true,
|
|
457
|
+
},
|
|
458
|
+
]);
|
|
459
|
+
|
|
460
|
+
if (useExistingSlack) {
|
|
461
|
+
slackToken = existingConfig.slackToken;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (!slackToken) {
|
|
466
|
+
process.stdin.resume();
|
|
467
|
+
const { wantSlack } = await inquirer.prompt([
|
|
468
|
+
{
|
|
469
|
+
type: 'confirm',
|
|
470
|
+
name: 'wantSlack',
|
|
471
|
+
message: 'Slack entegrasyonu eklemek ister misiniz?',
|
|
472
|
+
default: false,
|
|
473
|
+
},
|
|
474
|
+
]);
|
|
475
|
+
|
|
476
|
+
if (wantSlack) {
|
|
477
|
+
process.stdin.resume();
|
|
478
|
+
const slackAnswers = await inquirer.prompt([
|
|
479
|
+
{
|
|
480
|
+
type: 'input',
|
|
481
|
+
name: 'token',
|
|
482
|
+
message: 'Slack bot token (xoxb- ile başlar):',
|
|
483
|
+
validate: (val) => {
|
|
484
|
+
const trimmed = val.trim();
|
|
485
|
+
if (trimmed === '') return 'Token boş olamaz';
|
|
486
|
+
if (!trimmed.startsWith('xoxb-')) return 'Slack bot token xoxb- ile başlamalı';
|
|
487
|
+
return true;
|
|
488
|
+
},
|
|
489
|
+
},
|
|
490
|
+
]);
|
|
491
|
+
|
|
492
|
+
slackToken = slackAnswers.token.trim();
|
|
493
|
+
|
|
494
|
+
// Slack'i bağla
|
|
495
|
+
try {
|
|
496
|
+
const response = await fetch('https://api.natureco.me/api/agent/slack/connect', {
|
|
497
|
+
method: 'POST',
|
|
498
|
+
headers: {
|
|
499
|
+
'Content-Type': 'application/json',
|
|
500
|
+
'Authorization': `Bearer ${naturecoApiKey}`,
|
|
501
|
+
},
|
|
502
|
+
body: JSON.stringify({
|
|
503
|
+
agent_id: selectedBotId,
|
|
504
|
+
slack_bot_token: slackToken,
|
|
505
|
+
}),
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
if (response.ok) {
|
|
509
|
+
console.log(chalk.green('\n✅ Slack bağlantısı başarılı!\n'));
|
|
510
|
+
} else {
|
|
511
|
+
console.log(chalk.yellow('\n⚠️ Slack bağlantısı kurulamadı\n'));
|
|
512
|
+
}
|
|
513
|
+
} catch (err) {
|
|
514
|
+
console.log(chalk.yellow(`\n⚠️ Slack bağlantı hatası: ${err.message}\n`));
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// WhatsApp entegrasyonu (NatureCo seçildiyse veya NatureCo key varsa)
|
|
521
|
+
let whatsappConnected = false;
|
|
522
|
+
|
|
523
|
+
if (naturecoApiKey && selectedBotId) {
|
|
524
|
+
// Kayıtlı WhatsApp bağlantısı varsa hatırlat
|
|
525
|
+
if (existingConfig.whatsappConnected) {
|
|
526
|
+
process.stdin.resume();
|
|
527
|
+
const { useExistingWhatsApp } = await inquirer.prompt([
|
|
528
|
+
{
|
|
529
|
+
type: 'confirm',
|
|
530
|
+
name: 'useExistingWhatsApp',
|
|
531
|
+
message: 'Kayıtlı WhatsApp bağlantısı bulundu. Bunu kullanmak ister misiniz?',
|
|
532
|
+
default: true,
|
|
533
|
+
},
|
|
534
|
+
]);
|
|
535
|
+
|
|
536
|
+
if (useExistingWhatsApp) {
|
|
537
|
+
whatsappConnected = true;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
if (!whatsappConnected) {
|
|
542
|
+
process.stdin.resume();
|
|
543
|
+
const { wantWhatsApp } = await inquirer.prompt([
|
|
544
|
+
{
|
|
545
|
+
type: 'confirm',
|
|
546
|
+
name: 'wantWhatsApp',
|
|
547
|
+
message: 'WhatsApp entegrasyonu eklemek ister misiniz?',
|
|
548
|
+
default: false,
|
|
549
|
+
},
|
|
550
|
+
]);
|
|
551
|
+
|
|
552
|
+
if (wantWhatsApp) {
|
|
553
|
+
console.log(chalk.cyan('\n📱 WhatsApp bağlantısı QR kod ile yapılır.'));
|
|
554
|
+
console.log(chalk.gray('Telefonunuzda WhatsApp\'ı açın ve QR kodu taratın.\n'));
|
|
555
|
+
console.log(chalk.yellow('⏳ QR kod alınıyor...\n'));
|
|
556
|
+
|
|
557
|
+
// WhatsApp'ı bağla
|
|
558
|
+
try {
|
|
559
|
+
const response = await fetch('https://api.natureco.me/api/agent/whatsapp/connect', {
|
|
560
|
+
method: 'POST',
|
|
561
|
+
headers: {
|
|
562
|
+
'Content-Type': 'application/json',
|
|
563
|
+
'Authorization': `Bearer ${naturecoApiKey}`,
|
|
564
|
+
},
|
|
565
|
+
body: JSON.stringify({
|
|
566
|
+
agent_id: selectedBotId,
|
|
567
|
+
}),
|
|
568
|
+
});
|
|
569
|
+
|
|
570
|
+
if (response.ok) {
|
|
571
|
+
const data = await response.json();
|
|
572
|
+
|
|
573
|
+
if (data.qr_code) {
|
|
574
|
+
console.log(chalk.green('✅ QR kod hazır!\n'));
|
|
575
|
+
console.log(chalk.cyan('QR Kod:'));
|
|
576
|
+
console.log(data.qr_code);
|
|
577
|
+
console.log('');
|
|
578
|
+
|
|
579
|
+
if (data.qr_url) {
|
|
580
|
+
console.log(chalk.cyan('QR URL:'), chalk.white(data.qr_url));
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
console.log(chalk.gray('\n1. WhatsApp\'ı açın'));
|
|
584
|
+
console.log(chalk.gray('2. Ayarlar > Bağlı Cihazlar > Cihaz Bağla'));
|
|
585
|
+
console.log(chalk.gray('3. Bu QR kodu taratın\n'));
|
|
586
|
+
} else if (data.connection_url) {
|
|
587
|
+
console.log(chalk.green('✅ Bağlantı linki hazır!\n'));
|
|
588
|
+
console.log(chalk.cyan('Bağlantı URL:'), chalk.white(data.connection_url));
|
|
589
|
+
console.log(chalk.gray('\nBu linki tarayıcıda açın ve QR kodu taratın.\n'));
|
|
590
|
+
} else {
|
|
591
|
+
console.log(chalk.green('✅ WhatsApp bağlantısı başlatıldı!\n'));
|
|
592
|
+
}
|
|
593
|
+
|
|
594
|
+
whatsappConnected = true;
|
|
595
|
+
} else {
|
|
596
|
+
console.log(chalk.yellow('\n⚠️ WhatsApp bağlantısı kurulamadı\n'));
|
|
597
|
+
}
|
|
598
|
+
} catch (err) {
|
|
599
|
+
console.log(chalk.yellow(`\n⚠️ WhatsApp bağlantı hatası: ${err.message}\n`));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
|
|
373
605
|
// Config kaydet
|
|
374
606
|
const config = {
|
|
375
607
|
setupCompleted: true,
|
|
@@ -397,6 +629,21 @@ async function setup() {
|
|
|
397
629
|
config.telegramUserId = telegramUserId;
|
|
398
630
|
}
|
|
399
631
|
|
|
632
|
+
if (discordToken) {
|
|
633
|
+
config.discordToken = discordToken;
|
|
634
|
+
config.discordBotId = selectedBotId;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
if (slackToken) {
|
|
638
|
+
config.slackToken = slackToken;
|
|
639
|
+
config.slackBotId = selectedBotId;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
if (whatsappConnected) {
|
|
643
|
+
config.whatsappConnected = true;
|
|
644
|
+
config.whatsappBotId = selectedBotId;
|
|
645
|
+
}
|
|
646
|
+
|
|
400
647
|
saveConfig(config);
|
|
401
648
|
|
|
402
649
|
// Kurulum tamamlandı
|
|
@@ -413,6 +660,18 @@ async function setup() {
|
|
|
413
660
|
if (telegramToken) {
|
|
414
661
|
console.log(chalk.cyan('Telegram:'), chalk.green('✓ Bağlı'));
|
|
415
662
|
}
|
|
663
|
+
|
|
664
|
+
if (discordToken) {
|
|
665
|
+
console.log(chalk.cyan('Discord:'), chalk.green('✓ Bağlı'));
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
if (slackToken) {
|
|
669
|
+
console.log(chalk.cyan('Slack:'), chalk.green('✓ Bağlı'));
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (whatsappConnected) {
|
|
673
|
+
console.log(chalk.cyan('WhatsApp:'), chalk.green('✓ Bağlı'));
|
|
674
|
+
}
|
|
416
675
|
|
|
417
676
|
console.log(chalk.cyan('Config:'), chalk.white(CONFIG_FILE));
|
|
418
677
|
console.log('');
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
const inquirer = require('inquirer');
|
|
3
|
+
const { getApiKey, getConfig, saveConfig } = require('../utils/config');
|
|
4
|
+
const { getBots } = require('../utils/api');
|
|
5
|
+
|
|
6
|
+
async function slack(action) {
|
|
7
|
+
if (!action || action === 'connect') {
|
|
8
|
+
return connectSlack();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (action === 'disconnect') {
|
|
12
|
+
return disconnectSlack();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (action === 'status') {
|
|
16
|
+
return statusSlack();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log(chalk.red('\n❌ Unknown action\n'));
|
|
20
|
+
console.log(chalk.gray('Available actions: connect, disconnect, status\n'));
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function connectSlack() {
|
|
25
|
+
const apiKey = getApiKey();
|
|
26
|
+
|
|
27
|
+
if (!apiKey) {
|
|
28
|
+
console.log(chalk.red('\n❌ Not logged in. Run "natureco login" first.\n'));
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const config = getConfig();
|
|
33
|
+
|
|
34
|
+
console.log(chalk.yellow('\n⏳ Loading bots...\n'));
|
|
35
|
+
|
|
36
|
+
let botList;
|
|
37
|
+
try {
|
|
38
|
+
botList = await getBots(apiKey);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.log(chalk.red(`\n❌ Error: ${err.message}\n`));
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!botList || !botList.bots || botList.bots.length === 0) {
|
|
45
|
+
console.log(chalk.gray('No bots found. Create one at https://developers.natureco.me\n'));
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
process.stdin.resume();
|
|
50
|
+
|
|
51
|
+
const answers = await inquirer.prompt([
|
|
52
|
+
{
|
|
53
|
+
type: 'list',
|
|
54
|
+
name: 'botId',
|
|
55
|
+
message: 'Select bot to connect:',
|
|
56
|
+
choices: botList.bots.map(b => ({ name: b.name, value: b.id })),
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
type: 'input',
|
|
60
|
+
name: 'token',
|
|
61
|
+
message: 'Slack bot token (starts with xoxb-):',
|
|
62
|
+
validate: (val) => {
|
|
63
|
+
const trimmed = val.trim();
|
|
64
|
+
if (trimmed === '') return 'Token cannot be empty';
|
|
65
|
+
if (!trimmed.startsWith('xoxb-')) return 'Slack bot token must start with xoxb-';
|
|
66
|
+
return true;
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
const selectedBot = botList.bots.find(b => b.id === answers.botId);
|
|
72
|
+
|
|
73
|
+
console.log(chalk.yellow('\n⏳ Connecting to Slack...\n'));
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const response = await fetch('https://api.natureco.me/api/agent/slack/connect', {
|
|
77
|
+
method: 'POST',
|
|
78
|
+
headers: {
|
|
79
|
+
'Content-Type': 'application/json',
|
|
80
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
81
|
+
},
|
|
82
|
+
body: JSON.stringify({
|
|
83
|
+
agent_id: answers.botId,
|
|
84
|
+
slack_bot_token: answers.token.trim(),
|
|
85
|
+
}),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
if (!response.ok) {
|
|
89
|
+
const error = await response.text();
|
|
90
|
+
throw new Error(error);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const data = await response.json();
|
|
94
|
+
|
|
95
|
+
console.log(chalk.green('✅ Slack connected successfully!\n'));
|
|
96
|
+
console.log(chalk.cyan('Bot:'), chalk.white(selectedBot.name));
|
|
97
|
+
console.log(chalk.cyan('Slack Bot:'), chalk.white(data.bot_name || 'Unknown'));
|
|
98
|
+
console.log(chalk.gray('\nYour bot is now active on Slack.'));
|
|
99
|
+
console.log(chalk.gray('Users can mention the bot or use slash commands.\n'));
|
|
100
|
+
|
|
101
|
+
// Save to config
|
|
102
|
+
config.slackToken = answers.token.trim();
|
|
103
|
+
config.slackBotId = answers.botId;
|
|
104
|
+
saveConfig(config);
|
|
105
|
+
} catch (err) {
|
|
106
|
+
console.log(chalk.red(`\n❌ Connection failed: ${err.message}\n`));
|
|
107
|
+
process.exit(1);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async function disconnectSlack() {
|
|
112
|
+
const config = getConfig();
|
|
113
|
+
|
|
114
|
+
if (!config.slackToken) {
|
|
115
|
+
console.log(chalk.gray('\n⚠️ No Slack connection found\n'));
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
process.stdin.resume();
|
|
120
|
+
|
|
121
|
+
const { confirm } = await inquirer.prompt([
|
|
122
|
+
{
|
|
123
|
+
type: 'confirm',
|
|
124
|
+
name: 'confirm',
|
|
125
|
+
message: 'Are you sure you want to disconnect Slack?',
|
|
126
|
+
default: false,
|
|
127
|
+
},
|
|
128
|
+
]);
|
|
129
|
+
|
|
130
|
+
if (!confirm) {
|
|
131
|
+
console.log(chalk.gray('\nCancelled\n'));
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Remove from config
|
|
136
|
+
delete config.slackToken;
|
|
137
|
+
delete config.slackBotId;
|
|
138
|
+
saveConfig(config);
|
|
139
|
+
|
|
140
|
+
console.log(chalk.green('\n✅ Slack disconnected\n'));
|
|
141
|
+
console.log(chalk.gray('Note: The bot is still registered on Slack.'));
|
|
142
|
+
console.log(chalk.gray('You may need to manually remove it from Slack App settings.\n'));
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function statusSlack() {
|
|
146
|
+
const config = getConfig();
|
|
147
|
+
|
|
148
|
+
if (!config.slackToken) {
|
|
149
|
+
console.log(chalk.gray('\n⚠️ Slack not connected\n'));
|
|
150
|
+
console.log(chalk.gray('Connect with: natureco slack connect\n'));
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
console.log(chalk.green('\n✅ Slack connected\n'));
|
|
155
|
+
console.log(chalk.cyan('Token:'), chalk.white(config.slackToken.slice(0, 20) + '...'));
|
|
156
|
+
|
|
157
|
+
if (config.slackBotId) {
|
|
158
|
+
console.log(chalk.cyan('Bot ID:'), chalk.white(config.slackBotId));
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
console.log(chalk.gray('\nDisconnect with: natureco slack disconnect\n'));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
module.exports = slack;
|
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
const inquirer = require('inquirer');
|
|
3
|
+
const { getApiKey, getConfig, saveConfig } = require('../utils/config');
|
|
4
|
+
const { getBots } = require('../utils/api');
|
|
5
|
+
|
|
6
|
+
async function whatsapp(action) {
|
|
7
|
+
if (!action || action === 'connect') {
|
|
8
|
+
return connectWhatsApp();
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
if (action === 'disconnect') {
|
|
12
|
+
return disconnectWhatsApp();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
if (action === 'status') {
|
|
16
|
+
return statusWhatsApp();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
console.log(chalk.red('\n❌ Unknown action\n'));
|
|
20
|
+
console.log(chalk.gray('Available actions: connect, disconnect, status\n'));
|
|
21
|
+
process.exit(1);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function connectWhatsApp() {
|
|
25
|
+
const apiKey = getApiKey();
|
|
26
|
+
|
|
27
|
+
if (!apiKey) {
|
|
28
|
+
console.log(chalk.red('\n❌ Not logged in. Run "natureco login" first.\n'));
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const config = getConfig();
|
|
33
|
+
|
|
34
|
+
console.log(chalk.yellow('\n⏳ Loading bots...\n'));
|
|
35
|
+
|
|
36
|
+
let botList;
|
|
37
|
+
try {
|
|
38
|
+
botList = await getBots(apiKey);
|
|
39
|
+
} catch (err) {
|
|
40
|
+
console.log(chalk.red(`\n❌ Error: ${err.message}\n`));
|
|
41
|
+
process.exit(1);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (!botList || !botList.bots || botList.bots.length === 0) {
|
|
45
|
+
console.log(chalk.gray('No bots found. Create one at https://developers.natureco.me\n'));
|
|
46
|
+
process.exit(1);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
process.stdin.resume();
|
|
50
|
+
|
|
51
|
+
const { botId } = await inquirer.prompt([
|
|
52
|
+
{
|
|
53
|
+
type: 'list',
|
|
54
|
+
name: 'botId',
|
|
55
|
+
message: 'Select bot to connect:',
|
|
56
|
+
choices: botList.bots.map(b => ({ name: b.name, value: b.id })),
|
|
57
|
+
},
|
|
58
|
+
]);
|
|
59
|
+
|
|
60
|
+
const selectedBot = botList.bots.find(b => b.id === botId);
|
|
61
|
+
|
|
62
|
+
console.log(chalk.cyan('\n📱 WhatsApp bağlantısı QR kod ile yapılır.'));
|
|
63
|
+
console.log(chalk.gray('Telefonunuzda WhatsApp\'ı açın ve QR kodu taratın.\n'));
|
|
64
|
+
console.log(chalk.yellow('⏳ QR kod alınıyor...\n'));
|
|
65
|
+
|
|
66
|
+
try {
|
|
67
|
+
const response = await fetch('https://api.natureco.me/api/agent/whatsapp/connect', {
|
|
68
|
+
method: 'POST',
|
|
69
|
+
headers: {
|
|
70
|
+
'Content-Type': 'application/json',
|
|
71
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
72
|
+
},
|
|
73
|
+
body: JSON.stringify({
|
|
74
|
+
agent_id: botId,
|
|
75
|
+
}),
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
const error = await response.text();
|
|
80
|
+
throw new Error(error);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const data = await response.json();
|
|
84
|
+
|
|
85
|
+
if (data.qr_code) {
|
|
86
|
+
console.log(chalk.green('✅ QR kod hazır!\n'));
|
|
87
|
+
console.log(chalk.cyan('QR Kod:'));
|
|
88
|
+
console.log(data.qr_code);
|
|
89
|
+
console.log('');
|
|
90
|
+
|
|
91
|
+
if (data.qr_url) {
|
|
92
|
+
console.log(chalk.cyan('QR URL:'), chalk.white(data.qr_url));
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
console.log(chalk.gray('\n1. WhatsApp\'ı açın'));
|
|
96
|
+
console.log(chalk.gray('2. Ayarlar > Bağlı Cihazlar > Cihaz Bağla'));
|
|
97
|
+
console.log(chalk.gray('3. Bu QR kodu taratın\n'));
|
|
98
|
+
} else if (data.connection_url) {
|
|
99
|
+
console.log(chalk.green('✅ Bağlantı linki hazır!\n'));
|
|
100
|
+
console.log(chalk.cyan('Bağlantı URL:'), chalk.white(data.connection_url));
|
|
101
|
+
console.log(chalk.gray('\nBu linki tarayıcıda açın ve QR kodu taratın.\n'));
|
|
102
|
+
} else {
|
|
103
|
+
console.log(chalk.green('✅ WhatsApp bağlantısı başlatıldı!\n'));
|
|
104
|
+
console.log(chalk.gray('Bağlantı durumunu kontrol edin: natureco whatsapp status\n'));
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// Save to config
|
|
108
|
+
config.whatsappConnected = true;
|
|
109
|
+
config.whatsappBotId = botId;
|
|
110
|
+
saveConfig(config);
|
|
111
|
+
|
|
112
|
+
console.log(chalk.cyan('Bot:'), chalk.white(selectedBot.name));
|
|
113
|
+
console.log(chalk.gray('Bağlantı tamamlandığında botunuz WhatsApp\'ta aktif olacak.\n'));
|
|
114
|
+
} catch (err) {
|
|
115
|
+
console.log(chalk.red(`\n❌ Connection failed: ${err.message}\n`));
|
|
116
|
+
process.exit(1);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
async function disconnectWhatsApp() {
|
|
121
|
+
const config = getConfig();
|
|
122
|
+
|
|
123
|
+
if (!config.whatsappConnected) {
|
|
124
|
+
console.log(chalk.gray('\n⚠️ No WhatsApp connection found\n'));
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
process.stdin.resume();
|
|
129
|
+
|
|
130
|
+
const { confirm } = await inquirer.prompt([
|
|
131
|
+
{
|
|
132
|
+
type: 'confirm',
|
|
133
|
+
name: 'confirm',
|
|
134
|
+
message: 'Are you sure you want to disconnect WhatsApp?',
|
|
135
|
+
default: false,
|
|
136
|
+
},
|
|
137
|
+
]);
|
|
138
|
+
|
|
139
|
+
if (!confirm) {
|
|
140
|
+
console.log(chalk.gray('\nCancelled\n'));
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
const apiKey = getApiKey();
|
|
145
|
+
|
|
146
|
+
if (apiKey && config.whatsappBotId) {
|
|
147
|
+
try {
|
|
148
|
+
await fetch('https://api.natureco.me/api/agent/whatsapp/disconnect', {
|
|
149
|
+
method: 'POST',
|
|
150
|
+
headers: {
|
|
151
|
+
'Content-Type': 'application/json',
|
|
152
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
153
|
+
},
|
|
154
|
+
body: JSON.stringify({
|
|
155
|
+
agent_id: config.whatsappBotId,
|
|
156
|
+
}),
|
|
157
|
+
});
|
|
158
|
+
} catch (err) {
|
|
159
|
+
console.log(chalk.yellow(`\n⚠️ API disconnect failed: ${err.message}`));
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// Remove from config
|
|
164
|
+
delete config.whatsappConnected;
|
|
165
|
+
delete config.whatsappBotId;
|
|
166
|
+
saveConfig(config);
|
|
167
|
+
|
|
168
|
+
console.log(chalk.green('\n✅ WhatsApp disconnected\n'));
|
|
169
|
+
console.log(chalk.gray('Note: You may need to manually remove the device from WhatsApp settings.\n'));
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function statusWhatsApp() {
|
|
173
|
+
const config = getConfig();
|
|
174
|
+
|
|
175
|
+
if (!config.whatsappConnected) {
|
|
176
|
+
console.log(chalk.gray('\n⚠️ WhatsApp not connected\n'));
|
|
177
|
+
console.log(chalk.gray('Connect with: natureco whatsapp connect\n'));
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
console.log(chalk.green('\n✅ WhatsApp connected\n'));
|
|
182
|
+
|
|
183
|
+
if (config.whatsappBotId) {
|
|
184
|
+
console.log(chalk.cyan('Bot ID:'), chalk.white(config.whatsappBotId));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
console.log(chalk.gray('\nDisconnect with: natureco whatsapp disconnect\n'));
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
module.exports = whatsapp;
|