change web service sms

This commit is contained in:
2026-05-30 11:13:52 +03:30
parent f213ad2453
commit 0673729a4d
3 changed files with 218 additions and 39 deletions
+63 -16
View File
@@ -3,6 +3,7 @@ import 'dotenv/config';
const username = process.env.SMS_USERNAME;
const password = process.env.SMS_PASSWORD ?? process.env.SMS_API_KEY;
const sender = process.env.SMS_SENDER;
const provider = (process.env.SMS_PROVIDER ?? 'trez').toLowerCase();
if (!username || !password || !sender) {
console.error('Set SMS_USERNAME, SMS_PASSWORD (or SMS_API_KEY), and SMS_SENDER in .env');
@@ -11,20 +12,60 @@ if (!username || !password || !sender) {
const targets = process.argv.slice(2).length
? process.argv.slice(2)
: ['09397439581'];
function escapeXml(value) {
return String(value)
.replace(/&/g, '&')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
}
: ['09128081392'];
const text = 'کد ورود منتایم: 123456';
const timeoutMs = Number.parseInt(process.env.SMS_TIMEOUT_MS ?? '20000', 10);
function parseTrezResponse(body) {
const trimmed = body.trim();
if (!trimmed) return { ok: true, reason: 'empty' };
const lower = trimmed.toLowerCase();
if (lower.includes('error') || lower.includes('fail') || lower.includes('خطا')) {
return { ok: false, reason: trimmed };
}
if (/^-?\d+$/.test(trimmed)) {
const n = Number(trimmed);
if (n === 1 || n > 1) return { ok: true, reason: trimmed };
return { ok: false, reason: `code ${trimmed}` };
}
return { ok: true, reason: trimmed };
}
async function sendViaTrez(mobile) {
const baseUrl = (process.env.SMS_BASE_URL ?? 'http://smspanel.Trez.ir').replace(/\/$/, '');
const path = process.env.SMS_SEND_PATH ?? '/SendMessageWithCode.ashx';
const url = `${baseUrl}${path.startsWith('/') ? path : `/${path}`}`;
const res = await fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: new URLSearchParams({
PhoneNumber: sender,
Username: username,
Password: password,
Mobile: mobile,
Message: text,
}).toString(),
signal: AbortSignal.timeout(timeoutMs),
});
const body = await res.text();
const parsed = parseTrezResponse(body);
console.log(
`mobile=${mobile} provider=trez http=${res.status} body=${JSON.stringify(body.trim())} ${parsed.ok ? 'OK' : 'FAILED'} (${parsed.reason})`,
);
}
async function sendVia0098(mobile) {
const escapeXml = (value) =>
String(value)
.replace(/&/g, '&amp;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&apos;');
for (const mobile of targets) {
const soapBody = `<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
@@ -45,7 +86,7 @@ for (const mobile of targets) {
SOAPAction: 'http://tempuri.org/SendSMS',
},
body: soapBody,
signal: AbortSignal.timeout(20_000),
signal: AbortSignal.timeout(timeoutMs),
});
const body = await res.text();
@@ -53,10 +94,16 @@ for (const mobile of targets) {
const code = match?.[1]?.trim() ?? '?';
const n = Number(code);
const ok = Number.isFinite(n) ? n >= 0 : false;
const status = ok ? 'OK (accepted by 0098)' : 'FAILED';
console.log(
`mobile=${mobile} provider=0098 http=${res.status} RESULT=${code} ${ok ? 'OK' : 'FAILED'}`,
);
}
console.log(`mobile=${mobile} http=${res.status} RESULT=${code} ${status}`);
if (!ok) {
console.log(body.slice(0, 400));
console.log(`SMS_PROVIDER=${provider}`);
for (const mobile of targets) {
if (provider === '0098') {
await sendVia0098(mobile);
} else {
await sendViaTrez(mobile);
}
}