Skip to content

Commit

Permalink
Implement SMS provider support and enhance SMS sending logic.
Browse files Browse the repository at this point in the history
Added support for multiple SMS providers (4Pay and Wapi) with corresponding configurations in the IAP model. Refactored SMS sending logic to call provider-specific methods and handle different response formats. Updated views to include new fields for configuring SMS providers.
  • Loading branch information
dhongu committed Feb 4, 2025
1 parent 762aab4 commit 740f4c8
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 15 deletions.
76 changes: 76 additions & 0 deletions deltatech_sms/models/iap.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,85 @@
# See README.rst file on addons root folder for license details

from odoo import fields, models
import logging
import requests

_logger = logging.getLogger(__name__)

class IapAccount(models.Model):
_inherit = "iap.account"

endpoint = fields.Char()

sms_provider = fields.Selection([('4pay', 'SMS 4Pay'), ('wapi', 'SMS Wapi')], string="SMS Provider", required=True,
default="4pay")
sms_secret = fields.Char(string="SMS Secret")
sms_gateway = fields.Char(string="SMS Gateway")

def send_sms(self, phone_number, message):
"""Send SMS using IAP """
response = {}
if self.sms_provider == '4pay':
response = self._send_sms_4pay(phone_number, message)
if self.sms_provider == 'wapi':
response = self._send_sms_wapi(phone_number, message)
return response

def _send_sms_4pay(self, phone_number, message):
params = {
"servID": self.sms_gateway,
"msg_dst": phone_number,
"msg_text": message,
"API": "",
"password": self.sms_secret,
"external_messageID": 1
}
result = requests.get('https://sms.4pay.ro/smscust/api.send_sms', params=params, timeout=60)
response = result.content.decode("utf-8")

if "OK" not in response:
_logger.error(f"SMS: {response}")
res = {
"status": 500,
"message": response,
"data": False
}
else:
res = {
"status": 200,
"message": "Message has been queued for sending!",
"data": False
}

return res

def _send_sms_wapi(self, phone_number, message):

# Define the endpoint and payload
url = "https://smswapi.com/api/send/sms"
data = {
"secret": self.sms_secret,
"mode": "devices",
"phone": phone_number,
"message": message,
'device': self.sms_gateway,
"sim": 1
}

# Make the POST request
response = requests.post(url, data=data)

res = response.json()
_logger.info(f"SMS: {res}")

if response.status_code == 200:
res = response.json()
else:
res = {
"status": 500,
"message": response.content,
"data": False
}

return res

22 changes: 7 additions & 15 deletions deltatech_sms/models/sms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,19 @@ class SmsApi(models.AbstractModel):
@api.model
def _contact_iap(self, local_endpoint, params):
account = self.env["iap.account"].get("sms")
# params['account_token'] = account.account_token
# endpoint = self.env['ir.config_parameter'].sudo().get_param('sms.endpoint')


res = []
# endpoint = self.env["ir.config_parameter"].sudo().get_param("sms.endpoint", "")
# endpoint = account.endpoint or endpoint

for message in params["messages"]:
res_value = {"state": "success", "res_id": message["res_id"]}

endpoint = account.endpoint
if not endpoint:
res_value["state"] = "Endpoint is not defined."
endpoint = endpoint.format(**message)
self.env.cr.execute("select unaccent(%s);", [endpoint])
endpoint_unaccent = self.env.cr.fetchone()[0]
result = requests.get(endpoint_unaccent)
response = result.content.decode("utf-8")

if "OK" not in response:
_logger.error("SMS: %s" % response)
response = account.send_sms(message["number"], message["content"])

if response['status'] != 200:
res_value["state"] = "server_error"
res_value["error"] = response['message']

res += [res_value]

return res
3 changes: 3 additions & 0 deletions deltatech_sms/views/iap_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<field name="arch" type="xml">
<field name="account_token" position="after">
<field name="endpoint" />
<field name="sms_provider" />
<field name="sms_secret" />
<field name="sms_gateway" />
</field>
</field>
</record>
Expand Down

0 comments on commit 740f4c8

Please sign in to comment.