natureco-cli 1.0.26 → 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 +1 -1
- package/src/commands/whatsapp.js +74 -6
package/package.json
CHANGED
package/src/commands/whatsapp.js
CHANGED
|
@@ -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);
|
|
@@ -116,8 +133,12 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
|
|
|
116
133
|
version,
|
|
117
134
|
auth: state,
|
|
118
135
|
printQRInTerminal: false,
|
|
119
|
-
browser: Browsers.ubuntu('Chrome'),
|
|
120
136
|
logger: logger,
|
|
137
|
+
browser: Browsers.ubuntu('Chrome'),
|
|
138
|
+
connectTimeoutMs: 60000,
|
|
139
|
+
defaultQueryTimeoutMs: 60000,
|
|
140
|
+
keepAliveIntervalMs: 10000,
|
|
141
|
+
retryRequestDelayMs: 2000,
|
|
121
142
|
});
|
|
122
143
|
|
|
123
144
|
let qrDisplayed = false;
|
|
@@ -145,10 +166,10 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
|
|
|
145
166
|
if (connection === 'close') {
|
|
146
167
|
const statusCode = lastDisconnect?.error?.output?.statusCode;
|
|
147
168
|
|
|
148
|
-
if (statusCode === 515) {
|
|
169
|
+
if (statusCode === 515 || statusCode === 408) {
|
|
149
170
|
// Normal — yeniden bağlan, logout değil
|
|
150
171
|
console.log(chalk.yellow('🔄 Yeniden bağlanıyor...'));
|
|
151
|
-
setTimeout(() => startWhatsAppConnection(sessionDir, botId, selectedBot, config),
|
|
172
|
+
setTimeout(() => startWhatsAppConnection(sessionDir, botId, selectedBot, config, allowedNumbersList), 2000);
|
|
152
173
|
return;
|
|
153
174
|
} else if (statusCode === 401) {
|
|
154
175
|
console.log(chalk.red('❌ Oturum sonlandı, tekrar bağlanın.'));
|
|
@@ -171,12 +192,22 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
|
|
|
171
192
|
console.log(chalk.cyan('Telefon:'), chalk.white(sock.user?.id || 'Unknown'));
|
|
172
193
|
console.log(chalk.gray('\nSession kaydedildi: ~/.natureco/whatsapp-sessions/'));
|
|
173
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
|
+
|
|
174
199
|
// Save to config
|
|
175
200
|
config.whatsappConnected = true;
|
|
176
201
|
config.whatsappBotId = botId;
|
|
177
202
|
config.whatsappPhone = sock.user?.id;
|
|
203
|
+
config.whatsappAllowedNumbers = finalAllowedNumbers;
|
|
178
204
|
saveConfig(config);
|
|
179
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
|
+
|
|
180
211
|
console.log(chalk.green('\n✅ Kurulum tamamlandı!\n'));
|
|
181
212
|
console.log(chalk.gray('Botunuz WhatsApp\'ta aktif.\n'));
|
|
182
213
|
|
|
@@ -187,6 +218,43 @@ async function startWhatsAppConnection(sessionDir, botId, selectedBot, config) {
|
|
|
187
218
|
}
|
|
188
219
|
});
|
|
189
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
|
+
|
|
190
258
|
// Save credentials on update
|
|
191
259
|
sock.ev.on('creds.update', saveCreds);
|
|
192
260
|
|