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>4.4.1</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.
Pre-built jars are available here. Select the directory for the latest version and download one of these jar files:
- twilio-java-sdk-4.x.x-with-dependencies.jar
- twilio-java-sdk-4.x.x.jar -- use this if you have issues with conflicting jars in your project. You'll need to include versions of the dependencies yourself. See the pom.xml file in twilio-java for the list of libraries.
You can view the javadocs for this project at: http://twilio.github.io/twilio-java
Here are some examples (also found in TwilioRestExamples.java )
packagecom.twilio.sdk.examples; importjava.util.HashMap; importjava.util.Iterator; importjava.util.List; importjava.util.ArrayList; importjava.util.Map; importcom.twilio.sdk.TwilioRestClient; importcom.twilio.sdk.TwilioRestException; importcom.twilio.sdk.TwilioRestResponse; importcom.twilio.sdk.resource.factory.CallFactory; importcom.twilio.sdk.resource.factory.SmsFactory; importcom.twilio.sdk.resource.instance.Account; importcom.twilio.sdk.resource.instance.AvailablePhoneNumber; importcom.twilio.sdk.resource.instance.Call; importcom.twilio.sdk.resource.instance.Conference; importcom.twilio.sdk.resource.instance.Participant; importcom.twilio.sdk.resource.list.AccountList; importcom.twilio.sdk.resource.list.AvailablePhoneNumberList; importcom.twilio.sdk.resource.list.ParticipantList; importcom.twilio.sdk.verbs.TwiMLException; importorg.apache.http.NameValuePair; importorg.apache.http.message.BasicNameValuePair; publicclassExample{publicstaticfinalStringACCOUNT_SID = "AC....."; publicstaticfinalStringAUTH_TOKEN = "......."; publicstaticvoidmain(finalString[] args) throwsTwilioRestException, TwiMLException{// Create a rest clientfinalTwilioRestClientclient = newTwilioRestClient(ACCOUNT_SID, AUTH_TOKEN); // Get the main account (The one we used to authenticate the client)finalAccountmainAccount = client.getAccount(); // Get all accounts including sub accountsAccountListaccountList = client.getAccounts(); // All lists implement an iterable interface, you can use the foreach// syntax on themfor (finalAccounta : accountList){System.out.println(a.getFriendlyName())} // You can also iterate manually...finalIterator<Account> itr = accountList.iterator(); while (itr.hasNext()){finalAccounta = itr.next(); System.out.println(a.getFriendlyName())} // You can also get just the first page of dataaccountList = client.getAccounts(); finalList<Account> accounts = accountList.getPageData(); // Make a callfinalCallFactorycallFactory = mainAccount.getCallFactory(); finalMap<String, String> callParams = newHashMap<String, String>(); callParams.put("To", "5105551212"); // Replace with a valid phone numbercallParams.put("From", "(510) 555-1212"); // Replace with a valid phone number in your accountcallParams.put("Url", "http://demo.twilio.com/welcome/voice/"); finalCallcall = callFactory.create(callParams); System.out.println(call.getSid()); // Send an SMS (Requires version 3.4+)finalSmsFactorymessageFactory = mainAccount.getSmsFactory(); finalList<NameValuePair> messageParams = newArrayList<NameValuePair>(); messageParams.add(newBasicNameValuePair("To", "5105551212")); // Replace with a valid phone numbermessageParams.add(newBasicNameValuePair("From", "(510) 555-1212")); // Replace with a valid phone number in your accountmessageParams.add(newBasicNameValuePair("Body", "This is a test message!")); messageFactory.create(messageParams); // Search for available phone numbers & then buy a random phone numberfinalAvailablePhoneNumberListphoneNumbers = mainAccount.getAvailablePhoneNumbers(); finalList<AvailablePhoneNumber> list = phoneNumbers.getPageData(); // Buy the first number returnedfinalMap<String, String> params = newHashMap<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 sidfinalConferencec = mainAccount.getConference("CA12345..."); finalParticipantListparticipants = c.getParticipants(); for (finalParticipantp : participants){// Randomly mute or kick each participantif (Math.random() > 0.5){p.mute()} else{p.kick()} } // Make a raw HTTP request to the api... note, this is deprecated stylefinalTwilioRestResponseresp = client.request("/2010-04-01/Accounts", "GET", null); if (!resp.isError()){System.out.println(resp.getResponseText())} } }If you need help installing or using the library, please contact Twilio Support at [email protected] first. Twilio's Support staff are well-versed in all of the Twilio Helper Libraries, and usually reply within 24 hours.
If you've instead found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo!
