Daryl Bennett
Daryl Bennett

Reputation: 472

How to make an outbound call in Twilio with Java, using TwiML and not a URL?

I'm looking to make an outbound call in a Java application using Twilio. All of the tutorials I found used a static TwiML file hosted on a URL. I haven't been able to find any documentation on how to pass in TwiML as a parameter for an outgoing call.

I found this on this link, but it doesn't explain how to dynamically render TwiML: https://www.twilio.com/docs/guides/how-to-make-outbound-phone-calls-in-java#where-to-next

Of course, the TwiML you use to make the outbound call doesn't need to be a static file like in this example. Server-side Java code that you control can dynamically render TwiML to use for the outbound call.

I've tried the following:

PhoneNumber to = new PhoneNumber(toPhone); // Replace with your phone number
PhoneNumber from = new PhoneNumber(fromPhone); // Replace with a Twilio number
TwiML twiml = new VoiceResponse.Builder()
        .say(new Say.Builder(message).build())
        .build();
Call call = Call.creator(to, from, twiml.toXml()).create(client);

While the Call.creator()has some overloaded methods of (PhoneNumber, PhoneNumber, String), none of them will accept TwiML, nor XML.

How do make an outbound call in Java using TwiML? Thanks

Upvotes: 2

Views: 1155

Answers (2)

Vincent T Nguyen
Vincent T Nguyen

Reputation: 11

I understand that your question and Phil's answer are over four years old; however, one can place outbound calls with Twilio and directly pass the TwiML that you want the call to follow concurrently, starting in Twilio version 3.39.0 as shown here: https://github.com/twilio/twilio-node/releases/tag/3.39.0. One can now opt the URL for TwiML starting in this release.

Upvotes: 1

philnash
philnash

Reputation: 73027

Twilio developer evangelist here.

You cannot make an outbound call with Twilio and directly pass the TwiML that you want the call to follow at the same time.

When you make the outbound call you need to pass a URL. That URL does not have to return static TwiML though. If you want to return dynamic TwiML you need to set the URL for the call to a URL of a web application that can respond to the request with TwiML.

It might make it more obvious what I mean if you check out some of the dynamic tutorials. This one on building an automated phone survey dynamically generates the next question and takes input from the user in response. This click to call implementation in Java also dynamically generates the <Dial> when the call connects.

Let me know if that helps at all.

Upvotes: 3

Related Questions