update: the sms service to use new sms panel
deploy to danak / build_and_deploy (push) Has been cancelled
deploy to danak / build_and_deploy (push) Has been cancelled
This commit is contained in:
+60
-35
@@ -1,43 +1,68 @@
|
||||
const axios = require('axios')
|
||||
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 {
|
||||
|
||||
// get token
|
||||
const getToken = await axios.post('https://RestfulSms.com/api/Token', {
|
||||
UserApiKey: '4d1a116be65a637f9a8636c5',
|
||||
SecretKey: 'BargRestaurant@A!@#'
|
||||
})
|
||||
|
||||
if (!getToken.data.IsSuccessful) {
|
||||
return false
|
||||
}
|
||||
|
||||
const token = getToken.data.TokenKey
|
||||
|
||||
//get lines
|
||||
const getLineNumber = await axios.get('https://RestfulSms.com/api/SMSLine', {
|
||||
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: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-sms-ir-secure-token': token
|
||||
"X-API-KEY": this.SMS_API_KEY,
|
||||
},
|
||||
});
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error("Error in sending SMS", error);
|
||||
}
|
||||
})
|
||||
|
||||
// message details
|
||||
const data = {
|
||||
Messages: [message],
|
||||
MobileNumbers:Array.isArray(phoneNumber) ? phoneNumber : [phoneNumber],
|
||||
LineNumber : getLineNumber.data.SMSLines[0].LineNumber,
|
||||
SendDateTime: '',
|
||||
CanContinueInCaseOfError: 'false'
|
||||
}
|
||||
|
||||
return axios.post(`http://RestfulSms.com/api/MessageSend`, data, {
|
||||
//******************************** */
|
||||
async getLineNumber() {
|
||||
try {
|
||||
const { data } = await axios.get(`${this.API_URL}/line`, {
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-sms-ir-secure-token': token
|
||||
}
|
||||
})
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
"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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user