Magondu
Magondu

Reputation: 197

Using Status_Callback on twilio Rest API

I am trying to get a notification from twilio when a user hangs up or when it is completed. I have already gotten my code to call the user but i cannot get a notification when the call is complete.

from twilio.rest import TwilioRestClient

def call():
    account_sid = '************************'
    auth_token = '***********************'

    client = TwilioRestClient(account_sid, auth_token)

    call = client.calls.create(to='+254723453841',
                       from_='+1 214-390-9422',
                       url='https://dl.dropbox.com/u/*******/acceptanceOfTerms.xml',
                       if_machine='Hangup', timeout=20,  status_callback='https://dl.dropbox.com/u/*******/response.xml' )


     print call.sid

     print 'The Sid is '+ call.sid+' and the call status is '+call.status

Frrm the twilio documentation http://www.twilio.com/docs/quickstart/python/rest/call-request You are supposed to include a url for notification. My question is what is supposed to be the content of this file in the url and how i can receive a return on the status that a call has been completed or rejected etc..

Upvotes: 2

Views: 5656

Answers (3)

Robert A
Robert A

Reputation: 463

import configobj
from twilio.rest import TwilioRestClient
import time

 def call_to(url, to):

    cfg = configobj.ConfigObj("config.ini")
    sid = cfg["twilio"]["sid"]
    auth_token = cfg["twilio"]["auth_token"]
    twilio_number = cfg["twilio"]["twilio_number"]

    client = TwilioRestClient(sid, auth_token)

    call = client.calls.create(
        to=to,
        from_=twilio_number,
        url=url
    )

    for i in range(0,10):
        print i
        time.sleep(5)
        sid=call.sid
        callinfo = client.calls.get(sid)
        print callinfo.status

enter image description here

Upvotes: 0

John Mee
John Mee

Reputation: 52283

status_callback (string) – A URL that Twilio will request when the call ends to notify your app.

They don't say what it sends with the URL other than 'request' it, so you'll have to trial and error to find out. It will send back the same parameters which you sent them to open the call.

Give it a callback url - one that hits a server under your control - and see what happens. Hopefully it will pass some parameters (via GET or POST) which identifies which call has ended. You might also have to sniff the remote ip address in order to confirm it is coming from the right people.

status_method (string) – The HTTP method Twilio should use when requesting the above URL.

With this you can tell it whether to 'get' or 'post' presumably.

Upvotes: 1

gbin
gbin

Reputation: 3000

I found the answer there : http://www.twilio.com/docs/api/twiml/twilio_request

After receiving a call, requesting TwiML from your app, processing it, and finally ending the call, Twilio will make an asynchronous HTTP request to the StatusCallback URL configured for the called Twilio number (if there is one). By providing a StatusCallback URL for your Twilio number and capturing this request you can determine when a call ends and receive information about the call.

Request Parameters

The parameters Twilio passes to your application in an asynchronous request to the StatusCallback URL include all those passed in a synchronous TwiML request.

Following the link http://www.twilio.com/docs/api/twiml/twilio_request#synchronous

When Twilio receives a call for one of your Twilio numbers it makes a synchronous HTTP request to the Voice URL configured for that number, and expects to receive TwiML in response. Twilio sends the following parameters with its request as POST parameters or URL query parameters, depending on which HTTP method you've configured.

Upvotes: 3

Related Questions