Skip to content

Commit

Permalink
Add SMS provider integration with 4Pay and Wapi
Browse files Browse the repository at this point in the history
Introduced fields and logic to support SMS sending via 4Pay and Wapi providers. Updated IAP account model with necessary fields and implemented methods to handle provider-specific SMS transmission. Refactored SMS API logic to utilize the new `send_sms` method for enhanced flexibility.
  • Loading branch information
dhongu committed Feb 4, 2025
1 parent 138dc9f commit 640165c
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 20 deletions.
78 changes: 77 additions & 1 deletion 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

28 changes: 10 additions & 18 deletions deltatech_sms/models/sms_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,18 @@ class SmsApi(BaseSmsApi):
def _contact_iap(self, local_endpoint, params, timeout=15):
account = self.env["iap.account"].get("sms")


res = []

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

endpoint = account.endpoint
if not endpoint:
res_value["state"] = "Endpoint is not defined."

for number in message["numbers"]:
res_value["uuid"] = number["uuid"]
endpoint_number = endpoint.format(number=number["number"], content=message["content"])
self.env.cr.execute("select unaccent(%s);", [endpoint_number])
endpoint_unaccent = self.env.cr.fetchone()[0]
result = requests.get(endpoint_unaccent, timeout=60)
response = result.content.decode("utf-8")

if "OK" not in response:
_logger.error(f"SMS: {response}")
res_value["state"] = "server_error"
res += [res_value]
res_value = {"state": "success", "res_id": message["res_id"]}

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
4 changes: 3 additions & 1 deletion deltatech_sms/views/iap_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<field name="inherit_id" ref="iap.iap_account_view_form" />
<field name="arch" type="xml">
<field name="service_name" 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 640165c

Please sign in to comment.