update: the sms service to use new sms panel

This commit is contained in:
mahyargdz
2025-02-16 12:41:26 +03:30
parent 0f6dd5826e
commit 86edb153a2
3 changed files with 66 additions and 46 deletions
+1 -1
View File
@@ -190,7 +190,7 @@ module.exports.otp = [
/** send sms to user */ /** send sms to user */
const smsResult = await sms(phone, message); const smsResult = await sms(phone, message);
if (!smsResult.IsSuccessful) { if (!smsResult) {
console.log(smsResult); console.log(smsResult);
return res500(res, `مشکلی در ارسال پیامک پیش آمده است`); return res500(res, `مشکلی در ارسال پیامک پیش آمده است`);
} }
+57 -37
View File
@@ -1,48 +1,68 @@
const axios = require("axios"); const axios = require("axios");
module.exports.sms = async (phoneNumber, message) => { 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 { try {
// get token const lineNumber = await this.getLineNumber();
const getToken = await axios.post("https://RestfulSms.com/api/Token", { const smsData = {
UserApiKey: "569ee43ed0d9ac27708ce43d", MessageTexts: [message],
SecretKey: "drtime@A!@#", LineNumber: `${lineNumber}`,
}); Mobiles: [phone],
};
if (!getToken.data.IsSuccessful) { const { data } = await axios.post(`${this.API_URL}/send/likeToLike`, smsData, {
throw new Error("sending sms failed");
}
const token = getToken.data.TokenKey;
//get lines
const resData = await axios.get("https://RestfulSms.com/api/SMSLine", {
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": this.SMS_API_KEY,
"x-sms-ir-secure-token": token,
}, },
}); });
return data;
// message details } catch (error) {
const data = { console.error("Error in sending SMS", error);
Messages: [message], }
MobileNumbers: [phoneNumber], }
LineNumber: resData.data.SMSLines[0].LineNumber, //******************************** */
SendDateTime: "", async getLineNumber() {
CanContinueInCaseOfError: "false", try {
}; const { data } = await axios.get(`${this.API_URL}/line`, {
const response = await axios.post(
`http://RestfulSms.com/api/MessageSend`,
data,
{
headers: { headers: {
"Content-Type": "application/json", "X-API-KEY": this.SMS_API_KEY,
"x-sms-ir-secure-token": token,
}, },
});
return data.data[1] ?? data.data[0];
} catch (error) {
console.error("Error in getting line number", error);
} }
); }
//******************************** */
return response.data; async sendOTPSms(otp, phone) {
} catch (e) { const smsData = {
console.log(e); Parameters: [{ name: "code", value: `${otp}` }],
throw e; 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);
}
}
}
View File