Implement basic sipgate.io SMS sending
parent
06c3a39bde
commit
6a092c343d
@ -0,0 +1,33 @@
|
||||
from django.conf import settings
|
||||
from phonenumber_field.phonenumber import PhoneNumber
|
||||
import requests
|
||||
|
||||
|
||||
BASE_URL = "https://api.sipgate.com/v2"
|
||||
|
||||
|
||||
def send(recipient, message):
|
||||
if not settings.SIPGATE_TOKEN_ID:
|
||||
raise RuntimeError("required setting SIPGATE_TOKEN_ID is not set")
|
||||
|
||||
if not settings.SIPGATE_TOKEN:
|
||||
raise RuntimeError("required setting SIPGATE_TOKEN is not set")
|
||||
|
||||
if not settings.SIPGATE_SMS_EXTENSION:
|
||||
raise RuntimeError("required setting SIPGATE_SMS_EXTENSION is not set")
|
||||
|
||||
if not PhoneNumber.from_string(recipient).is_valid():
|
||||
raise ValueError("invalid phone number")
|
||||
|
||||
r = requests.post(
|
||||
f"{BASE_URL}/sessions/sms",
|
||||
auth=requests.auth.HTTPBasicAuth(
|
||||
settings.SIPGATE_TOKEN_ID, settings.SIPGATE_TOKEN
|
||||
),
|
||||
json={
|
||||
"smsId": settings.SIPGATE_SMS_EXTENSION,
|
||||
"recipient": recipient,
|
||||
"message": message,
|
||||
},
|
||||
)
|
||||
r.raise_for_status()
|
Loading…
Reference in New Issue