69 lines
2.7 KiB
Python
69 lines
2.7 KiB
Python
import logging
|
|
import zeep
|
|
from django.conf import settings
|
|
from zeep.helpers import serialize_object
|
|
from .base import SMSProvider, SMSResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
class BehinSMSProvider(SMSProvider):
|
|
"""SOAP adapter for panel.behinsms.com, isolated from application code."""
|
|
|
|
def __init__(self):
|
|
self.username = settings.BEHIN_SMS_USERNAME
|
|
self.password = settings.BEHIN_SMS_PASSWORD
|
|
self.special_number = settings.BEHIN_SMS_SPECIAL_NUMBER
|
|
self.wsdl_url = settings.BEHIN_SMS_WSDL_URL
|
|
self._client = None
|
|
|
|
def _configuration_error(self):
|
|
missing = [name for name, value in (
|
|
("BEHIN_SMS_USERNAME", self.username),
|
|
("BEHIN_SMS_PASSWORD", self.password),
|
|
("BEHIN_SMS_SPECIAL_NUMBER", self.special_number)
|
|
) if not value]
|
|
return f"تنظیمات پیامک بهین ناقص است: {', '.join(missing)}" if missing else ""
|
|
|
|
@property
|
|
def client(self):
|
|
if self._client is None:
|
|
# استفاده از تنظیمات پیشفرض Zeep (مشابه نمونهٔ کاری)
|
|
self._client = zeep.Client(wsdl=self.wsdl_url)
|
|
return self._client
|
|
|
|
def get_credit(self):
|
|
error = self._configuration_error()
|
|
if error:
|
|
raise ValueError(error)
|
|
return self.client.service.GetCredit(UserName=self.username, Password=self.password)
|
|
|
|
def send_message(self, mobile, message):
|
|
return self.send_many([mobile], message)
|
|
|
|
def send_many(self, mobile_numbers, message, is_flash=False):
|
|
error = self._configuration_error()
|
|
if error:
|
|
return SMSResult(False, error=error)
|
|
|
|
recipients = list(dict.fromkeys(str(number).strip() for number in mobile_numbers if str(number).strip()))
|
|
if not recipients:
|
|
return SMSResult(False, error="شماره گیرنده پیامک خالی است.")
|
|
|
|
try:
|
|
response = self.client.service.SendArray(
|
|
UserName=self.username,
|
|
Password=self.password,
|
|
RecipientNumber={"string": recipients},
|
|
MessageBody=message,
|
|
SpecialNumber=self.special_number,
|
|
IsFlashMessage=is_flash,
|
|
CheckingMessageID={"long": [0] * len(recipients)}
|
|
)
|
|
serialized = serialize_object(response)
|
|
if serialized is False or serialized is None:
|
|
return SMSResult(False, error="وبسرویس بهین ارسال پیامک را تأیید نکرد.")
|
|
return SMSResult(True, provider_id=str(serialized)[:500])
|
|
|
|
except Exception as exc:
|
|
logger.exception("Behin SMS delivery failed")
|
|
return SMSResult(False, error=str(exc)[:1000]) |