/** * Test script for sending SMS via Kavenegar API * * Usage: * node test-sms.js * * Make sure to update the API_KEY and RECEPTOR (your phone number) below */ const https = require('https'); const querystring = require('querystring'); // ===== CONFIGURATION ===== // Replace with your actual Kavenegar API key const API_KEY = '75776C717969412B4B52306A5956462F4A714E6F6C65544D6A2B654B7566786E'; // Replace with your actual API key // Replace with your phone number (format: 09*********) const RECEPTOR = '09199503061'; // Replace with your actual phone number // Optional: Sender number (if not provided, uses default account sender) // const SENDER = '10004346'; // Optional - can be removed if you want to use default // Test message const MESSAGE = 'تست ارسال پیامک از سیستم Yara724'; // ===== END CONFIGURATION ===== function sendSMS(receptor, message, sender = null, options = {}) { return new Promise((resolve, reject) => { // Build URL const baseUrl = `https://api.kavenegar.com/v1/${API_KEY}/sms/send.json`; // Build query parameters const params = { receptor: receptor, message: encodeURIComponent(message), // Encode message for URL }; // Only add sender if it's provided (not null/undefined/empty) if (sender && sender.trim() !== '') { params.sender = sender; } // Add optional parameters if (options.date) params.date = options.date; if (options.type) params.type = options.type; if (options.localid) params.localid = options.localid; if (options.hide) params.hide = options.hide; if (options.tag) params.tag = options.tag; if (options.policy) params.policy = options.policy; const queryString = querystring.stringify(params); const url = `${baseUrl}?${queryString}`; console.log('\n📤 Sending SMS...'); console.log('URL:', url.replace(API_KEY, '***API_KEY***')); console.log('Receptor:', receptor); console.log('Message:', message); console.log('Sender:', sender || '(Using default account sender)'); // Make HTTPS request https.get(url, (res) => { let data = ''; res.on('data', (chunk) => { data += chunk; }); res.on('end', () => { try { const response = JSON.parse(data); if (response.return && response.return.status === 200) { console.log('\n✅ SMS sent successfully!'); console.log('Response:', JSON.stringify(response, null, 2)); if (response.entries && response.entries.length > 0) { console.log('\n📊 SMS Details:'); response.entries.forEach((entry, index) => { console.log(`\n Message ${index + 1}:`); console.log(` Message ID: ${entry.messageid}`); console.log(` Status: ${entry.status} (${entry.statustext})`); console.log(` Receptor: ${entry.receptor}`); console.log(` Sender: ${entry.sender}`); console.log(` Cost: ${entry.cost} Rials`); console.log(` Date: ${new Date(entry.date * 1000).toLocaleString('fa-IR')}`); }); } resolve(response); } else { console.error('\n❌ Error sending SMS'); console.error('Response:', JSON.stringify(response, null, 2)); reject(new Error(`API Error: ${response.return?.message || 'Unknown error'}`)); } } catch (error) { console.error('\n❌ Error parsing response:', error.message); console.error('Raw response:', data); reject(error); } }); }).on('error', (error) => { console.error('\n❌ Network error:', error.message); reject(error); }); }); } /** * Test function */ async function testSMS() { console.log('='.repeat(50)); console.log('🧪 Kavenegar SMS Test Script'); console.log('='.repeat(50)); // Validate configuration if (API_KEY === '613472435563797A37677331D' || API_KEY.length < 10) { console.error('\n⚠️ WARNING: Please update API_KEY with your actual Kavenegar API key!'); console.error(' You can find it in your Kavenegar panel: https://panel.kavenegar.com/client/membership/api'); } if (RECEPTOR === '09123456789' || !RECEPTOR.startsWith('09')) { console.error('\n⚠️ WARNING: Please update RECEPTOR with your actual phone number!'); console.error(' Format: 09********* (11 digits starting with 09)'); } try { // Test 1: Simple SMS (without sender - uses default account sender) console.log('\n\n📝 Test 1: Simple SMS (No sender - using default)'); await sendSMS(RECEPTOR, MESSAGE); // Wait a bit before next test await new Promise(resolve => setTimeout(resolve, 2000)); // Test 2: SMS with tag (optional - uncomment if you have tags set up) // console.log('\n\n📝 Test 2: SMS with Tag'); // await sendSMS(RECEPTOR, 'تست پیامک با تگ', SENDER, { tag: 'test' }); // Test 3: Multiple recipients (uncomment to test) // console.log('\n\n📝 Test 3: Multiple Recipients'); // const multipleReceptors = `${RECEPTOR},09123456789`; // Add more numbers separated by comma // await sendSMS(multipleReceptors, 'تست پیامک به چندین گیرنده', SENDER); console.log('\n\n' + '='.repeat(50)); console.log('✅ All tests completed!'); console.log('='.repeat(50)); } catch (error) { console.error('\n\n❌ Test failed:', error.message); process.exit(1); } } // Run the test if (require.main === module) { testSMS().catch((error) => { console.error('Fatal error:', error); process.exit(1); }); } module.exports = { sendSMS };