Code Monkey home page Code Monkey logo

twilio-java's Introduction

Installing

TwilioJava is now using Maven. At present the jars are available from a public maven repository.

Use the following dependency in your project:

   <dependency>
      <groupId>com.twilio.sdk</groupId>
      <artifactId>twilio-java-sdk</artifactId>
      <version>3.3.10</version>
      <scope>compile</scope>
   </dependency>

If you want to compile it yourself, here's how:

$ git clone [email protected]:twilio/twilio-java
$ cd twilio-java 
$ mvn install       # Requires maven, download from http://maven.apache.org/download.html

This will also build the javadoc in twilio-java/target/apidocs. You can open the index.html located there to view it locally.

The pre-built jars are available at:

You can view the javadocs for this project at: http://twilio.github.com/twilio-java

Examples

Here are some examples (also found in TwilioRestExamples.java )

    // Create a rest client
    TwilioRestClient client = new TwilioRestClient(ACCOUNT_SID, AUTH_TOKEN);

    // Get the main account (The one we used to authenticate the client)
    Account mainAccount = client.getAccount();

    // Get all accounts including sub accounts
    AccountList accountList = client.getAccounts();

    // All lists implement an iterable interface, you can use the foreach
    // syntax on them
    for (Account a : accountList) {
        System.out.println(a.getFriendlyName());
    }

    // You can also iterate manually...
    Iterator<Account> itr = accountList.iterator();
    while (itr.hasNext()) {
        Account a = itr.next();
        System.out.println(a.getFriendlyName());
    }

    // You can also get just the first page of data
    accountList = client.getAccounts();
    List<Account> accounts = accountList.getPageData();

    // Make a call
    CallFactory callFactory = mainAccount.getCallFactory();
    Map<String, String> callParams = new HashMap<String, String>();
    callParams.put("To", "5105551212"); // Replace with a valid phone number
    callParams.put("From", "(510) 555-1212"); // Replace with a valid phone number in your account
    callParams.put("Url", "http://demo.twilio.com/welcome/voice/");
    Call call = callFactory.create(callParams);
    System.out.println(call.getSid());

    // Send an sms
    SmsFactory smsFactory = mainAccount.getSmsFactory();
    Map<String, String> smsParams = new HashMap<String, String>();
    smsParams.put("To", "5105551212"); // Replace with a valid phone number
    smsParams.put("From", "(510) 555-1212"); // Replace with a valid phone number in your account
    smsParams.put("Body", "This is a test message!");
    smsFactory.create(smsParams);

    // Search for available phone numbers & then buy a random phone number
    AvailablePhoneNumberList phoneNumbers = mainAccount.getAvailablePhoneNumbers();
    List<AvailablePhoneNumber> list = phoneNumbers.getPageData();

    // Buy the first number returned
    Map<String, String> params = new HashMap<String, String>();
    params.put("PhoneNumber", list.get(0).getPhoneNumber());
    params.put("VoiceUrl", "http://demo.twilio.com/welcome/voice/");
    mainAccount.getIncomingPhoneNumberFactory().create(params);

    // View a conference using its sid
    Conference c = mainAccount.getConference("CA12345...");
    ParticipantList participants = c.getParticipants();

    for (Participant p : participants) {
        // Randomly mute or kick each participant
        if (Math.random() > 0.5) {
            p.mute();
        } else {
            p.kick();
        }
    }

    // Make a raw HTTP request to the api... note, this is deprecated style
    TwilioRestResponse resp = client.request("/2010-04-01/Accounts", "GET", null);
    if (!resp.isError()) {
        System.out.println(resp.getResponseText());
    }

twilio-java's People

Contributors

christerf avatar eanderle avatar frankstratton avatar beans0063 avatar andrewmbenton avatar kyleconroy avatar csaken avatar

Watchers

James Cloos avatar Chris Foster avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.