Search in sources :

Example 1 with TwiMLException

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

the class MmsHelloMonkey 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 {
    try {
        Message sms = new Message.Builder().body(new Body("Hello, Mobile Monkey")).media(new Media("https://demo.twilio.com/owl.png")).build();
        MessagingResponse twiml = new MessagingResponse.Builder().message(sms).build();
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXml());
}
Also used : Message(com.twilio.twiml.messaging.Message) Media(com.twilio.twiml.messaging.Media) Body(com.twilio.twiml.messaging.Body) MessagingResponse(com.twilio.twiml.MessagingResponse) TwiMLException(com.twilio.twiml.TwiMLException)

Example 2 with TwiMLException

use of com.twilio.twiml.TwiMLException 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 3 with TwiMLException

use of com.twilio.twiml.TwiMLException 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 4 with TwiMLException

use of com.twilio.twiml.TwiMLException 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 5 with TwiMLException

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

the class ReplyByName 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 fromNumber = request.getParameter("From");
    String name = fromNumber != null && people.get(fromNumber) != null ? people.get(fromNumber) : "Monkey";
    String message = String.format("%s, thanks for the message!", name);
    try {
        MessagingResponse twiml = new MessagingResponse.Builder().message(message).build();
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
    response.setContentType("application/xml");
    response.getWriter().print(twiml.toXml());
}
Also used : MessagingResponse(com.twilio.twiml.MessagingResponse) TwiMLException(com.twilio.twiml.TwiMLException)

Aggregations

TwiMLException (com.twilio.twiml.TwiMLException)10 VoiceResponse (com.twilio.twiml.VoiceResponse)6 MessagingResponse (com.twilio.twiml.MessagingResponse)4 Body (com.twilio.twiml.messaging.Body)3 Message (com.twilio.twiml.messaging.Message)3 Play (com.twilio.twiml.voice.Play)3 Say (com.twilio.twiml.voice.Say)3 TwiML (com.twilio.twiml.TwiML)1 Media (com.twilio.twiml.messaging.Media)1 Dial (com.twilio.twiml.voice.Dial)1 Queue (com.twilio.twiml.voice.Queue)1 Sms (com.twilio.twiml.voice.Sms)1 HttpSession (javax.servlet.http.HttpSession)1