Files
vaishnavi-ecommerce-backend/testWhatsApp.js
2026-03-10 12:43:27 +05:30

191 lines
5.3 KiB
JavaScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// testWappConnect.js - COMPREHENSIVE TEST
require('dotenv').config();
const axios = require('axios');
const BASE_URL = 'https://api.wappconnect.com';
const SESSION = 'Sahasrara_metatech';
const USERNAME = '8369805801';
const PASSWORD = '918369805801';
const TEST_PHONE = '918070905353';
async function comprehensiveTest() {
console.log('🧪 WappConnect Comprehensive Test\n');
console.log('📋 Configuration:');
console.log(' Base URL:', BASE_URL);
console.log(' Session:', SESSION);
console.log(' Username:', USERNAME);
console.log(' Test Phone:', TEST_PHONE);
console.log('\n' + '='.repeat(60) + '\n');
// ===== TEST 1: Login =====
console.log('TEST 1: Login to WappConnect\n');
let authToken = null;
let cookies = null;
try {
const loginResponse = await axios.post(
`${BASE_URL}/api/auth/login`,
{
username: USERNAME,
password: PASSWORD,
},
{
headers: {
'Content-Type': 'application/json',
},
}
);
console.log('✅ Login successful!');
console.log(' Response:', loginResponse.data);
if (loginResponse.data.token) {
authToken = loginResponse.data.token;
console.log(' Token:', authToken.substring(0, 20) + '...');
}
if (loginResponse.headers['set-cookie']) {
cookies = loginResponse.headers['set-cookie'].join('; ');
console.log(' Cookies received');
}
} catch (error) {
console.error('❌ Login failed:', error.message);
if (error.response) {
console.error(' Status:', error.response.status);
console.error(' Data:', error.response.data);
}
}
console.log('\n' + '='.repeat(60) + '\n');
// ===== TEST 2: Check Session Status =====
console.log('TEST 2: Check Session Status\n');
const statusEndpoints = [
`/api/${SESSION}/status`,
`/api/${SESSION}/check-connection`,
`/api/${SESSION}/check-connection-session`,
`/${SESSION}/status`,
];
for (const endpoint of statusEndpoints) {
try {
console.log(`🔄 Trying: ${endpoint}`);
const headers = {
'Content-Type': 'application/json',
};
if (authToken) headers['Authorization'] = `Bearer ${authToken}`;
if (cookies) headers['Cookie'] = cookies;
const response = await axios.get(`${BASE_URL}${endpoint}`, { headers });
console.log('✅ SUCCESS!');
console.log(' Status:', response.data);
break;
} catch (error) {
console.log(` ❌ Failed: ${error.response?.status || error.message}`);
}
}
console.log('\n' + '='.repeat(60) + '\n');
// ===== TEST 3: Send Message =====
console.log('TEST 3: Send Test Message\n');
const messageEndpoints = [
`/api/${SESSION}/send-text`,
`/api/${SESSION}/sendText`,
`/api/${SESSION}/send-message`,
`/api/${SESSION}/sendMessage`,
`/${SESSION}/send-text`,
`/send-message/${SESSION}`,
`/api/sendText/${SESSION}`,
];
const messagePayloads = [
// Payload variation 1
{
phone: TEST_PHONE,
message: '🧪 Test from WappConnect - Method 1',
isGroup: false,
},
// Payload variation 2
{
phone: TEST_PHONE,
text: '🧪 Test from WappConnect - Method 2',
},
// Payload variation 3
{
chatId: `${TEST_PHONE}@c.us`,
message: '🧪 Test from WappConnect - Method 3',
},
// Payload variation 4
{
number: TEST_PHONE,
body: '🧪 Test from WappConnect - Method 4',
},
];
let messageSent = false;
for (const endpoint of messageEndpoints) {
if (messageSent) break;
for (const payload of messagePayloads) {
try {
console.log(`🔄 Trying: ${endpoint}`);
console.log(` Payload:`, JSON.stringify(payload, null, 2));
const headers = {
'Content-Type': 'application/json',
};
if (authToken) headers['Authorization'] = `Bearer ${authToken}`;
if (cookies) headers['Cookie'] = cookies;
const response = await axios.post(
`${BASE_URL}${endpoint}`,
payload,
{ headers, timeout: 15000 }
);
console.log('✅ MESSAGE SENT SUCCESSFULLY!');
console.log(' Response:', response.data);
console.log('\n📱 CHECK YOUR WHATSAPP NOW!\n');
messageSent = true;
// Save working configuration
console.log('💾 WORKING CONFIGURATION:');
console.log(' Endpoint:', endpoint);
console.log(' Payload:', JSON.stringify(payload, null, 2));
if (authToken) console.log(' Uses Token: Yes');
if (cookies) console.log(' Uses Cookies: Yes');
break;
} catch (error) {
console.log(` ❌ Failed: ${error.response?.status || error.message}`);
if (error.response?.data) {
console.log(` Error:`, error.response.data);
}
}
}
}
if (!messageSent) {
console.log('\n❌ Could not send message with any endpoint/payload combination');
console.log('\n Recommendations:');
console.log(' 1. Check if WhatsApp is connected in dashboard');
console.log(' 2. Try connecting WhatsApp again (scan QR)');
console.log(' 3. Contact WappConnect support');
console.log(' 4. Consider using Twilio instead (easier setup)');
}
console.log('\n' + '='.repeat(60));
}
comprehensiveTest();