37 lines
1.7 KiB
Python
37 lines
1.7 KiB
Python
import logging
|
|
|
|
import requests
|
|
from django.conf import settings
|
|
|
|
from apps.accounts.models import normalize_mobile
|
|
from .base import SMSProvider, SMSResult
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ZeroZeroNinetyEightSMSProvider(SMSProvider):
|
|
"""Adapter for the 0098SMS HTTP gateway."""
|
|
|
|
def __init__(self):
|
|
self.username = settings.SMS0098_USERNAME
|
|
self.password = settings.SMS0098_PASSWORD
|
|
self.sender = settings.SMS0098_FROM
|
|
self.api_url = settings.SMS0098_API_URL
|
|
|
|
def send_message(self, mobile, message):
|
|
missing = [name for name, value in (("SMS0098_USERNAME", self.username), ("SMS0098_PASSWORD", self.password), ("SMS0098_FROM", self.sender)) if not value]
|
|
if missing:
|
|
return SMSResult(False, error=f"تنظیمات 0098SMS ناقص است: {', '.join(missing)}")
|
|
try:
|
|
recipient = int(normalize_mobile(str(mobile)).removeprefix("+98"))
|
|
response = requests.get(self.api_url, params={"USERNAME": self.username, "PASSWORD": self.password, "FROM": self.sender, "TO": recipient, "TEXT": str(message)}, timeout=settings.INTEGRATION_HTTP_TIMEOUT_SECONDS)
|
|
response.raise_for_status()
|
|
# This legacy endpoint may append an HTML page containing the
|
|
# original query string. Persist only its first scalar result so
|
|
# credentials can never leak into notification logs.
|
|
provider_id = response.text.strip().splitlines()[0][:100]
|
|
return SMSResult(True, provider_id=provider_id)
|
|
except (ValueError, requests.RequestException) as exc:
|
|
logger.exception("0098SMS delivery failed")
|
|
return SMSResult(False, error=str(exc)[:1000])
|