Brief introduction
Twilio provides strong support for the future of business communication, and developers to embed voice, VoIP and messaging into an application. They virtualize all infrastructure required for global cloud-based environment, and through its Twilio communications API platform will open. You can easily build and extend applications. Enjoy now with pay-pricing flexibility brought about, and benefit from the reliability of the cloud.
Use Twilio Voice, an application can initiate and receive phone calls. Twilio SMS enables applications to send and receive text messages. Use Twilio client can initiate VoIP from any phone, tablet or browser calls and supports WebRTC.
Register an account
About account registration can refer to this article: http://uuxn.com/twilio-toll-free-sms
ready
Visit the official website need to get three parameters before use
- accountSid
- authToken
- fromPhoneNumber
use
Create a maven project, add a dependency.
<dependency> <groupId>com.twilio.sdk</groupId> <artifactId>twilio</artifactId> <version>7.17.0</version> </dependency>
Write code
@RunWith(SpringRunner.class) @SpringBootTest public class DemoApplicationTests { private static final String accountSid = "ACxxxx"; // Your Account SID from www.twilio.com/user/account private static final String authToken = "xxxx"; // Your Auth Token from www.twilio.com/user/account @Test public void contextLoads() { Twilio.init(accountSid, authToken); Message message = Message.creator( new PhoneNumber("+xxx"), // To number ,Phone number with area code new PhoneNumber("+xxx"), // From number " A book is the same today as it always was and it will never change." // SMS body ).create(); if (! StringUtils.isEmpty(message.getSid())){ System.out.println(message.getSid()); } } @Test public void sendCall() throws URISyntaxException { Twilio.init(accountSid, authToken); Call call = Call.creator( new PhoneNumber("+xxxx"), // To number new PhoneNumber("+xxxx"), // From number // Read TwiML at this URL when a call connects (hold music) new URI("http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") ).create(); if (! StringUtils.isEmpty(call.getSid())){ System.out.println(call.getSid()); } } }
result
When the normal phone can receive text messages, using the transmission frequency control in a 1s
reference
https://www.twilio.com/docs/sms/quickstart/java
https://github.com/twilio/twilio-java