如何使用 Java 在 Twilio 中使用 TwiML 而不是 URL 进行出站呼叫?

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

我希望使用 Twilio 在 Java 应用程序中进行出站呼叫。我发现的所有教程都使用托管在 URL 上的静态 TwiML 文件。我还没有找到任何关于如何将 TwiML 作为传出呼叫的参数传递的文档。

我在这个 link 上找到了这个,但它没有解释如何动态呈现 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.

我试过以下方法:

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

虽然 Call.creator() 有一些 (PhoneNumber, PhoneNumber, String) 的重载方法,但其中 none 将接受 TwiML,XML 也不接受。

如何使用 TwiML 在 Java 中进行出站呼叫? 谢谢

这里是 Twilio 开发人员布道者。

您不能 make an outbound call with Twilio 同时直接传递您希望调用遵循的 TwiML。

当你拨打电话时,你需要传递一个URL。不过 URL 不必 return static TwiML。如果您想要 return 动态 TwiML,您需要设置 URL 以调用可以使用 TwiML 响应请求的 Web 应用程序的 URL。

如果您查看一些动态教程,可能会更清楚我的意思。通话接通后 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> 上的这个。

如果有帮助请告诉我。

我知道你的问题和 Phil 的回答已经有四年多了;但是,可以使用 Twilio 发出出站调用并直接传递您希望调用同时遵循的 TwiML,从 Twilio 版本 3.39.0 开始,如下所示:https://github.com/twilio/twilio-node/releases/tag/3.39.0。从该版本开始,现在可以为 TwiML 选择 URL。