5

I'm using Twilio PHP library but the question is actually language agnostic. I tried to do something like following:

$client = new Services_Twilio('MyAccountSID', 'My auth token');
$client->account->calls->create($from_number, $to_number, $url_or_AppSID, array(
    'TwilioParam1' => 'value1',
    'TwilioParam2' => 'value2',
    'MyCustomParameter1' => 'CustomValue1',
    'MyCustomParamete2' => 'CustomValue2'
));

Then I expected to receive those parameters when Twilio requested my $url_or_AppSID (my TwiML App) but it did not happen. I know that a possible way to do that is building an URL adding those parameters in query string and passing my custom URL in $url_or_AppSID parameter but this force me to set GET method and I wanna pass my custom parameters through a POST request, not GET. Also it's probably that I have to use an AppSID which has already a request URL registered via POST.

Is there a way to pass custom parameters using POST method?

Delmo
  • 1,855
  • 1
  • 15
  • 23

3 Answers3

5

Finally, I received a response from Twilio Support:

Unfortunately you cannot add custom parameters such as below. You are correct in that the only way you would be able to do this is add the parameters to the querystring and we would just pass those parameters along to your application.

Then I sent them a feature request:

I think this should be taken in account for future release.

They replied me:

If this feature request does not already exist. I will add it.

Well stay tuned for future release.

Delmo
  • 1,855
  • 1
  • 15
  • 23
3

Twilio associates a unique call SID to each call and since this parameter is available both when the call is created and when Twilio posts a request to the handler, I can use my own database to pass the parameters.

Using a database may seem like quite an overhead, but if I also want to store the "outcome" of the call (e.g. did the customer pick up the phone? did we get to an automated machine?), I'm already implementing all the necessary functionality anyway.

So my Java code to make a call looks as follows:

Map<String, String> params = new HashMap<String, String>();
params.put("From", myTwilioPhoneNumber);
params.put("To", customerPhone);
params.put("Url", myHandlerUrl));
Call call = client.getAccount().getCallFactory().create(params);
// THE LINE BELOW IS THE KEY TO PARAMETER PASSING
db.store(call.getSid(), myCustomParametersJSON);

Now, my handler servlet code starts like this:

Sting callSid = request.getParameter("CallSid");
// Optionally sleep 20 ms to make sure that data written by
// db.store(call.getSid(), myCustomParametersJSON);
// can now be read.
CustomData customData = parseJSON(db.fetchKey(callSid));
TwiMLResponse twimlResponse = new TwiMLResponse();
Say sayMessage = new Say(makeCustomMessage(customData));
twimlResponse.append(sayMessage);
...
Scott Mayers
  • 407
  • 4
  • 16
0

There is probably no way to send custom params in the POST. The only way to send is by constructing the URL in GET.

Last parameter array can contain only specific keys i.e. SendDigits, IfMachine, Timeout.

Docs for reference

Mansoor Jafar
  • 1,342
  • 3
  • 14
  • 31
  • Thanks @mansoor. I had reviewed all the documentation and they didn't mention anything about custom parameters but I kept the hope to someone know some way to do this but finally Twilio Support confirmed our guess :( – Delmo May 26 '15 at 00:21