2
0
Fork 0

Implement basic sipgate.io SMS sending

This commit is contained in:
Luca 2022-04-27 23:25:21 +02:00
parent 06c3a39bde
commit 6a092c343d
4 changed files with 44 additions and 8 deletions

View File

@ -8,19 +8,16 @@ from dynamic_preferences.registries import global_preferences_registry
global_preferences = global_preferences_registry.manager() global_preferences = global_preferences_registry.manager()
def text_input(type=None): def text_input():
attrs = {"class": "input"} return forms.TextInput(attrs={"class": "input"})
if type != None:
attrs["type"] = type
return forms.TextInput(attrs=attrs)
def validate_allowed_countries(value): def validate_allowed_countries(value):
if not f"{value.country_code}" in global_preferences["helper__allowed_countries"]: if not f"{value.country_code}" in global_preferences["helper__allowed_countries"]:
raise ValidationError( raise ValidationError(
"Vorwahl nicht Erlaubt, bitte wende dich an den Infodesk oder schicke uns eine Mail" "Vorwahl nicht erlaubt, bitte wende dich an den Infopoint"
) )
return value return value
@ -33,7 +30,7 @@ class RegisterForm(forms.Form):
max_length=Helper.phone.field.max_length, max_length=Helper.phone.field.max_length,
label="Handynummer für Benachrichtigungen", label="Handynummer für Benachrichtigungen",
help_text="Wir nutzen deine Handynummer, um dir Benachrichtigungen zu deinen Schichten zu schicken. Wir löschen alle Daten 7 Tage nach dem Festival.", help_text="Wir nutzen deine Handynummer, um dir Benachrichtigungen zu deinen Schichten zu schicken. Wir löschen alle Daten 7 Tage nach dem Festival.",
widget=text_input("tel"), widget=text_input(),
validators=[ validators=[
validate_international_phonenumber, validate_international_phonenumber,
validate_allowed_countries, validate_allowed_countries,

View File

View File

@ -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()

View File

@ -179,3 +179,9 @@ PHONENUMBER_DEFAULT_REGION = "DE"
MESSAGE_TAGS = { MESSAGE_TAGS = {
messages.ERROR: "danger", messages.ERROR: "danger",
} }
SIPGATE_SMS_EXTENSION = getenv("SIPGATE_SMS_EXTENSION")
SIPGATE_TOKEN_ID = getenv("SIPGATE_TOKEN_ID")
SIPGATE_TOKEN = getenv("SIPGATE_TOKEN")