From 86edb153a297f55d458e17fd9a0ac4ab012dbfce Mon Sep 17 00:00:00 2001 From: mahyargdz Date: Sun, 16 Feb 2025 12:41:26 +0330 Subject: [PATCH] update: the sms service to use new sms panel --- server/controllers/userController.js | 2 +- server/plugins/sms.js | 110 ++++++++++++++++----------- static/uploads/images/news/ds.jpg | 0 3 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 static/uploads/images/news/ds.jpg diff --git a/server/controllers/userController.js b/server/controllers/userController.js index 68d21cd..d181d0b 100644 --- a/server/controllers/userController.js +++ b/server/controllers/userController.js @@ -190,7 +190,7 @@ module.exports.otp = [ /** send sms to user */ const smsResult = await sms(phone, message); - if (!smsResult.IsSuccessful) { + if (!smsResult) { console.log(smsResult); return res500(res, `مشکلی در ارسال پیامک پیش آمده است`); } diff --git a/server/plugins/sms.js b/server/plugins/sms.js index 46e691a..f83a3fe 100644 --- a/server/plugins/sms.js +++ b/server/plugins/sms.js @@ -1,48 +1,68 @@ const axios = require("axios"); + module.exports.sms = async (phoneNumber, message) => { - try { - // get token - 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; - } + 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 { + 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); + } + } +} diff --git a/static/uploads/images/news/ds.jpg b/static/uploads/images/news/ds.jpg new file mode 100644 index 0000000..e69de29