update: the sms service to use new sms panel
This commit is contained in:
@@ -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, `مشکلی در ارسال پیامک پیش آمده است`);
|
||||||
}
|
}
|
||||||
|
|||||||
+65
-45
@@ -1,48 +1,68 @@
|
|||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
|
|
||||||
module.exports.sms = async (phoneNumber, message) => {
|
module.exports.sms = async (phoneNumber, message) => {
|
||||||
try {
|
const smsService = new SmsService("QkkNxhyXZ6GtEf1soOTtikomO3mA4LaNQDH8mol8huDIwh00");
|
||||||
// get token
|
return smsService.sendMessage(phoneNumber, message);
|
||||||
const getToken = await axios.post("https://RestfulSms.com/api/Token", {
|
|
||||||
UserApiKey: "569ee43ed0d9ac27708ce43d",
|
|
||||||
SecretKey: "drtime@A!@#",
|
|
||||||
});
|
|
||||||
|
|
||||||
if (!getToken.data.IsSuccessful) {
|
|
||||||
throw new Error("sending sms failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
const token = getToken.data.TokenKey;
|
|
||||||
//get lines
|
|
||||||
const resData = await axios.get("https://RestfulSms.com/api/SMSLine", {
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"x-sms-ir-secure-token": token,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
// message details
|
|
||||||
const data = {
|
|
||||||
Messages: [message],
|
|
||||||
MobileNumbers: [phoneNumber],
|
|
||||||
LineNumber: resData.data.SMSLines[0].LineNumber,
|
|
||||||
SendDateTime: "",
|
|
||||||
CanContinueInCaseOfError: "false",
|
|
||||||
};
|
|
||||||
|
|
||||||
const response = await axios.post(
|
|
||||||
`http://RestfulSms.com/api/MessageSend`,
|
|
||||||
data,
|
|
||||||
{
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
"x-sms-ir-secure-token": token,
|
|
||||||
},
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
return response.data;
|
|
||||||
} catch (e) {
|
|
||||||
console.log(e);
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user