DickFeynman
DickFeynman

Reputation: 769

Call multiple numbers sequentially using twilio outgoing call

So I'm using the twilio api to do outgoing calls from a list

NUMS = ['xxx-xxx-xxxx', 'jjj-jjj-jjjj']
for num in NUMS:
    c = make_call(num, "Hi-how-are-you!!!")

and the make_call function contains the twillio code

def make_call(to_number, mesg):
    global FROM
    call_status = ['COMPLETED', 'FAILED', 'BUSY', 'NO_ANSWER']

    # put your own credentials here 
    ACCOUNT_SID = "--------------------" 
    AUTH_TOKEN = "--------------------" 

    client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN) 

    call = client.calls.create(
       to=to_number, 
       from_=FROM, 
       url=URL+"/voice/reply/"+mesg,  
       method="POST",  
       status_callback=URL+"/voice/status",
       status_callback_method="POST",
       timeout=10
    ) 
    return call

No idea what i'm doing wrong, but it queues BOTH and then CALLS THEM BOTH AT THE SAME TIME. If I pick up on call, the other one ends. I want to call sequentially, and putting a time.sleep() doesn't work either.

Help is appreciated.

Upvotes: 0

Views: 1650

Answers (1)

Devin Rader
Devin Rader

Reputation: 10366

Twilio evangelist here.

Each call to client.calls.create is simple an HTTP request to Twilios REST API that tells it to start an outbound phone call. Calls are made asynchronously, so if you wanted to you could call that function 10 times to simultaneously start ten separate phone calls.

If you want to make calls in a serial fashion, rather than using a loop to start the calls I would suggest starting the first call and then using the StatusCallback route handler to have Twilio tell you when that first call has completed (and why) and then in that handler, start the next call.

Each time the active call completes, Twilio will request that StatusCallback route allowing you to start the next call in your sequence.

Hope that helps.

Upvotes: 1

Related Questions