Twilio发送短信

简介

Twilio 为未来的商业沟通提供强大支持,并使开发人员可以将语音、VoIP 和消息传送嵌入到应用程序中。 它们对基于云的全球环境中所需的全部基础结构进行虚拟化,并经过 Twilio 通讯 API 平台将其公开。 可轻松构建和扩展应用程序。 享受现用现付订价所带来的灵活性,并从云可靠性中受益。java

利用 Twilio 语音,应用程序能够发起和接收电话呼叫。 Twilio SMS 使应用程序可以发送和接收文本消息。 利用 Twilio 客户端,能够从任何手机、平板电脑或浏览器发起 VoIP 呼叫并支持 WebRTC。git

帐号注册

关于帐号注册能够参考这篇文章 : http://uuxn.com/twilio-toll-free-smsgithub

准备

使用前须要登录官网获取三个参数浏览器

  • accountSid
  • authToken
  • fromPhoneNumber

使用

  1. 建立maven工程,添加依赖。微信

    <dependency>
     	<groupId>com.twilio.sdk</groupId>
     	<artifactId>twilio</artifactId>
     	<version>7.17.0</version>
     </dependency>
  2. 编写代码maven

    @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](https://my.oschina.net/azibug)
     	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](https://my.oschina.net/azibug)
         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. 结果ui

    手机能够正常收到短信,使用的时候发送频率控制在1s一条this

参考

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

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

如但愿了解更多,请关注微信公众号

相关文章
相关标签/搜索