How to Use Twilio to Connect Patients with Investigators in Django

by Applied Informatics

Twilio is a cloud communications company based in San Francisco, California. Twilio allows software developers to programmatically make and receive phone calls as well as send and receive text messages using its web service APIs. Twilio’s services are accessed over HTTP and are billed based on usage.

We at Applied, have used the twilio system for our award winning healthcare system iConnect to connect patients to clinical trial sites and investigators using a proper and dedicated channel system.

For this we used integrated twilio application into our django views.

There are three phases in this process.

  1. The patient calls the investigator using call modal popup integrated.
  2. Twilio connects patient to the investigator using its secure channel.
  3. Getting status of the call by grabbing data from twilio json.

Step 1 :

Installing Twilio :

pip install twilio

Create a Django view that uses twilio call system for our django application.

Upon navigating to proper url. The Twilio based credentials which we want for our call are:

from import TwilioRestClient
client = TwilioRestClient()
doctors_number = /* getting those from database of site_investigators /*
doctors_name = /* getting those from database of site_investigators /*
patient_name = request.POST.get('patient_email','') /* getting from the form which is submitted by patient/*
patient_number = request.POST.get('patient_number','')
url = "%s/calling/%s/%s/%s/" % (BASE_URL, doctors_number, doctors_name, patient_name)
client = TwilioRestClient(TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
call = client.calls.create(to=patient_number, from_=TWILIO_NUMBER, url=url, method="GET")
to_return = {'call_id':call.sid}

The patient in this way calls the investigator directly using twilio service.

Step 2:

Grabbing status for tracking is the second step which allows to record patient information regarding call.
For that we are using second view as call status view in django. Information which we get as json from the call are as follows:

client = TwilioRestClient(settings.ACCOUNT_SID, settings.ACCOUNT_TOKEN)
call = client.calls.get(call_id) /* this is to get the call_id of the call recently being made/*
then we get call.status from it as well as call.duration which we record in our database as patient information.

The voice which is used during the transfer call is maintained by the views as under:

def twillo_initial(request):
response = """<Response>
<Say voice="woman">Wellcome to Dory by </Say>
return HttpResponse(response, mimetype="application/xml")

Please feel free to reach out to and share your queries and experiences with Twilio.

For more information: please visit and

Leave a Reply

Your email address will not be published. Required fields are marked *

Healthcare Informatics Solutions

Healthcare IT news, developments, opinions and solutions

Contact us now

Popular Posts