natureco-cli 1.0.27 → 1.0.28

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "natureco-cli",
3
- "version": "1.0.27",
3
+ "version": "1.0.28",
4
4
  "description": "NatureCo AI Bot Terminal Interface",
5
5
  "main": "bin/natureco.js",
6
6
  "bin": {
@@ -6,7 +6,7 @@ const path = require('path');
6
6
  const os = require('os');
7
7
  const pino = require('pino');
8
8
  const { getApiKey, getConfig, saveConfig } = require('../utils/config');
9
- const { getBots } = require('../utils/api');
9
+ const { getBots, sendMessage } = require('../utils/api');
10
10
 
11
11
  // Baileys imports
12
12
  let makeWASocket, useMultiFileAuthState, DisconnectReason, fetchLatestBaileysVersion, Browsers;
@@ -85,6 +85,17 @@ async function connectWhatsApp() {
85
85
 
86
86
  const selectedBot = botList.bots.find(b => b.id === botId);
87
87
 
88
+ // Ask for allowed numbers
89
+ process.stdin.resume();
90
+ const { allowedNumbers } = await inquirer.prompt([
91
+ {
92
+ type: 'input',
93
+ name: 'allowedNumbers',
94
+ message: 'Hangi numaralar mesaj gönderebilir? (virgülle ayırın, boş bırakırsan sadece kendi numaran)',
95
+ default: '',
96
+ },
97
+ ]);
98
+
88
99
  console.log(chalk.cyan('\n📱 WhatsApp bağlantısı başlatılıyor...'));
89
100
  console.log(chalk.gray('Telefonunuzda WhatsApp\'ı açın ve QR kodu taratın.\n'));
90
101
 
@@ -97,11 +108,17 @@ async function connectWhatsApp() {
97
108
  fs.mkdirSync(sessionDir, { recursive: true });
98
109
  }
99
110
 
111
+ // Parse allowed numbers
112
+ const allowedNumbersList = allowedNumbers
113
+ .split(',')
114
+ .map(n => n.trim())
115
+ .filter(n => n.length > 0);
116
+
100
117
  // Start connection
101
- await startWhatsAppConnection(sessionDir, botId, selectedBot, config);
118
+ await startWhatsAppConnection(sessionDir, botId, selectedBot, config, allowedNumbersList);
102
119
  }
103
120
 
104
- async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
121
+ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config, allowedNumbersList = []) {
105
122
  try {
106
123
  // Create auth state
107
124
  const { state, saveCreds } = await useMultiFileAuthState(sessionDir);
@@ -152,7 +169,7 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
152
169
  if (statusCode === 515 || statusCode === 408) {
153
170
  // Normal — yeniden bağlan, logout değil
154
171
  console.log(chalk.yellow('🔄 Yeniden bağlanıyor...'));
155
- setTimeout(() => startWhatsAppConnection(sessionDir, botId, selectedBot, config), 2000);
172
+ setTimeout(() => startWhatsAppConnection(sessionDir, botId, selectedBot, config, allowedNumbersList), 2000);
156
173
  return;
157
174
  } else if (statusCode === 401) {
158
175
  console.log(chalk.red('❌ Oturum sonlandı, tekrar bağlanın.'));
@@ -175,12 +192,22 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
175
192
  console.log(chalk.cyan('Telefon:'), chalk.white(sock.user?.id || 'Unknown'));
176
193
  console.log(chalk.gray('\nSession kaydedildi: ~/.natureco/whatsapp-sessions/'));
177
194
 
195
+ // Prepare allowed numbers list (add own number)
196
+ const ownNumber = sock.user?.id?.replace('@s.whatsapp.net', '');
197
+ const finalAllowedNumbers = ownNumber ? [ownNumber, ...allowedNumbersList] : allowedNumbersList;
198
+
178
199
  // Save to config
179
200
  config.whatsappConnected = true;
180
201
  config.whatsappBotId = botId;
181
202
  config.whatsappPhone = sock.user?.id;
203
+ config.whatsappAllowedNumbers = finalAllowedNumbers;
182
204
  saveConfig(config);
183
205
 
206
+ if (finalAllowedNumbers.length > 0) {
207
+ console.log(chalk.cyan('\nİzin verilen numaralar:'));
208
+ finalAllowedNumbers.forEach(num => console.log(chalk.gray(` - ${num}`)));
209
+ }
210
+
184
211
  console.log(chalk.green('\n✅ Kurulum tamamlandı!\n'));
185
212
  console.log(chalk.gray('Botunuz WhatsApp\'ta aktif.\n'));
186
213
 
@@ -191,6 +218,43 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
191
218
  }
192
219
  });
193
220
 
221
+ // Message handler with access control
222
+ sock.ev.on('messages.upsert', async ({ messages }) => {
223
+ for (const msg of messages) {
224
+ if (msg.key.fromMe) continue; // kendi mesajları yoksay
225
+
226
+ const sender = msg.key.remoteJid?.replace('@s.whatsapp.net', '');
227
+ const allowedNumbers = config.whatsappAllowedNumbers || [];
228
+
229
+ // İzin listesi doluysa kontrol et
230
+ if (allowedNumbers.length > 0 && !allowedNumbers.includes(sender)) {
231
+ console.log(chalk.yellow(`⚠️ İzinsiz numara: ${sender}`));
232
+ continue; // izinsiz numara, yoksay
233
+ }
234
+
235
+ // Mesaj içeriğini al
236
+ const messageText = msg.message?.conversation ||
237
+ msg.message?.extendedTextMessage?.text ||
238
+ '';
239
+
240
+ if (messageText) {
241
+ console.log(chalk.cyan(`\n📱 ${sender}:`), chalk.white(messageText));
242
+
243
+ try {
244
+ const response = await sendMessage(config.apiKey, config.defaultBotId, messageText, null, '');
245
+ const reply = response?.reply || response?.message || '';
246
+
247
+ if (reply) {
248
+ await sock.sendMessage(msg.key.remoteJid, { text: reply });
249
+ console.log(chalk.green(`✅ Cevap gönderildi: ${reply.substring(0, 50)}...`));
250
+ }
251
+ } catch (err) {
252
+ console.log(chalk.red('❌ API hatası:', err.message));
253
+ }
254
+ }
255
+ }
256
+ });
257
+
194
258
  // Save credentials on update
195
259
  sock.ev.on('creds.update', saveCreds);
196
260