I am using ExecutionCreator to use twilio IVR call service
Execution execution = new ExecutionCreator(pathFlowSid, new receiverPhoneNo, new senderPhoneNo)
.setParameters(request.getParametersMap()) //setting request params
.create(twilioClient);
Now I want to get the status of the IVR call and this is done by using Call resource and using statusCallback parameter as given in this https://www.twilio.com/docs/voice/api/call-resource#statuscallback, but there is no way of setting request parameters on Call resource as was possible with Execution resource(please refer to code added).
Is there any way I can get both these functionalities like statusCallback as well as setting request parameters ?
Related
How can I run a flow from another flow in Twilio Studio Flow?
Help with defining the To and From HTTP parameters:
I am a beginner in programming so I am failing to understand the brief notes given in support docs, namely specifying HTTP additional parameters for "To" and "From".
Additional details from comment:
I am trying to run REST API triggered Flow B from primary Flow A by using an http request widget in Flow A in the format below: (as suggested in a similar problem posted on this portal) Widget: HTTP Request [ACCOUNT_SID:AUTH_TOKEN#studio.twilio.com/v1/Flows/THE_OTHER_STUDIO_FLOW_SID/Executions][2] Content Type: Form URL Encoded KEY:VALUES To:+1234567890 From:+2773123456 I am getting error 401. I tried to swap the To number with the From number without success
There are 2 ways you can trigger one twilio studio flow from another
Method 1:
Use the TwiML Redirect Widget. Place the widget where you need it and specify the target studio flow URL there. Studio URLs have the following format
https://webhooks.twilio.com/v1/Accounts/{AccountSid}/Flows/{FlowSid}
Method 2:
Do the same as above programmatically. You can send twilio a twiML response such as the one below
let twiml = new Twilio.twiml.VoiceResponse();
if (something) {
twiml.redirect({
method: 'POST'
}, 'https://webhooks.twilio.com/v1/Accounts/{AccountSid}/Flows/{FlowSid1}');
} else {
twiml.redirect({
method: 'POST'
}, 'https://webhooks.twilio.com/v1/Accounts/{AccountSid}/Flows/{FlowSid2}');
}
For more info, check out https://www.twilio.com/docs/voice/twiml/redirect
Assuming you are not trying to bridge the call between the two flows, this should be possible. To simplify:
You have a call come in on Flow A ("Incoming Call" trigger on Flow A).
Flow A executes its logic.
That logic triggers Flow B by calling its REST API endpoint so that it makes a new outbound call ("REST API" trigger on Flow B).
This last thing is the hard part. Make sure you are looking at the docs for the REST API Execution resource. To trigger a new flow, you need to make a POST request which supplies the To and From parameters.
If you are a beginner at programming, it might be helpful for you to start with a separate HTTP client like Postman to start to get familiar with the structure of an HTTP request, and learn the full extent of what is required to successfully make this API request before you start trying to cram it into Studio and automate it.
That said, this request should be possible to do within the Studio Make HTTP Request widget. If you make your content type Application/JSON, you can pass the To/From parameters directly in a JSON-formatted request body, like this:
{
"To": "+19995551234",
"From": "+12345556789"
}
To be perfectly honest, I don't know what the widget means by "Http Parameters". This could be HTTP Headers, URI parameters, or something else. I think the JSON form is clearer.
I came across the same situation. The solution for authentication is to change the url to include AccountSid and AuthToken
https://[AccountSid]:[AuthToken]#studio.twilio.com/v2/Flows/[SID]/Executions
Instead of Application / Json, use Form Parameters. Then add individual parameters below, for To, From, and Parameters​ (JSON string) for other variables.
I'm using the Twilio Voice API to create an outbound call:
$call = $twilio->calls->create(
"+14155551212", // to
"+15017122661", // from
array(
"url" => "http://demo.twilio.com/docs/voice.xml"
)
);
As you can see, the script used for the call is accessed with the "url" parameter pointing to a XML file.
The XML is hard coded though. Is there a way to write "inline" TwiML inside this create function so I can pass in PHP directly to make the script dynamic? Then I wouldnt be using a hard coded XML file but dynamic PHP instead.
For example, if I have:
$customer_name = $customer['name'];
I'd like to be able to pass this into the script to be read when a listens to the call.
How can I accomplish this?
Twilio developer evangelist here.
There is currently not a way to create a call and directly give it static TwiML to execute.
If you don't want to host static TwiML you could choose to host your TwiML in Twilio's TwiML Bins.
If you want the TwiML to be dynamic, but you don't want to host it yourself, you could use Twilio Functions to respond to your webhook.
Let me know if that helps at all.
So I really don't know what the problem is here, I've tried many things, but I can't get the Twilio request hashes to match up. Let me explain.
I decided to implement an instance of Twilio's RequestValidator to ensure the requests were coming from Twilio. But after following the tutorial here: https://www.twilio.com/docs/usage/security?code-sample=code-validate-signature-of-request-1&code-language=PHP&code-sdk-version=5.x
The validator is only returning false. Here is the code that I used:
$url = 'https://example.com/api/endpoint/to/endpoint/';
$request_params = $_REQUEST;
$twilio_validator = new RequestValidator('myauthtoken');
if (!$twilio_validator->validate($_SERVER['HTTP_X_TWILIO_SIGNATURE'], $url, $request_params)) {
throw new CallException('Not from Twilio');
}
Even though the URL is an example, that is exactly how I have the actual URL formatted...no port, basic auth, or fragment. Just the protocol, domain, and path with a trailing "/". In addition, the URL is the exact VoiceURL I set when I set up this Twilio App (this is calling the VoiceURL to one of my Twilio Apps).
My auth token is the auth token for my whole account
The request params is where I'm sure I'm messing something up. Twilio is making a GET request to this endpoint, and I tried using the $_GET superglobal as well, to no avail. I'm using $_REQUEST here because of this issue: https://github.com/twilio/twilio-php/issues/510 and because I thought it would be the best choice. I have also tried using file_get_contents('php://input') to the exact same problem (the hashes not matching, ultimately).
I even forked and opened a PR on the PHP SDK to update the class a little bit, just to see if I could learn any more...so I know the class and it's methods pretty well...I just don't see my issue.
What am I doing wrong here to make it so that the RequestValidator isn't validating that the requests from Twilio are coming from Twilio?
So after a lot of research and working with Twilio help, I figured out the answer to my question.
When Twilio is making a GET request to my server, you aren't supposed to pass the GET parameters as the third parameter to the validate method on the RequestValidator class. When Twilio is making a GET request to your server, validating actually needs to look like this:
// this is the interesting part...you don't even set the pathname on the domain...
// EVEN IF YOU THE PATHNAME IS SET IN YOUR VOICE URL.
// This is because of the different way the RequestValidator handles GET and POST params
$domain = 'https://example.com'; // make sure to add no trailing '/'
// setting up the RequestValidator
$twilio_validator = new RequestValidator('myauthtoken');
// figuring out if the request is from twilio
$is_from_twilio = $twilio_validator->validate(
// the signature header that Twilio sends
$_SERVER['HTTP_X_TWILIO_SIGNATURE'],
// The domain name CONCATENATED to the Request URI. $_SERVER['REQUEST_URI'] holds everything that comes after the domain name in a URL (pathname, query parameters, and fragment)
$domain.$_SERVER['REQUEST_URI']
// if the request is a get request, as mine are, there is no need for the third parameter
);
// resolving the response
if (!$is_from_twilio) {
echo 'Not from Twilio';
exit;
}
Refer to the comments in the code for a more in depth discussion on the code at work here..
I believe Twilio's outbound call could be HTTP POST request. Is there a way I can pass my custom POST body (json etc) when making outbound voice call request? I'm writing a generic call center where I would like to pass the conversation workflow when making outbound calls so that the code which receives the call knows how to run the conversation. I looked at the documentation (https://www.twilio.com/docs/api/twiml/twilio_request) and looks like we can only pass standard parameters (from, to etc). Thanks for any help.
I believe the only parameter you can customize is the Url Parameter.
Your JSON is pretty much a string (you might have to url encode it and also watch for the length), but you could put it in the query string of the Url Parameter.
?json=url_encoded_json
What is the difference between DialCallStatus and CallStatus?
I saw when you use Dial verb with the action attribute, we can get the DialCallStatus and the CallStatus, what is the differences between these values?
Twilio evangelist here.
DialCallStatus tells you the status of the call intiated by Twilio when you use the <Dial> verb and will only be included as as an HTTP parameter when Twilio requests the URL you have specified in the <Dial> verbs action attribute.
So for example, if you make a phone call into a Twilio number you can see what the CallStatus for that particular call is. Once that call connects, if you then use the <Dial> verb to have Twilio make a outbound phone call and bridge it to the first call that second call has its own status, which is communicated to you using the DialCallStatus parameter. This is because when the second call ends, the first call can continue on if you tell it to.
Does that make sense?
Hope that helps.