69 lines
1.8 KiB
JavaScript
69 lines
1.8 KiB
JavaScript
const axios = require("axios");
|
|
|
|
module.exports.sms = async (phoneNumber, message) => {
|
|
const smsService = new SmsService("QkkNxhyXZ6GtEf1soOTtikomO3mA4LaNQDH8mol8huDIwh00");
|
|
return smsService.sendMessage(phoneNumber, message);
|
|
};
|
|
class SmsService {
|
|
constructor(apiKey) {
|
|
this.API_URL = "https://api.sms.ir/v1";
|
|
this.OTP_PATTERN = "83604";
|
|
this.SMS_API_KEY = apiKey;
|
|
}
|
|
//******************************** */
|
|
|
|
//******************************** */
|
|
|
|
async sendMessage(phone, message) {
|
|
try {
|
|
const lineNumber = await this.getLineNumber();
|
|
const smsData = {
|
|
MessageTexts: [message],
|
|
LineNumber: `${lineNumber}`,
|
|
Mobiles: [phone],
|
|
};
|
|
const { data } = await axios.post(`${this.API_URL}/send/likeToLike`, smsData, {
|
|
headers: {
|
|
"X-API-KEY": this.SMS_API_KEY,
|
|
},
|
|
});
|
|
return data;
|
|
} catch (error) {
|
|
console.error("Error in sending SMS", error);
|
|
}
|
|
}
|
|
//******************************** */
|
|
async getLineNumber() {
|
|
try {
|
|
const { data } = await axios.get(`${this.API_URL}/line`, {
|
|
headers: {
|
|
"X-API-KEY": this.SMS_API_KEY,
|
|
},
|
|
});
|
|
return data.data[1] ?? data.data[0];
|
|
} catch (error) {
|
|
console.error("Error in getting line number", error);
|
|
}
|
|
}
|
|
//******************************** */
|
|
|
|
async sendOTPSms(otp, phone) {
|
|
const smsData = {
|
|
Parameters: [{ name: "code", value: `${otp}` }],
|
|
Mobile: phone,
|
|
TemplateId: this.OTP_PATTERN,
|
|
};
|
|
try {
|
|
const { data } = await axios.post(`${this.API_URL}/send/verify`, smsData, {
|
|
headers: {
|
|
"X-API-KEY": this.SMS_API_KEY,
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
return data;
|
|
} catch (error) {
|
|
console.error("Error in sending OTP SMS", error);
|
|
}
|
|
}
|
|
}
|