Twilio發送短信


簡介

Twilio 為將來的商業溝通提供強大支持,並使開發人員能夠將語音、VoIP 和消息傳送嵌入到應用程序中。 它們對基於雲的全球環境中所需的所有基礎結構進行虛擬化,並通過 Twilio 通信 API 平台將其公開。 可輕松構建和擴展應用程序。 享受現用現付定價所帶來的靈活性,並從雲可靠性中受益。

利用 Twilio 語音,應用程序可以發起和接收電話呼叫。 Twilio SMS 使應用程序能夠發送和接收文本消息。 利用 Twilio 客戶端,可以從任何手機、平板電腦或瀏覽器發起 VoIP 呼叫並支持 WebRTC。

賬號注冊

關於賬號注冊可以參考這篇文章 : http://uuxn.com/twilio-toll-free-sms

准備

使用前需要登陸官網獲取三個參數

  • accountSid
  • authToken
  • fromPhoneNumber

使用

  1. 創建maven工程,添加依賴。

     <dependency>
     	<groupId>com.twilio.sdk</groupId>
     	<artifactId>twilio</artifactId>
     	<version>7.17.0</version>
     </dependency>
    
  2. 編寫代碼

     @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());
         
         	}
         
         }
     }
    
  3. 結果

    手機可以正常收到短信,使用的時候發送頻率控制在1s一條

參考

https://www.twilio.com/docs/sms/quickstart/java

https://github.com/twilio/twilio-java

**如希望了解更多,請關注微信公眾號**
![](https://img2018.cnblogs.com/blog/1821244/201909/1821244-20190930122546413-2090380039.jpg)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM