natureco-cli 1.0.22 → 1.0.23
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/package.json +2 -1
- package/src/commands/whatsapp.js +79 -37
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "natureco-cli",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.23",
|
|
4
4
|
"description": "NatureCo AI Bot Terminal Interface",
|
|
5
5
|
"main": "bin/natureco.js",
|
|
6
6
|
"bin": {
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
"chalk": "^4.1.2",
|
|
29
29
|
"commander": "^11.1.0",
|
|
30
30
|
"conf": "^10.2.0",
|
|
31
|
+
"eventsource": "^2.0.2",
|
|
31
32
|
"inquirer": "^8.2.7",
|
|
32
33
|
"node-cron": "^2.0.3",
|
|
33
34
|
"ora": "^5.4.1",
|
package/src/commands/whatsapp.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const chalk = require('chalk');
|
|
2
2
|
const inquirer = require('inquirer');
|
|
3
3
|
const qrcode = require('qrcode-terminal');
|
|
4
|
+
const EventSource = require('eventsource');
|
|
4
5
|
const { getApiKey, getConfig, saveConfig } = require('../utils/config');
|
|
5
6
|
const { getBots } = require('../utils/api');
|
|
6
7
|
|
|
@@ -62,14 +63,14 @@ async function connectWhatsApp() {
|
|
|
62
63
|
|
|
63
64
|
console.log(chalk.cyan('\n📱 WhatsApp bağlantısı QR kod ile yapılır.'));
|
|
64
65
|
console.log(chalk.gray('Telefonunuzda WhatsApp\'ı açın ve QR kodu taratın.\n'));
|
|
65
|
-
console.log(chalk.yellow('⏳
|
|
66
|
+
console.log(chalk.yellow('⏳ Session oluşturuluyor...\n'));
|
|
66
67
|
|
|
67
68
|
try {
|
|
68
|
-
|
|
69
|
+
// Create WhatsApp session
|
|
70
|
+
const response = await fetch('http://localhost:3000/connect', {
|
|
69
71
|
method: 'POST',
|
|
70
72
|
headers: {
|
|
71
73
|
'Content-Type': 'application/json',
|
|
72
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
73
74
|
},
|
|
74
75
|
body: JSON.stringify({
|
|
75
76
|
bot_id: botId,
|
|
@@ -83,42 +84,87 @@ async function connectWhatsApp() {
|
|
|
83
84
|
|
|
84
85
|
const data = await response.json();
|
|
85
86
|
|
|
86
|
-
if (data.
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
// Display QR code in terminal
|
|
90
|
-
const qrData = Buffer.from(data.qr, 'base64').toString('utf-8');
|
|
91
|
-
qrcode.generate(qrData, { small: true });
|
|
92
|
-
|
|
93
|
-
console.log('');
|
|
94
|
-
console.log(chalk.gray('1. WhatsApp\'ı açın'));
|
|
95
|
-
console.log(chalk.gray('2. Ayarlar > Bağlı Cihazlar > Cihaz Bağla'));
|
|
96
|
-
console.log(chalk.gray('3. Bu QR kodu taratın\n'));
|
|
97
|
-
|
|
98
|
-
if (data.session_id) {
|
|
99
|
-
console.log(chalk.cyan('Session ID:'), chalk.white(data.session_id));
|
|
100
|
-
|
|
101
|
-
// Save session ID to config
|
|
102
|
-
config.whatsappSessionId = data.session_id;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (data.note) {
|
|
106
|
-
console.log(chalk.yellow('\n⚠️ Note:'), chalk.gray(data.note));
|
|
107
|
-
}
|
|
108
|
-
} else {
|
|
109
|
-
console.log(chalk.green('✅ WhatsApp bağlantısı başlatıldı!\n'));
|
|
110
|
-
console.log(chalk.gray('Bağlantı durumunu kontrol edin: natureco whatsapp status\n'));
|
|
87
|
+
if (!data.session_id) {
|
|
88
|
+
throw new Error('No session ID returned');
|
|
111
89
|
}
|
|
112
90
|
|
|
113
|
-
|
|
91
|
+
console.log(chalk.green('✅ Session oluşturuldu!\n'));
|
|
92
|
+
console.log(chalk.cyan('Session ID:'), chalk.white(data.session_id));
|
|
93
|
+
console.log(chalk.yellow('\n⏳ QR kod bekleniyor...\n'));
|
|
94
|
+
|
|
95
|
+
// Save session ID to config
|
|
96
|
+
config.whatsappSessionId = data.session_id;
|
|
114
97
|
config.whatsappConnected = true;
|
|
115
98
|
config.whatsappBotId = botId;
|
|
116
99
|
saveConfig(config);
|
|
117
100
|
|
|
118
|
-
|
|
119
|
-
|
|
101
|
+
// Connect to SSE endpoint for QR code
|
|
102
|
+
const sseUrl = `http://localhost:3000/qr/${data.session_id}`;
|
|
103
|
+
const eventSource = new EventSource(sseUrl);
|
|
104
|
+
|
|
105
|
+
let qrDisplayed = false;
|
|
106
|
+
|
|
107
|
+
eventSource.onmessage = (event) => {
|
|
108
|
+
try {
|
|
109
|
+
const eventData = JSON.parse(event.data);
|
|
110
|
+
|
|
111
|
+
if (eventData.type === 'qr') {
|
|
112
|
+
if (!qrDisplayed) {
|
|
113
|
+
console.log(chalk.green('✅ QR kod hazır!\n'));
|
|
114
|
+
|
|
115
|
+
// Display QR code in terminal using the text version
|
|
116
|
+
if (eventData.qrText) {
|
|
117
|
+
qrcode.generate(eventData.qrText, { small: true });
|
|
118
|
+
} else if (eventData.qr) {
|
|
119
|
+
// Fallback: try to extract from base64 data URL
|
|
120
|
+
console.log(chalk.yellow('QR kod alındı (base64 format)\n'));
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
console.log('');
|
|
124
|
+
console.log(chalk.gray('1. WhatsApp\'ı açın'));
|
|
125
|
+
console.log(chalk.gray('2. Ayarlar > Bağlı Cihazlar > Cihaz Bağla'));
|
|
126
|
+
console.log(chalk.gray('3. Bu QR kodu taratın\n'));
|
|
127
|
+
console.log(chalk.yellow('⏳ QR kod taranması bekleniyor...\n'));
|
|
128
|
+
|
|
129
|
+
qrDisplayed = true;
|
|
130
|
+
}
|
|
131
|
+
} else if (eventData.type === 'ready') {
|
|
132
|
+
console.log(chalk.green('✅ WhatsApp bağlandı!\n'));
|
|
133
|
+
console.log(chalk.cyan('Bot:'), chalk.white(selectedBot.name));
|
|
134
|
+
console.log(chalk.gray('Botunuz WhatsApp\'ta aktif.\n'));
|
|
135
|
+
eventSource.close();
|
|
136
|
+
process.exit(0);
|
|
137
|
+
} else if (eventData.type === 'error') {
|
|
138
|
+
console.log(chalk.red(`\n❌ Hata: ${eventData.error}\n`));
|
|
139
|
+
eventSource.close();
|
|
140
|
+
process.exit(1);
|
|
141
|
+
} else if (eventData.type === 'disconnected') {
|
|
142
|
+
console.log(chalk.yellow(`\n⚠️ Bağlantı kesildi: ${eventData.reason}\n`));
|
|
143
|
+
eventSource.close();
|
|
144
|
+
process.exit(1);
|
|
145
|
+
}
|
|
146
|
+
} catch (err) {
|
|
147
|
+
console.error(chalk.red('Event parse error:'), err.message);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
eventSource.onerror = (err) => {
|
|
152
|
+
console.log(chalk.red('\n❌ SSE connection error\n'));
|
|
153
|
+
console.log(chalk.gray('Make sure WhatsApp service is running on http://localhost:3000\n'));
|
|
154
|
+
eventSource.close();
|
|
155
|
+
process.exit(1);
|
|
156
|
+
};
|
|
157
|
+
|
|
158
|
+
// Handle Ctrl+C
|
|
159
|
+
process.on('SIGINT', () => {
|
|
160
|
+
console.log(chalk.yellow('\n\n⚠️ Bağlantı iptal edildi\n'));
|
|
161
|
+
eventSource.close();
|
|
162
|
+
process.exit(0);
|
|
163
|
+
});
|
|
164
|
+
|
|
120
165
|
} catch (err) {
|
|
121
166
|
console.log(chalk.red(`\n❌ Connection failed: ${err.message}\n`));
|
|
167
|
+
console.log(chalk.gray('Make sure WhatsApp service is running on http://localhost:3000\n'));
|
|
122
168
|
process.exit(1);
|
|
123
169
|
}
|
|
124
170
|
}
|
|
@@ -151,11 +197,10 @@ async function disconnectWhatsApp() {
|
|
|
151
197
|
|
|
152
198
|
if (apiKey && config.whatsappSessionId) {
|
|
153
199
|
try {
|
|
154
|
-
const response = await fetch('
|
|
200
|
+
const response = await fetch('http://localhost:3000/disconnect', {
|
|
155
201
|
method: 'POST',
|
|
156
202
|
headers: {
|
|
157
203
|
'Content-Type': 'application/json',
|
|
158
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
159
204
|
},
|
|
160
205
|
body: JSON.stringify({
|
|
161
206
|
session_id: config.whatsappSessionId,
|
|
@@ -196,11 +241,8 @@ async function statusWhatsApp() {
|
|
|
196
241
|
try {
|
|
197
242
|
console.log(chalk.yellow('\n⏳ Checking connection status...\n'));
|
|
198
243
|
|
|
199
|
-
const response = await fetch(`
|
|
244
|
+
const response = await fetch(`http://localhost:3000/status/${config.whatsappSessionId}`, {
|
|
200
245
|
method: 'GET',
|
|
201
|
-
headers: {
|
|
202
|
-
'Authorization': `Bearer ${apiKey}`,
|
|
203
|
-
},
|
|
204
246
|
});
|
|
205
247
|
|
|
206
248
|
if (response.ok) {
|