0
votes

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.

1
Your question is unclear. You're printing in a completely different view from the one where you make the Twilio call, and what you're printing is what is being sent from your frontend.Daniel Roseman
@DanielRoseman at ./start-campaign it creates a call resource which calls all the numbers present in the phonebook. Using the status_callback, I tried to create a JSON response as shown in the tutorial(example 3) on Create a Call resource and specify a StatusCallbackEvent, in it sends a GET request to ./events. I tried to follow the tutorial, but I feel that I'm lost.Ayodele Ashad
But I don't understand why you think there should be a "JSON response". Twilio has called your callback endpoint with the parameters you see, and you've successfully printed two of them. What else are you expecting?Daniel Roseman
The Twilio python library is already parsing the JSON response into a python object that you can fetch properties from. The 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
@philnash How to make the API call? I know how to get the SID I'm trying to use if call.status == 'completed': pass. But it is not working and I am unable to find how to approach the problemAyodele Ashad

1 Answers

0
votes

Once the callback is received, you have the CallSid ID in the GET data. So you can use that to fetch the complete Call resource:

def events(request):
    sid = request.GET['CallSid']
    call = client.calls(sid).fetch()
    print(call.price)