Files
asan-service/server/plugins/SMS_Module.js
T
2025-02-16 10:58:19 +03:30

87 lines
2.6 KiB
JavaScript

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 => `<string>${item}</string>`).join('')
// }
// const xml = `<?xml version="1.0" encoding="utf-8"?>
// <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
// <soap:Body>
// <SendMessage xmlns="http://www.afe.ir/">
// <Username>${accountInfo.Username}</Username>
// <Password>${accountInfo.Password}</Password>
// <Number>${accountInfo.Number}</Number>
// <Mobile>
// ${renderMobileNumbers(mobileNumbersArray)}
// </Mobile>
// <Message>${message}</Message>
// <Type>1</Type>
// </SendMessage>
// </soap:Body>
// </soap:Envelope>`
// 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)
}
}
}