Implement basic sipgate.io SMS sending
This commit is contained in:
parent
06c3a39bde
commit
6a092c343d
|
@ -8,19 +8,16 @@ from dynamic_preferences.registries import global_preferences_registry
|
|||
global_preferences = global_preferences_registry.manager()
|
||||
|
||||
|
||||
def text_input(type=None):
|
||||
attrs = {"class": "input"}
|
||||
if type != None:
|
||||
attrs["type"] = type
|
||||
|
||||
return forms.TextInput(attrs=attrs)
|
||||
def text_input():
|
||||
return forms.TextInput(attrs={"class": "input"})
|
||||
|
||||
|
||||
def validate_allowed_countries(value):
|
||||
if not f"{value.country_code}" in global_preferences["helper__allowed_countries"]:
|
||||
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
|
||||
|
||||
|
||||
|
@ -33,7 +30,7 @@ class RegisterForm(forms.Form):
|
|||
max_length=Helper.phone.field.max_length,
|
||||
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.",
|
||||
widget=text_input("tel"),
|
||||
widget=text_input(),
|
||||
validators=[
|
||||
validate_international_phonenumber,
|
||||
validate_allowed_countries,
|
||||
|
|
|
@ -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()
|
|
@ -179,3 +179,9 @@ PHONENUMBER_DEFAULT_REGION = "DE"
|
|||
MESSAGE_TAGS = {
|
||||
messages.ERROR: "danger",
|
||||
}
|
||||
|
||||
SIPGATE_SMS_EXTENSION = getenv("SIPGATE_SMS_EXTENSION")
|
||||
|
||||
SIPGATE_TOKEN_ID = getenv("SIPGATE_TOKEN_ID")
|
||||
|
||||
SIPGATE_TOKEN = getenv("SIPGATE_TOKEN")
|
||||
|
|
Loading…
Reference in New Issue