Files
monica_site/site/messaging/providers/sms/smtp2go.py
T
westfarn ee28dcabab
Deploy Beta / unit-tests (push) Successful in 13s
Deploy Beta / docker (push) Successful in 18s
Deploy Beta / deploy-beta (push) Successful in 1m38s
Unify SMTP2GO email and SMS into one webhook URL.
Classify payloads on /webhooks/smtp2go/ so one SMTP2GO webhook covers both channels under the 10-webhook limit; keep /email/ and /sms/ as aliases and harden SMS event matching.
2026-08-10 11:06:37 -05:00

50 lines
1.3 KiB
Python

"""SMTP2GO SMS REST API."""
import logging
import requests
from django.conf import settings
from messaging.services import render_merge_tags
logger = logging.getLogger(__name__)
def send_sms(message) -> str:
contact = message.contact
if not contact.phone:
raise ValueError("Contact has no phone number")
api_key = settings.SMTP2GO_SMS_API_KEY
if not api_key:
raise RuntimeError("SMTP2GO_SMS_API_KEY is not configured")
campaign = message.campaign
body = message.body_snapshot or campaign.body_override or (
campaign.template.body if campaign.template else ""
)
body = render_merge_tags(body, contact)
payload = {
"api_key": api_key,
"to": contact.phone,
"text": body[:1600],
}
response = requests.post(
settings.SMTP2GO_SMS_API_URL,
json=payload,
timeout=30,
)
response.raise_for_status()
data = response.json() if response.content else {}
# Prefer SMS id fields used on webhooks (`message_id` / `sms_id`).
nested = data.get("data") if isinstance(data.get("data"), dict) else {}
return str(
nested.get("sms_id")
or nested.get("message_id")
or data.get("sms_id")
or data.get("message_id")
or data.get("request_id")
or f"sms-{message.pk}"
)