簡介
Twilio 為將來的商業溝通提供強大支持,並使開發人員能夠將語音、VoIP 和消息傳送嵌入到應用程序中。 它們對基於雲的全球環境中所需的所有基礎結構進行虛擬化,並通過 Twilio 通信 API 平台將其公開。 可輕松構建和擴展應用程序。 享受現用現付定價所帶來的靈活性,並從雲可靠性中受益。
利用 Twilio 語音,應用程序可以發起和接收電話呼叫。 Twilio SMS 使應用程序能夠發送和接收文本消息。 利用 Twilio 客戶端,可以從任何手機、平板電腦或瀏覽器發起 VoIP 呼叫並支持 WebRTC。
賬號注冊
關於賬號注冊可以參考這篇文章 : http://uuxn.com/twilio-toll-free-sms
准備
使用前需要登陸官網獲取三個參數
- accountSid
- authToken
- fromPhoneNumber
使用
-
創建maven工程,添加依賴。
<dependency> <groupId>com.twilio.sdk</groupId> <artifactId>twilio</artifactId> <version>7.17.0</version> </dependency>
-
編寫代碼
@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()); } } }
-
結果
手機可以正常收到短信,使用的時候發送頻率控制在1s一條
參考
https://www.twilio.com/docs/sms/quickstart/java
https://github.com/twilio/twilio-java