I am trying to send sms using twilio and django 1.8.2. I am facing a problem when there are multiple calls to send sms within the same function.
This is the code snippet to send an sms to any particular phone number:
def send_twilio_message(to_number, body):
client = twilio.rest.TwilioRestClient(
settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
return client.messages.create(
body=body,
to=to_number,
from_=settings.TWILIO_PHONE_NUMBER
)
When I make two requests to this function like this:
def some_function():
send_twilio_message(number1, "text 1")
send_twilio_message(number2, "text 2")
I am getting this error:
Traceback:
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py"
in waitlist_phq
782. exclusion_code = waitlist_alert(wl.participant,
phq_score)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py"
in waitlist_alert
857. send_twilio_message(phonenumber, 'Looks like your
child maybe having suicidal thoughts. Get in touch with your child
ASAP. - TreadWill.')
File "/home/arka/Project/TreadWill/dreamhost/CCBT/sendsms/utils.py" in
send_twilio_message
13. from_=settings.TWILIO_PHONE_NUMBER
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/messages.py" in create
122. return self.create_instance(kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in create_instance
365. data=transform_params(body))
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in request
200. resp = make_twilio_request(method, uri, auth=self.auth,
**kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_twilio_request
152. resp = make_request(method, uri, **kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_request
117. resp, content = http.request(url, method, headers=headers,
body=data)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in request
1314. (response, content) = self._request(conn, authority, uri,
request_uri, method, body, headers, redirections, cachekey)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _request
1064. (response, content) = self._conn_request(conn, request_uri,
method, body, headers)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _conn_request
987. conn.connect()
File "/usr/lib/python3.5/http/client.py" in connect
1260. server_hostname=server_hostname)
File "/usr/lib/python3.5/ssl.py" in wrap_socket
377. _context=self)
File "/usr/lib/python3.5/ssl.py" in __init__
752. self.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
988. self._sslobj.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
633. self._sslobj.do_handshake()
Exception Type: SSLError at
/iitk/phq/NnBLRUEsbm5jbERmfGZWNkt6RVNPcEFBNnplNkA4RVFOTkN4TnI=/
Exception Value: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify
failed (_ssl.c:645)
But, when I do this:
import time
def some_function():
send_twilio_message(number1, "text 1")
time.sleep(60)
send_twilio_message(number2, "text 2")
it is working fine. The problem with this approach is that the user might close the browser if the page keeps loading for 60 seconds (I've tried for less than 60 seconds, I get the same error).
I've tried disabling SSL verification in my Twilio account, but that didn't work.
I've looked into other answers, and I've gone through Twilio's documentation. I haven't been able to figure out a good solution. Any help will be appreciated.
Edit: I was trying all this on my localserver. When I tried the code:
def some_function():
send_twilio_message(number1, "text 1")
send_twilio_message(number2, "text 2")
on a hosted server, it worked fine. So, I think the problem might be due to my institute's network. The problem is resolved for me, but I still haven't been able to figure out why I was facing the issue. If anyone has any idea why this might be happening, please post your answer.