I am unable to fetch the JSON response as shown in the tutorial.
I have implemented a Programmable Voice program. I want to show the user the details of the call and if I get the JSON response I would be able to get all of them (cost, duration, status, etc).
# views.py
def start_campaign(request, campaign_id):
try:
campaign = Campaign.objects.get(pk=campaign_id)
account_sid = 'XXX'
auth_token = 'XXX'
client = Client(account_sid, auth_token)
phone_numbers = Contact.objects.filter(phone_book=campaign.phone_book)
custom_url = 'http://XXX.ngrok.io/assets/' + str(campaign_id)
for phone_number in phone_numbers:
call = client.calls.create(
method='GET',
status_callback='http://XXX.ngrok.io/events',
status_callback_event='completed',
status_callback_method='GET',
url=str(custom_url),
to=str(phone_number),
from_='+1XXX'
)
detail = client.calls(str(call.sid)).fetch()
print(detail.price)
except Campaign.DoesNotExist:
raise Http404("Campaign Does Not Exist")
context = {
'all_campaigns': campaign
}
return render(request, "CallCenter/start_campaign.html", context)
def events(request):
status = request.GET.getlist('CallStatus', default=None)
duration = request.GET.getlist('CallDuration', default=None)
print("Duration:{}\nStatus:{}".format(duration, status))
return render(request, "CallCenter/events.html")
In the terminal, I get a output which prints out the status and duration of the call from
"GET /events?Called=%2BXX&ToState=&CallerCountry=US&Direction=outbound-api&Timestamp=Sat,+12+Oct+2019+19:11:50+%2B0000&CallbackSource=call-progress-events&SipResponseCode=200&Ca llerState=AL&ToZip=&SequenceNumber=0&CallSid=XXX&To=%2BXXX&CallerZip=35766&ToCountry=IN&CalledZip=&ApiVersion=2010-04-01&CalledCity=&CallStatus=completed&Duration=1&From= %2BXXX&CallDuration=5&AccountSid=XXX&CalledCountry=IN&CallerCity=ESTILLFORK&ToCity=&FromCountry=US&Caller=%2B12563804721&FromCity=ESTILLFORK&CalledState=&FromZip=35766&Fro mState=AL HTTP/1.1" 200 123
printing out
Duration:['5', '5']
Status:['completed', 'completed']
Unfortunately, I don't know how to ask for the JSON response from Twilio.
call
object you have will include all the details. (Although, since you only just created the call, it won't have a duration or price yet. You'll want to store the SID and make the API call once the call is completed.) – philnash