how to call someone from java code

Some code about how to use TwilioCaller inside java classes.

Add a accounts.properties in the classpath of the application - WEB-INF/classes path:

twilio.accounts=default,primo
# default account
account.default.twilioSid=1111
account.default.twilioToken=222
account.default.twilioNumber=333
account.default.applicationSid=444
#primo account
account.primo.twilioSid=5555
account.primo.twilioToken=666
account.primo.twilioNumber=777
account.primo.applicationSid=888

And inside the class where you want call some number:

  • Inject org.twiliofaces.cdi.doers.Caller

@Inject
@TwilioAccount(accountName = "default")
Caller caller;



public void directCall(String to, String endpointUrl) {

	try {
  		String callId = caller
  		.endpoint(endpointUrl).to(to).call();
		System.out.println("call id: " + callId);
	} catch (TwilioRestException e) {
		e.printStackTrace();
	}


}
  • you can create your runtime org.twiliofaces.cdi.doers.simple.SimpleCaller using:

public void call(String accountSid, String authToken, String from, String to, String url) {

	try {
  		String callId =  new SimpleCaller().accountSid(accountSid).authToken(authToken)
  				.endpoint("http://demo.twilio.com/docs/voice.xml")
               			.from(from).to(to)
               			.call();
		System.out.println("call id: " + callId);
	} catch (TwilioRestException e) {
		e.printStackTrace();
	}


}

and the twiml page should be like:

<?xml version="1.0" encoding="UTF-8"?>
<f:view xmlns="http://www.w3c.org/1999/xhtml"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:tf="http://twiliofaces.org/twiliofaces">
	<tf:response>
		<tf:say voice="woman" language="it">Hello!</tf:say>
	</tf:response>
</f:view>
comments powered by Disqus

Contact

Do you want collaborate with us?
Do you like this project?
Questions?
Please send me an email!

fiorenzo.pizza@gmail.com

Github code

Fork the twiliofaces repository on GitHub
and clone it to your local PC.

Get Started

Copy the Twiliofaces JAR file into your /WEB-INF/lib directory, or include all required Maven dependencies in your pom.xml!