Search in sources :

Example 1 with VoiceResponse

use of com.twilio.twiml.VoiceResponse in project api-snippets by TwilioDevEd.

the class TwilioHandleRecordingServlet method service.

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String recordingUrl = request.getParameter("RecordingUrl");
    Say sayHello = new Say.Builder("Listen to your recorded message.").build();
    Say sayGoodbye = new Say.Builder("Goodbye").build();
    VoiceResponse twiml;
    if (recordingUrl != null) {
        Play play = new Play.Builder(recordingUrl).build();
        twiml = new VoiceResponse.Builder().say(sayHello).play(play).say(sayGoodbye).build();
    } else {
        response.sendRedirect("/twiml");
        return;
    }
    response.setContentType("application/xml");
    try {
        response.getWriter().print(twiml.toXml());
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
}
Also used : Play(com.twilio.twiml.voice.Play) VoiceResponse(com.twilio.twiml.VoiceResponse) Say(com.twilio.twiml.voice.Say) TwiMLException(com.twilio.twiml.TwiMLException)

Example 2 with VoiceResponse

use of com.twilio.twiml.VoiceResponse in project api-snippets by TwilioDevEd.

the class Example method service.

public void service(final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    Dial dial = new Dial.Builder().queue(new Queue.Builder("Queue Demo").build()).build();
    VoiceResponse voiceResponse = new VoiceResponse.Builder().dial(dial).build();
    try {
        response.getWriter().print(voiceResponse.toXml());
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
}
Also used : Dial(com.twilio.twiml.voice.Dial) VoiceResponse(com.twilio.twiml.VoiceResponse) Queue(com.twilio.twiml.voice.Queue) TwiMLException(com.twilio.twiml.TwiMLException)

Example 3 with VoiceResponse

use of com.twilio.twiml.VoiceResponse in project api-snippets by TwilioDevEd.

the class IncomingCallServlet method doPost.

// Handle HTTP POST to /voice
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Get the city from the incoming call (if available)
    String fromCity = request.getParameter("FromCity");
    if (fromCity == null) {
        fromCity = "home slice";
    }
    // Create a TwiML builder object
    VoiceResponse twiml = new VoiceResponse.Builder().say(new Say.Builder(String.format("Never gonna give you up, %s!", fromCity)).build()).play(new Play.Builder("https://demo.twilio.com/docs/classic.mp3").build()).build();
    // Render TwiML as XML
    response.setContentType("text/xml");
    try {
        response.getWriter().print(twiml.toXml());
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
}
Also used : Play(com.twilio.twiml.voice.Play) VoiceResponse(com.twilio.twiml.VoiceResponse) TwiMLException(com.twilio.twiml.TwiMLException)

Example 4 with VoiceResponse

use of com.twilio.twiml.VoiceResponse in project twilio-java by twilio.

the class TwiMLResponseExample method main.

/**
 * TwiML example usage.
 *
 * @param args command line args
 * @throws TwiMLException if cannot generate TwiML
 */
public static void main(final String[] args) throws TwiMLException, URISyntaxException {
    // Say
    Say say = new Say.Builder("Hello World!").voice(Say.Voice.MAN).loop(5).build();
    VoiceResponse response = new VoiceResponse.Builder().say(say).build();
    System.out.println(response.toXml());
    // Gather, Redirect
    Gather gather = new Gather.Builder().numDigits(10).say(new Say.Builder("Press 1").build()).build();
    Redirect redirect = new Redirect.Builder(new URI("https://example.com")).build();
    response = new VoiceResponse.Builder().gather(gather).redirect(redirect).build();
    System.out.println(response.toXml());
    // Conference
    Conference conference = new Conference.Builder("my room").beep(Conference.Beep.TRUE).build();
    Dial dial = new Dial.Builder().callerId("+1 (555) 555-5555").action(new URI("https:///example.com")).hangupOnStar(true).conference(conference).build();
    response = new VoiceResponse.Builder().dial(dial).build();
    System.out.println(response.toXml());
}
Also used : VoiceResponse(com.twilio.twiml.VoiceResponse) Say(com.twilio.twiml.voice.Say) Redirect(com.twilio.twiml.voice.Redirect) URI(java.net.URI) Conference(com.twilio.twiml.voice.Conference) Gather(com.twilio.twiml.voice.Gather) Dial(com.twilio.twiml.voice.Dial)

Example 5 with VoiceResponse

use of com.twilio.twiml.VoiceResponse in project api-snippets by TwilioDevEd.

the class SendSmsDuringCall method service.

// service() responds to both GET and POST requests.
// You can also use doGet() or doPost()
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Create a dict of people we know.
    HashMap<String, String> people = new HashMap<String, String>();
    people.put("+14158675308", "Curious George");
    people.put("+12349013030", "Boots");
    people.put("+12348134522", "Virgil");
    // if the sender is known, then greet them by name
    String name = people.get(Request.getParameter("From")) != null ? people.get(Request.getParameter("From")) : "Monkey";
    try {
        Say say = new Say.Builder().build(String.format("Hello! %s", name));
        Sms sms = new Sms.Builder().build(String.format("%s, thanks for the call!", name));
        VoiceResponse twiml = new VoiceResponse.Builder().say(say).sms(sms).build();
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXml());
}
Also used : VoiceResponse(com.twilio.twiml.VoiceResponse) Sms(com.twilio.twiml.voice.Sms) Say(com.twilio.twiml.voice.Say) TwiMLException(com.twilio.twiml.TwiMLException)

Aggregations

VoiceResponse (com.twilio.twiml.VoiceResponse)7 TwiMLException (com.twilio.twiml.TwiMLException)5 Say (com.twilio.twiml.voice.Say)5 Play (com.twilio.twiml.voice.Play)3 Dial (com.twilio.twiml.voice.Dial)2 Conference (com.twilio.twiml.voice.Conference)1 Gather (com.twilio.twiml.voice.Gather)1 Queue (com.twilio.twiml.voice.Queue)1 Redirect (com.twilio.twiml.voice.Redirect)1 Sms (com.twilio.twiml.voice.Sms)1 URI (java.net.URI)1