当前位置: 首页 > article >正文

集成Twilio发送短信

一、前言

Twilio 是一个专注通讯服务的开放PaaS平台、是一个提供技术能力的网站,也是美国较为知名的云计算通讯服务类的初创企业。

Twilio 通过将复杂的底层通信功能打包成API并对外开放,让web、桌面及移动app可以方便的嵌入短信、语音及VoIP功能,从而实现云通信的功能。

二、Twilio SDK简介

参考链接

名称链接
git地址GitHub - twilio/twilio-java: A Java library for communicating with the Twilio REST API and generating TwiML.
api文档Docs: API Reference, Tutorials, and Integration | Twilio
账号注册流程 http://uuxn.com/twilio-toll-free-sms

发送消息,使用Twilio发送需要一个form Phone 和 to Phone,并且这是一个post请求

/2010-04-01/Accounts/{AccountSid}/Messages

这个消息的状态如下

  • queued
  • failed
  • sent
  • delivered
  • undelivered

参数实例

{
    "SmsSid": "AC16203ccc939a742e6b17607496bd6286",
    "SmsStatus": "delivered",
    "MessageStatus": "delivered",
    "To": "+15558675310",
    "MessageSid": "SMXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "AccountSid": "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "From": "+15017122661",
    "ApiVersion": "2010-04-01"
}

maven安装

  <dependency>
          <groupId>com.twilio.sdk</groupId>
          <artifactId>twilio</artifactId>
          <version>9.X.X</version>
          <scope>compile</scope>
  </dependency>

gradle安装

implementation "com.twilio.sdk:twilio:9.X.X"

源码编译

git clone git@github.com:twilio/twilio-java
cd twilio-java
mvn package -Dmaven.test.skip=true    # Requires maven, download from https://maven.apache.org/download.html

三、使用流程

1.账号注册

2.登录官网获取参数

  • accountSid
  • authToken
  • fromPhoneNumber

3.添加依赖

  <dependency>
            <groupId>com.twilio.sdk</groupId>
            <artifactId>twilio</artifactId>
            <version>7.17.0</version>
  </dependency>

4.编写测试类

package com.nonagon.mellaserver.utils;

import com.twilio.Twilio;
import com.twilio.base.ResourceSet;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.rest.api.v2010.account.Message.Status;
import com.twilio.type.PhoneNumber;
import java.util.concurrent.CompletableFuture;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.concurrent.FutureCallback;

/**
 * @ClassName:
 * @author: flitsneak nonagon
 * @date: 2021/1/25
 */
@Slf4j
public class TwilioSMSUtil {
  private static String ACCOUNT_SID =
      "AC16203ccc939a742e6b17607496bd6286";
  private static String AUTH_TOKEN =
      "139457ee14404d80d8822ea782f2dda7";

  static {
    Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
  }


  public static void SmsSender(String phoneNumber, String code) {
    Message message;
    if (phoneNumber.substring(0, 3).equals("+86")) {
      message = Message
          .creator(new PhoneNumber(phoneNumber), // to
              new PhoneNumber("+12019776411"), // from
            "your Code is "+code) //你好!我们很高兴你开始使用mella!你的验证码是"+code+",可以用它来激活你的账户
          .create();
    } else {
      message = Message
          .creator(new PhoneNumber(phoneNumber), // to
              new PhoneNumber("+12019776411"), // from
              "Hello! We're excited for you to start using your Mella! your Mella Verification Code is " + code+", you can use it to activate your account.")
          .create();
    }
    log.info("======短信id========" + message.getSid());
    log.info("====短信发送状态=====" + message.getStatus());
    if (message.getStatus() == Status.FAILED) {
      log.info("======短信发送失败====="+message.getSid());
    }

    //      CompletableFuture<ResourceSet<Message>> future = Message.reader().readAsync();


  }

  public static void main(String[] args) {
    //获取所有短信的状态
    ResourceSet<Message> messages = Message.reader().read();
    for (Message nn : messages) {
      log.info(nn.getSid() + " :" + nn.getStatus());
    }
  }


}



http://www.kler.cn/news/362036.html

相关文章:

  • C#读取和写入txt文档(在unity中示例)
  • js---三元表达式详解
  • 学习TOGAF架构
  • html+css+js实现Notification 通知
  • AI未来会拥有人类的情感吗,情感计算领域进展如何?
  • VSCODE 导入cubeide工程
  • 【AIGC半月报】AIGC大模型启元:2024.10(下)
  • React面试题目(从基本到高级)
  • 【用GPT记录的笔记】linux多线程下载
  • 当 AI 遇上爬虫:让数据提取变得前所未有地简单!
  • 常见的前端开发面试题及其答案
  • HarmonyOS的DevEcoStudio安装以及初步认识
  • 【Vue】Vue3(1)
  • 如何在springboot3微项目里面用idea批量创建单元测试逻辑
  • Type Approval (认证)
  • 超简洁的B端系统,还是看国外的设计.
  • 基于STM32的粮库检测系统设计
  • Shell脚本:模块引用
  • 基于SpringBoot健康生活助手微信小程序【附源码】
  • Maven3.9.9环境安装配置
  • Vxe vue vxe-table 分享实现打印表格以及同时打印两张表
  • uniapp结合uview-ui创建项目
  • 基于Prompt工程优化bot
  • 养老院网站毕设计算机毕业设计基于SpringBootSSM框架
  • C#使用XML文件的详解及示例
  • python 更换pip源