Files
monica_site/site/messaging/providers/sms/smtp2go.py
T
westfarn 8680c082fe
Deploy Beta / unit-tests (push) Successful in 13s
Deploy Beta / docker (push) Successful in 17s
Deploy Beta / deploy-beta (push) Successful in 1m44s
Improve campaign personalization and contact duplicate handling.
Add merge tags for email/SMS, paginated removable recipients, PCM event panels for postcard campaigns, and a portal modal when phone/address matches an existing contact.
2026-08-10 09:47:04 -05:00

46 lines
1.1 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 {}
# SMTP2GO returns varying shapes; store a useful id when present.
return str(
data.get("data", {}).get("sms_id")
or data.get("request_id")
or f"sms-{message.pk}"
)