Is there a way to pass a custom parameter when make a call with Twilio?

左心房为你撑大大i 提交于 2021-01-27 05:40:13

问题


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?


回答1:


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.




回答2:


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);
...



回答3:


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



来源:https://stackoverflow.com/questions/30430270/is-there-a-way-to-pass-a-custom-parameter-when-make-a-call-with-twilio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!