Eddie E Massey III
Eddie E Massey III

Reputation: 383

How do you get the phone number of the current call in progress from Twilio?

I am trying to allow the current caller to leave a callback number after specifying so with the gather function.

I imagine I need to use

calls = client.calls.list(status="in-progress")

I'm not sure where to go from here. Is there even a way to get the sid of a current call so I can then get the phone number?

Upvotes: 1

Views: 695

Answers (3)

Eddie E Massey III
Eddie E Massey III

Reputation: 383

This is late, but I managed to grab the 'Caller' parameter from the request

Python Flask:

request.values.get("Caller", None)

Upvotes: 0

TwHello Inc
TwHello Inc

Reputation: 601

The GATHER verb has an action parameter (https://www.twilio.com/docs/api/twiml/gather#attributes-action) where you can set an endpoint to receive the results of the caller's response. This call to your endpoint will contain Twilio's standard request parameters (https://www.twilio.com/docs/api/twiml/twilio_request#synchronous-request-parameters).

Your choice would be to save the caller's phone number found in the From parameter or issue another GATHER verb to prompt the caller to enter any callback number. Repeat the prior action parameter steps.

Upvotes: 0

Rob Spectre
Rob Spectre

Reputation: 418

The calls.list() method returns a list of Call resources which you can iterate through or retrieve by index.

from twilio.rest import TwilioRestClient

ACCOUNT_SID = ACxxxxxxxxxx
AUTH_TOKEN = yyyyyyyyyyyyy

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
calls = client.calls.list(status='in-progress')

for call in calls:
    print(call.sid)
first_call = calls[0]

The phone numbers related to the call in-progress are available via the to and from_ attributes. For your use case, I suspect the phone number you are looking for would be available here:

from twilio.rest import TwilioRestClient

ACCOUNT_SID = ACxxxxxxxxxx
AUTH_TOKEN = yyyyyyyyyyyyy

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
calls = client.calls.list(status='in-progress')

first_call = calls[0]
to_number = first_call.to
from_number = first_call.from_

In fact you can see all the attributes available for a call in the Call resource's __dict__ method.

from twilio.rest import TwilioRestClient

ACCOUNT_SID = ACxxxxxxxxxx
AUTH_TOKEN = yyyyyyyyyyyyy

client = TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN)
calls = client.calls.list(status='in-progress')

first_call = calls[0]
print(first_call.__dict__)

Hope that helps!

Upvotes: 2

Related Questions