Spaces:
Sleeping
Sleeping
Update index.js
Browse files
index.js
CHANGED
|
@@ -1,103 +1,125 @@
|
|
| 1 |
-
import express from 'express';
|
| 2 |
-
import axios from 'axios';
|
| 3 |
-
import chalk from 'chalk';
|
| 4 |
-
|
| 5 |
-
const app = express();
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
const
|
| 9 |
-
const
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"
|
| 20 |
-
"Accept
|
| 21 |
-
"
|
| 22 |
-
"
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
"Content-Type": "application/json"
|
| 30 |
-
}
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
}
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
const response = await axios.get(TOKEN_URL, {
|
| 40 |
-
headers: {
|
| 41 |
-
...defaultHeaders,
|
| 42 |
-
"Content-Type": "application/json",
|
| 43 |
-
"Authorization": `Bearer ${jwt}`,
|
| 44 |
-
"Ocp-Apim-Subscription-Key": "dbcd31c8bc4f471188f8b6d226bb9ff7"
|
| 45 |
-
}
|
| 46 |
-
});
|
| 47 |
-
|
| 48 |
-
return response.data?.client_token || null;
|
| 49 |
-
}
|
| 50 |
-
|
| 51 |
-
async function sendMessage(jwt, clientToken, number, message) {
|
| 52 |
-
const payload = {
|
| 53 |
-
Recipient: `63${number}`,
|
| 54 |
-
Message: message,
|
| 55 |
-
ShipperUuid: "LBCEXPRESS",
|
| 56 |
-
DefaultDisbursement: 3,
|
| 57 |
-
ApiSecret: "03da764a333680d6ebd2f6f4ef1e2928",
|
| 58 |
-
apikey: "7777be96b2d1c6d0dee73d566a820c5f"
|
| 59 |
-
};
|
| 60 |
-
|
| 61 |
-
const response = await axios.post(SEND_URL, payload, {
|
| 62 |
-
headers: {
|
| 63 |
-
...defaultHeaders,
|
| 64 |
-
"Authorization": `Bearer ${jwt}`,
|
| 65 |
-
"ptxToken": clientToken,
|
| 66 |
-
"Content-Type": "application/json"
|
| 67 |
-
}
|
| 68 |
-
});
|
| 69 |
-
|
| 70 |
-
if (response.data?.status === "ok") {
|
| 71 |
-
console.log(chalk.green(`[SENT] Message sent to 63${number}`));
|
| 72 |
-
return { success: true, message: `Message sent to 63${number}` };
|
| 73 |
-
} else {
|
| 74 |
-
console.log(chalk.red(`[FAILED] Could not send to 63${number}`));
|
| 75 |
-
return { success: false, message: `Could not send to 63${number}` };
|
| 76 |
}
|
| 77 |
-
}
|
| 78 |
-
|
| 79 |
-
app.get('/sms', async (req, res) => {
|
| 80 |
-
const number = req.query.number;
|
| 81 |
-
const message = req.query.message;
|
| 82 |
-
const count = req.query.count;
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
try {
|
| 89 |
-
const
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
return res.status(500).json({ success: false, message: 'Internal Server Error' });
|
| 102 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 103 |
});
|
|
|
|
| 1 |
+
import express from 'express';
|
| 2 |
+
import axios from 'axios';
|
| 3 |
+
import chalk from 'chalk';
|
| 4 |
+
|
| 5 |
+
const app = express();
|
| 6 |
+
const PORT = 7860;
|
| 7 |
+
|
| 8 |
+
const JWT_URL = "https://34.160.218.33.nip.io/lbconline/GenerateJWTToken";
|
| 9 |
+
const TOKEN_URL = "https://lbcapigateway.lbcapps.com/promotextertoken/generate_client_token";
|
| 10 |
+
const SEND_URL = "https://34.160.218.33.nip.io/lbconline/AddDefaultDisbursement";
|
| 11 |
+
|
| 12 |
+
const jwtBody = {
|
| 13 |
+
Client: "2E1EEB",
|
| 14 |
+
email: "xcarlodeggz@gmail.com",
|
| 15 |
+
password: "xzyocgTlbP+tH6/rpS0XOw==:e83e5d869cb34117e9a5408de6f6b764"
|
| 16 |
+
};
|
| 17 |
+
|
| 18 |
+
const defaultHeaders = {
|
| 19 |
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:136.0) Gecko/20100101 Firefox/136.0",
|
| 20 |
+
"Accept": "*/*",
|
| 21 |
+
"Accept-Language": "en-US,en;q=0.5",
|
| 22 |
+
"Referer": "https://lbconline.lbcexpress.com/",
|
| 23 |
+
"Origin": "https://lbconline.lbcexpress.com"
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
async function generateJWT() {
|
| 27 |
+
try {
|
| 28 |
+
const { data } = await axios.post(JWT_URL, jwtBody, {
|
| 29 |
+
headers: { ...defaultHeaders, "Content-Type": "application/json" }
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
const jwt = data?.replace(/"/g, "") || null;
|
| 33 |
+
if (!jwt) throw new Error("Failed to extract JWT token.");
|
| 34 |
+
console.log(chalk.blue("[INFO] JWT token generated successfully."));
|
| 35 |
+
return jwt;
|
| 36 |
+
} catch (error) {
|
| 37 |
+
console.error(chalk.red(`[ERROR] JWT generation failed: ${error.message}`));
|
| 38 |
+
throw error;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
async function generateClientToken(jwt) {
|
| 43 |
+
try {
|
| 44 |
+
const { data } = await axios.get(TOKEN_URL, {
|
| 45 |
+
headers: {
|
| 46 |
+
...defaultHeaders,
|
| 47 |
+
"Content-Type": "application/json",
|
| 48 |
+
"Authorization": `Bearer ${jwt}`,
|
| 49 |
+
"Ocp-Apim-Subscription-Key": "dbcd31c8bc4f471188f8b6d226bb9ff7"
|
| 50 |
+
}
|
| 51 |
+
});
|
| 52 |
+
|
| 53 |
+
const clientToken = data?.client_token || null;
|
| 54 |
+
if (!clientToken) throw new Error("Failed to obtain client token.");
|
| 55 |
+
console.log(chalk.blue("[INFO] Client token obtained successfully."));
|
| 56 |
+
return clientToken;
|
| 57 |
+
} catch (error) {
|
| 58 |
+
console.error(chalk.red(`[ERROR] Client token generation failed: ${error.message}`));
|
| 59 |
+
throw error;
|
| 60 |
}
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
async function sendMessage(jwt, clientToken, number, message) {
|
| 64 |
+
const payload = {
|
| 65 |
+
Recipient: `63${number}`,
|
| 66 |
+
Message: message,
|
| 67 |
+
ShipperUuid: "LBCEXPRESS",
|
| 68 |
+
DefaultDisbursement: 3,
|
| 69 |
+
ApiSecret: "03da764a333680d6ebd2f6f4ef1e2928",
|
| 70 |
+
apikey: "7777be96b2d1c6d0dee73d566a820c5f"
|
| 71 |
+
};
|
| 72 |
|
| 73 |
try {
|
| 74 |
+
const { data } = await axios.post(SEND_URL, payload, {
|
| 75 |
+
headers: {
|
| 76 |
+
...defaultHeaders,
|
| 77 |
+
"Authorization": `Bearer ${jwt}`,
|
| 78 |
+
"ptxToken": clientToken,
|
| 79 |
+
"Content-Type": "application/json"
|
| 80 |
+
}
|
| 81 |
+
});
|
| 82 |
+
|
| 83 |
+
if (data?.status === "ok") {
|
| 84 |
+
console.log(chalk.green(`[SENT] Message sent to 63${number}`));
|
| 85 |
+
return { success: true, message: `Message sent to 63${number}` };
|
| 86 |
+
} else {
|
| 87 |
+
console.log(chalk.red(`[FAILED] Could not send to 63${number}`));
|
| 88 |
+
return { success: false, message: `Could not send to 63${number}` };
|
| 89 |
}
|
| 90 |
+
} catch (error) {
|
| 91 |
+
console.error(chalk.red(`[ERROR] Failed to send message to 63${number}: ${error.message}`));
|
| 92 |
+
return { success: false, message: `Error sending to 63${number}` };
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
|
| 96 |
+
app.get('/sms', async (req, res) => {
|
| 97 |
+
const { number, message, count } = req.query;
|
| 98 |
+
|
| 99 |
+
if (!number || !message || !count) {
|
| 100 |
+
return res.status(400).json({ success: false, message: 'Number, message, and count are required' });
|
| 101 |
+
}
|
| 102 |
|
| 103 |
+
const numCount = parseInt(count);
|
| 104 |
+
if (isNaN(numCount) || numCount <= 0) {
|
| 105 |
+
return res.status(400).json({ success: false, message: 'Count must be a valid number greater than 0' });
|
|
|
|
| 106 |
}
|
| 107 |
+
|
| 108 |
+
try {
|
| 109 |
+
const jwt = await generateJWT();
|
| 110 |
+
const clientToken = await generateClientToken(jwt);
|
| 111 |
+
|
| 112 |
+
const results = await Promise.all(
|
| 113 |
+
Array.from({ length: numCount }, () => sendMessage(jwt, clientToken, number, message))
|
| 114 |
+
);
|
| 115 |
+
|
| 116 |
+
return res.json({ success: true, results });
|
| 117 |
+
} catch (err) {
|
| 118 |
+
console.error(chalk.red(`[ERROR] ${err.message}`));
|
| 119 |
+
return res.status(500).json({ success: false, message: 'Internal Server Error' });
|
| 120 |
+
}
|
| 121 |
+
});
|
| 122 |
+
|
| 123 |
+
app.listen(PORT, () => {
|
| 124 |
+
console.log(chalk.green(`[SERVER] Running on port ${PORT}`));
|
| 125 |
});
|