11from __future__ import absolute_import
22
33from django .conf import settings
4+ from django .contrib import messages
45from django .urls import reverse
56from django .utils import translation
67from django .utils .translation import pgettext , ugettext
@@ -38,9 +39,9 @@ class Twilio(object):
3839 ``TWILIO_CALLER_ID``
3940 Should be set to a verified phone number. Twilio_ differentiates between
4041 numbers verified for making phone calls and sending text messages.
41-
42+
4243 Optionally you may set an error message to be displayed incase of error.
43-
44+
4445 ``TWILIO_ERROR_MESSAGE``
4546 Should be set to a string with a custom text.
4647
@@ -64,10 +65,18 @@ def make_call(self, device, token):
6465
6566 def send_sms (self , device , token ):
6667 body = ugettext ('Your authentication token is %s' ) % token
67- self .client .messages .create (
68- to = device .number .as_e164 ,
69- from_ = getattr (settings , 'TWILIO_CALLER_ID' ),
70- body = body )
68+ try :
69+ self .client .messages .create (
70+ to = device .number .as_e164 ,
71+ from_ = getattr (settings , 'TWILIO_CALLER_ID' ),
72+ body = body )
73+ except Exception :
74+ twilio_error_message = getattr (settings , 'TWILIO_ERROR_MESSAGE' , None )
75+ if twilio_error_message :
76+ request = get_current_request ()
77+ messages .add_message (request , messages .ERROR , twilio_error_message )
78+ else :
79+ raise
7180
7281
7382def validate_voice_locale (locale ):
0 commit comments