const axios = require('axios')
// const accountInfo = {
// Username: 'asanservice.sms@gmail.com',
// Password: '9u&8[Ve9',
// Number: '5000467341',
// url: 'http://www.afe.ir/WebService/V4/BoxService.asmx'
// }
/// /////// http post soap method
// module.exports.SMS = (mobileNumbersArray, message) => {
// return new Promise((resolve, reject) => {
// function renderMobileNumbers(array) {
// return array.map(item => `${item}`).join('')
// }
// const xml = `
//
//
//
// ${accountInfo.Username}
// ${accountInfo.Password}
// ${accountInfo.Number}
//
// ${renderMobileNumbers(mobileNumbersArray)}
//
// ${message}
// 1
//
//
// `
// console.log('here')
// axios
// .post(accountInfo.url, xml, {
// headers: {
// 'Content-Type': 'text/xml; charset=utf-8',
// SOAPAction: 'http://www.afe.ir/SendMessage'
// }
// })
// .then(res => {
// resolve(res)
// })
// .catch(err => {
// console.log(
// 'this is sms module error ------------------ status code: ',
// err.response.status,
// 'error message: ',
// err.response.data
// )
// reject(err)
// })
// })
// }
module.exports.SMS = async (mobile, message) => {
const smsService = new SmsService('QkkNxhyXZ6GtEf1soOTtikomO3mA4LaNQDH8mol8huDIwh00')
return await smsService.sendOTPSms(message, mobile)
}
class SmsService {
constructor(apiKey) {
this.API_URL_FAST = 'https://api.sms.ir/v1'
this.OTP_PATTERN = '83604'
this.SMS_API_KEY = apiKey
}
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_FAST}/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)
}
}
}