SpellLBChshs / index.js
Jonell01's picture
Update index.js
46334d5 verified
import express from 'express';
import axios from 'axios';
import chalk from 'chalk';
const app = express();
const PORT = 7860;
const JWT_URL = "https://34.160.218.33.nip.io/lbconline/GenerateJWTToken";
const TOKEN_URL = "https://lbcapigateway.lbcapps.com/promotextertoken/generate_client_token";
const SEND_URL = "https://34.160.218.33.nip.io/lbconline/AddDefaultDisbursement";
const jwtBody = {
Client: "2E1EEB",
email: "xcarlodeggz@gmail.com",
password: "xzyocgTlbP+tH6/rpS0XOw==:e83e5d869cb34117e9a5408de6f6b764"
};
const defaultHeaders = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0",
"Accept": "*/*",
"Accept-Language": "en-US,en;q=0.5",
"Referer": "https://lbconline.lbcexpress.com/",
"Origin": "https://lbconline.lbcexpress.com"
};
async function generateJWT() {
try {
const { data } = await axios.post(JWT_URL, jwtBody, {
headers: { ...defaultHeaders, "Content-Type": "application/json" }
});
const jwt = data?.replace(/"/g, "") || null;
if (!jwt) throw new Error("Failed to extract JWT token.");
console.log(chalk.blue("[INFO] JWT token generated successfully."));
return jwt;
} catch (error) {
console.error(chalk.red(`[ERROR] JWT generation failed: ${error.message}`));
throw error;
}
}
async function generateClientToken(jwt) {
try {
const { data } = await axios.get(TOKEN_URL, {
headers: {
...defaultHeaders,
"Content-Type": "application/json",
"Authorization": `Bearer ${jwt}`,
"Ocp-Apim-Subscription-Key": "dbcd31c8bc4f471188f8b6d226bb9ff7"
}
});
const clientToken = data?.client_token || null;
if (!clientToken) throw new Error("Failed to obtain client token.");
console.log(chalk.blue("[INFO] Client token obtained successfully."));
return clientToken;
} catch (error) {
console.error(chalk.red(`[ERROR] Client token generation failed: ${error.message}`));
throw error;
}
}
async function sendMessage(jwt, clientToken, number, message) {
const payload = {
Recipient: `63${number}`,
Message: message,
ShipperUuid: "LBCEXPRESS",
DefaultDisbursement: 3,
ApiSecret: "03da764a333680d6ebd2f6f4ef1e2928",
apikey: "7777be96b2d1c6d0dee73d566a820c5f"
};
try {
const { data } = await axios.post(SEND_URL, payload, {
headers: {
...defaultHeaders,
"Authorization": `Bearer ${jwt}`,
"ptxToken": clientToken,
"Content-Type": "application/json"
}
});
if (data?.status === "ok") {
console.log(chalk.green(`[SENT] Message sent to 63${number}`));
return { success: true, message: `Message sent to 63${number}` };
} else {
console.log(chalk.red(`[FAILED] Could not send to 63${number}`));
return { success: false, message: `Could not send to 63${number}` };
}
} catch (error) {
console.error(chalk.red(`[ERROR] Failed to send message to 63${number}: ${error.message}`));
return { success: false, message: `Error sending to 63${number}` };
}
}
app.get('/sms', async (req, res) => {
const { number, message, count } = req.query;
if (!number || !message || !count) {
return res.status(400).json({ success: false, message: 'Number, message, and count are required' });
}
const numCount = parseInt(count);
if (isNaN(numCount) || numCount <= 0) {
return res.status(400).json({ success: false, message: 'Count must be a valid number greater than 0' });
}
try {
const jwt = await generateJWT();
const clientToken = await generateClientToken(jwt);
const results = await Promise.all(
Array.from({ length: numCount }, () => sendMessage(jwt, clientToken, number, message))
);
return res.json({ success: true, results });
} catch (err) {
console.error(chalk.red(`[ERROR] ${err.message}`));
return res.status(500).json({ success: false, message: 'Internal Server Error' });
}
});
app.listen(PORT, () => {
console.log(chalk.green(`[SERVER] Running on port ${PORT}`));
});