netgsm-otp
Version:
Unofficial Node.js/TypeScript client for NetGSM OTP SMS service
159 lines (158 loc) • 5.63 kB
JavaScript
;
/**
* NetGSM OTP SMS Service Client
* Unofficial TypeScript client for NetGSM OTP SMS service
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.NetGsmOtp = void 0;
/**
* Response codes from NetGSM API
*/
var NetGsmResponseCode;
(function (NetGsmResponseCode) {
NetGsmResponseCode["SUCCESS"] = "00";
NetGsmResponseCode["AUTHENTICATION_ERROR"] = "20";
NetGsmResponseCode["PARAMETER_ERROR"] = "30";
NetGsmResponseCode["BALANCE_ERROR"] = "40";
NetGsmResponseCode["SYSTEM_ERROR"] = "70";
})(NetGsmResponseCode || (NetGsmResponseCode = {}));
/**
* NetGSM OTP SMS Client
*/
class NetGsmOtp {
/**
* Create a new NetGsmOtp instance
* @param options Authentication options
*/
constructor(options) {
this.apiUrl = 'https://api.netgsm.com.tr/sms/send/otp';
this.usercode = options.usercode;
this.password = options.password;
this.msgheader = options.msgheader;
this.appkey = options.appkey;
}
/**
* Validate a phone number
* @param phoneNumber The phone number to validate
* @returns True if the phone number is valid, false otherwise
*/
isValidPhoneNumber(phoneNumber) {
// Remove any non-digit characters
const digitsOnly = phoneNumber.replace(/\D/g, '');
// Check if the phone number starts with country code (90)
if (digitsOnly.startsWith('90')) {
// If it starts with 90, it should be 12 digits (90 + 10 digits)
// And the digit after 90 should be 5 (for mobile numbers)
return digitsOnly.length === 12 && digitsOnly.charAt(2) === '5';
}
// If no country code, it should be 10 digits and start with 5
return digitsOnly.length === 10 && digitsOnly.charAt(0) === '5';
}
/**
* Send an OTP SMS message
* @param options Options for sending OTP
* @returns Promise with the response from NetGSM API
*/
async sendOtp(options) {
try {
// Validate phone number
if (!this.isValidPhoneNumber(options.phoneNumber)) {
return {
success: false,
message: 'Invalid phone number. Please provide a valid Turkish mobile number.',
};
}
// Prepare the XML payload
const xmlPayload = this.buildXmlPayload(options);
// Send the request
const response = await fetch(this.apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/xml',
},
body: xmlPayload,
});
// Parse the response
const responseText = await response.text();
return this.parseResponse(responseText);
}
catch (error) {
return {
success: false,
message: `Error sending OTP: ${error instanceof Error ? error.message : String(error)}`,
};
}
}
/**
* Build the XML payload for the API request
* @param options Options for sending OTP
* @returns XML string
*/
buildXmlPayload(options) {
const { phoneNumber, message, code } = options;
// Format the message with the code if provided
const formattedMessage = code
? message.replace('{code}', code)
: message;
return `<?xml version="1.0"?>
<mainbody>
<header>
<usercode>${this.usercode}</usercode>
<password>${this.password}</password>
<msgheader>${this.msgheader}</msgheader>
<appkey>${this.appkey}</appkey>
</header>
<body>
<msg>
<![CDATA[${formattedMessage}]]>
</msg>
<no>${phoneNumber}</no>
</body>
</mainbody>`;
}
/**
* Parse the response from NetGSM API
* @param responseText Response text from the API
* @returns Parsed response object
*/
parseResponse(responseText) {
// NetGSM API returns a code in the format "00 123456" where 00 is the status code
// and 123456 is the message ID if successful
const trimmedResponse = responseText.trim();
const responseCode = trimmedResponse.substring(0, 2);
switch (responseCode) {
case NetGsmResponseCode.SUCCESS:
return {
success: true,
message: 'OTP sent successfully',
code: trimmedResponse.substring(3), // Extract the message ID
};
case NetGsmResponseCode.AUTHENTICATION_ERROR:
return {
success: false,
message: 'Authentication error. Check your credentials.',
};
case NetGsmResponseCode.PARAMETER_ERROR:
return {
success: false,
message: 'Parameter error. Check your request parameters.',
};
case NetGsmResponseCode.BALANCE_ERROR:
return {
success: false,
message: 'Balance error. Insufficient balance in your account.',
};
case NetGsmResponseCode.SYSTEM_ERROR:
return {
success: false,
message: 'System error. Please try again later.',
};
default:
return {
success: false,
message: `Unknown error. Response: ${trimmedResponse}`,
};
}
}
}
exports.NetGsmOtp = NetGsmOtp;