Search in sources :

Example 1 with MessagingResponse

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

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

Example 3 with MessagingResponse

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

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

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

the class SmsApp method main.

public static void main(String[] args) {
    get("/", (req, res) -> "Hello Web");
    post("/sms", (req, res) -> {
        res.type("application/xml");
        Body body = new Body.Builder("The Robots are coming! Head for the hills!").build();
        Message sms = new Message.Builder().body(body).build();
        MessagingResponse twiml = new MessagingResponse.Builder().message(sms).build();
        return twiml.toXml();
    });
}
Also used : Message(com.twilio.twiml.messaging.Message) Body(com.twilio.twiml.messaging.Body) MessagingResponse(com.twilio.twiml.MessagingResponse)

Aggregations

MessagingResponse (com.twilio.twiml.MessagingResponse)5 TwiMLException (com.twilio.twiml.TwiMLException)4 Body (com.twilio.twiml.messaging.Body)4 Message (com.twilio.twiml.messaging.Message)4 Media (com.twilio.twiml.messaging.Media)1 HttpSession (javax.servlet.http.HttpSession)1