Search in sources :

Example 6 with TwiMLException

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

Example 7 with TwiMLException

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

the class SmsHelloMonkey 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")).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) Body(com.twilio.twiml.messaging.Body) MessagingResponse(com.twilio.twiml.MessagingResponse) TwiMLException(com.twilio.twiml.TwiMLException)

Example 8 with TwiMLException

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

the class VoiceServlet method doPost.

public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Create a TwiML response and add our friendly message.
    TwiML twiml = new VoiceResponse.Builder().gather(new Gather.Builder().numDigits(1).say(new Say.Builder("For sales, press 1. For support, press 2.").build()).build()).redirect(new Redirect.Builder().url("/voice").build()).build();
    response.setContentType("application/xml");
    try {
        response.getWriter().print(twiml.toXml());
    } catch (TwiMLException e) {
        throw new RuntimeException(e);
    }
}
Also used : TwiML(com.twilio.twiml.TwiML) VoiceResponse(com.twilio.twiml.VoiceResponse) TwiMLException(com.twilio.twiml.TwiMLException)

Example 9 with TwiMLException

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

the class TrackingSmsConversations method service.

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    HttpSession session = request.getSession(true);
    Integer sessionCounter = (Integer) session.getAttribute("counter");
    Integer counter = sessionCounter != null ? sessionCounter : new Integer(0);
    /* Increment the counter by one, and store the count in the session. */
    int count = counter.intValue();
    count++;
    session.setAttribute("counter", new Integer(count));
    // 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");
    String toNumber = request.getParameter("To");
    String fromNumber = request.getParameter("From");
    String name = fromNumber != null && people.get(fromNumber) != null ? people.get(fromNumber) : "Monkey";
    String message = String.format("%s has messaged %s %d times.", name, toNumber, String.valueOf(count));
    try {
        // Create a TwiML response and add our friendly message.
        Message sms = new Message.Builder().body(new Body(message)).build();
        MessagingResponse twimlResponse = new MessagingResponse.Builder().message(sms).build();
    } catch (TwiMLException e) {
        e.printStackTrace();
    }
    response.setContentType("application/xml");
    response.getWriter().print(twimlResponse.toXml());
}
Also used : Message(com.twilio.twiml.messaging.Message) HttpSession(javax.servlet.http.HttpSession) Body(com.twilio.twiml.messaging.Body) MessagingResponse(com.twilio.twiml.MessagingResponse) TwiMLException(com.twilio.twiml.TwiMLException)

Example 10 with TwiMLException

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

the class TwilioServlet method service.

public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    // Create a TwiML response and add our friendly message.
    Say say = new Say.Builder("Hello. It's me.").build();
    Play play = new Play.Builder("https://deved-sample-assets-2691.twil.io/ahoyhoy.mp3").build();
    VoiceResponse twiml = new VoiceResponse.Builder().say(say).play(play).build();
    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)

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