See: Description
Package | Description |
---|---|
com.twilio.sdk | |
com.twilio.sdk.verbs | |
com.twilio4j.twiml | |
com.twilio4j.twism | |
com.twilio4j.twism.eg | |
com.twilio4j.util |
twilio4j is aTwilio state machine library which allows you to create call flows easily in java. You can install one servlet and implement your code that reacts to Twilio callback hooks with TwiML, etc.
Note: In April 2012, I added the com.twilio.sdk sources for the Twilio java client library (from Twilio), which will allow you to have a Twilio Client on Google AppEngine. Read more about it here.
TwilioStateMachineServlet is the class you need to extend in order to create your own call flow state machine. First you must create an enumerated type that represents all the states of your machine. This enum is used to "type" the TwilioStateMachineServlet when you extend it.
You don't override any member functions in your subclass. Instead, you just supply a constructor, where you repeatedly call handler() to populate your state machine. And for each hander(), you call respondsWith() and supply straight TwiML statements, or a code block if you need to supply java code plus TwiML.
Example:
import com.twilio4j.twiml.TwiML;
import com.twilio4j.twism.TwilioParameters;
import com.twilio4j.twism.TwilioStateMachineServlet;
import static com.twilio4j.twism.eg.NumberGameState.*;
public class NumberGameStateMachineServlet extends TwilioStateMachineServlet<NumberGameState> {
private static final long serialVersionUID = 1L;
public NumberGameStateMachineServlet() {
handler(PICK_NUMBER).respondsWith(
gather(
say("pick a number between zero and nine.")
)
.action(CHECK_NUMBER)
.numDigits(1)
);
handler(CHECK_NUMBER).respondsWith(new TwilioHandler() {
public TwiML getTwiML(TwilioParameters params) {
char digit = params.Gather().getDigits().charAt(0);
if ( digit == '5' ) {
return say("You win! Goodbye.");
} else if ( digit < '5' ) {
return gather(
say("Pick again, higher.")
)
.action(CHECK_NUMBER)
.numDigits(1);
} else {
return gather(
say("Pick again, lower.")
)
.action(CHECK_NUMBER)
.numDigits(1);
}
}
});
}
public NumberGameState getInitialState() {
return PICK_NUMBER;
}
public NumberGameState lookupState(String pathInfo) {
return NumberGameState.valueOf(pathInfo);
}
public String getFortyCharacterSecret() {
return "3440e0fa2eae0a28e5dc58d76793eb151c19acf7";
}
}
Copyright © 2015 Gentomi, Inc. All Rights Reserved.