Files
serverMontime/scripts/test-sms.mjs
T

54 lines
1.6 KiB
JavaScript

import 'dotenv/config';
const username = process.env.SMS_USERNAME;
const password = process.env.SMS_PASSWORD;
const sender = process.env.SMS_SENDER;
if (!username || !password || !sender) {
console.error('Missing SMS env vars');
process.exit(1);
}
const text = 'Montime code: 123456';
const targets = process.argv.slice(2).length
? process.argv.slice(2)
: ['09397439581', '09128081392'];
function escapeXml(value) {
return String(value)
.replace(/&/g, '&')
.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<SendSMS xmlns="http://tempuri.org/">
<username>${escapeXml(username)}</username>
<password>${escapeXml(password)}</password>
<text>${escapeXml(text)}</text>
<mobileno>${escapeXml(mobile)}</mobileno>
<pnlno>${escapeXml(sender)}</pnlno>
</SendSMS>
</soap:Body>
</soap:Envelope>`;
const res = await fetch('https://webservice.0098sms.com/service.asmx', {
method: 'POST',
headers: {
'Content-Type': 'text/xml; charset=utf-8',
SOAPAction: 'http://tempuri.org/SendSMS',
},
body: soapBody,
signal: AbortSignal.timeout(20000),
});
const body = await res.text();
const match = body.match(/<SendSMSResult[^>]*>([^<]*)<\/SendSMSResult>/);
console.log(mobile, 'HTTP', res.status, 'RESULT', match?.[1] ?? body.slice(0, 200));
}