Search in sources :

Example 1 with TwiML

use of com.twilio.twiml.TwiML 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 2 with TwiML

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

the class Example method main.

/**
 * Example Twilio usage.
 *
 * @param args command line args
 * @throws TwiMLException if unable to generate TwiML
 */
public static void main(String[] args) throws TwiMLException, URISyntaxException {
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
    Iterable<Record> usage = Record.reader().read();
    for (Record record : usage) {
        System.out.println(record);
    }
    // Get a number
    IncomingPhoneNumber number = buyNumber();
    System.out.println(number.getPhoneNumber());
    // Send a text message
    Message message = Message.creator(ACCOUNT_SID, PHONE_NUMBER, number.getPhoneNumber(), "Hello world!").create();
    System.out.println(message.getSid());
    System.out.println(message.getBody());
    // Make a phone call
    Call call = new CallCreator(ACCOUNT_SID, PHONE_NUMBER, number.getPhoneNumber(), URI.create("https://twilio.com")).create();
    System.out.println(call.getSid());
    // Print all the messages
    Iterable<Message> messages = Message.reader().read();
    for (Message m : messages) {
        System.out.println(m.getSid());
        System.out.println(m.getBody());
    }
    // Get some calls
    Iterable<Call> calls = Call.reader().pageSize(2).read();
    for (Call c : calls) {
        System.out.println(c.getSid());
    }
    Trunk trunk = new TrunkCreator().setFriendlyName("shiny trunk").setSecure(false).create();
    System.out.println(trunk);
    // TwiML
    TwiML twiml = new VoiceResponse.Builder().say(new Say.Builder("Hello World!").build()).play(new Play.Builder().url(new URI("https://api.twilio.com/cowbell.mp3")).loop(5).build()).build();
    System.out.println(twiml.toXml());
}
Also used : Call(com.twilio.rest.api.v2010.account.Call) Trunk(com.twilio.rest.trunking.v1.Trunk) VoiceResponse(com.twilio.twiml.VoiceResponse) Message(com.twilio.rest.api.v2010.account.Message) TrunkCreator(com.twilio.rest.trunking.v1.TrunkCreator) URI(java.net.URI) Play(com.twilio.twiml.voice.Play) TwiML(com.twilio.twiml.TwiML) Record(com.twilio.rest.api.v2010.account.usage.Record) IncomingPhoneNumber(com.twilio.rest.api.v2010.account.IncomingPhoneNumber) CallCreator(com.twilio.rest.api.v2010.account.CallCreator)

Aggregations

TwiML (com.twilio.twiml.TwiML)2 VoiceResponse (com.twilio.twiml.VoiceResponse)2 Call (com.twilio.rest.api.v2010.account.Call)1 CallCreator (com.twilio.rest.api.v2010.account.CallCreator)1 IncomingPhoneNumber (com.twilio.rest.api.v2010.account.IncomingPhoneNumber)1 Message (com.twilio.rest.api.v2010.account.Message)1 Record (com.twilio.rest.api.v2010.account.usage.Record)1 Trunk (com.twilio.rest.trunking.v1.Trunk)1 TrunkCreator (com.twilio.rest.trunking.v1.TrunkCreator)1 TwiMLException (com.twilio.twiml.TwiMLException)1 Play (com.twilio.twiml.voice.Play)1 URI (java.net.URI)1