Commit 3a9d7f09 authored by 耿迪迪's avatar 耿迪迪

阿里语音通知 gengdidi

parent bfe77e34
...@@ -115,6 +115,18 @@ ...@@ -115,6 +115,18 @@
<version>1.8.6</version> <version>1.8.6</version>
</dependency> </dependency>
<!-- 阿里语音 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>tea-openapi</artifactId>
<version>0.2.2</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dyvmsapi20170525</artifactId>
<version>2.1.4</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package com.zehong.communication.controller;
import com.zehong.communication.common.result.Result;
import com.zehong.communication.common.result.ResultEnum;
import com.zehong.communication.model.SendAliVoiceNotify;
import com.zehong.communication.service.AliVoiceNotifyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.validation.Valid;
/**
* @author gengd
* 阿里语音通知
*/
@RestController
@RequestMapping("/aliVoiceNotify")
public class AliVoiceNotifyController {
private static final Logger logger = LoggerFactory.getLogger(AliVoiceNotifyController.class);
@Autowired
private AliVoiceNotifyService aliVoiceNotifyService;
/**
* 发送阿里语音
* @param sendAliVoiceNotify 阿里语音
* @param result 返回结果
* @return Result
*/
@RequestMapping(value = "/sendAliVoiceNotify",method = RequestMethod.POST)
public Result sendAliVoiceNotify(@Valid @RequestBody SendAliVoiceNotify sendAliVoiceNotify, BindingResult result){
try {
if(result.hasErrors()){
return new Result(ResultEnum.VALIDATA_ERROR.getCode(),result.getFieldError().getDefaultMessage());
}
return aliVoiceNotifyService.sendAliVoiceNotify(sendAliVoiceNotify);
} catch (Exception e) {
logger.error("阿里语音发送失败,phoneNumbers:%",sendAliVoiceNotify.getCalledNumber(),e);
return new Result(ResultEnum.SYSTEM_EXCEPTION);
}
}
}
package com.zehong.communication.model;
import lombok.Data;
import org.hibernate.validator.constraints.NotBlank;
import org.hibernate.validator.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
@Data
public class SendAliVoiceNotify extends BaseRequestModel {
/**
* 若您使用的文本转语音模板为公共模式外呼,则该参数值不填。
* */
private String calledShowNumber;
/**
* 接收语音通知的手机号码
* 一次请求仅支持一个被叫号,使用流程,请参见国内语音通知使用流程或国际/港澳台语音验证码使用流程。
*语音验证码流控频率规则:同一个被叫,1次/分钟、5次/小时、20次/24小时。
*/
@NotEmpty(message = "接收语音通知的手机号码不能为空")
@Pattern(regexp = "^1[0-9]{10}$", message = "接收语音通知的手机号不合法")
private String calledNumber;
/**
* 已通过审核的语音验证码模板ID
*/
@NotBlank(message = "语音验证码模板id不能为空")
private String ttsCode;
/**
* 模板中的变量参数,JSON格式
* {"AckNum":"123456"}
*/
@NotBlank(message = "语音模板不能为空")
private String ttsParam;
/**
* 非必填
* 一通电话内语音通知内容的播放次数。取值范围:1~3,默认取值3
*/
private int playTimes;
/**
* 非必填
* 语音通知的播放音量。取值范围:0~100,默认取值100。
*/
private int volume = -1;
/**
* 非必填
* 语速控制。取值范围为:-500~500。
*/
private int speed = 555;
/**
* 非必填
* 发起请求时预留给调用方的自定义ID,最终会通过在回执消息中将此ID带回给调用方。
*字符串类型,长度为1~15个字节。
*/
private String outId;
}
package com.zehong.communication.service;
import com.zehong.communication.common.result.Result;
import com.zehong.communication.model.SendAliVoiceNotify;
public interface AliVoiceNotifyService {
/**
* 发送阿里语音
* @param sendAliVoiceNotify 阿里语音
* @return sendAliVoiceNotify
*/
Result sendAliVoiceNotify(SendAliVoiceNotify sendAliVoiceNotify) throws Exception;
}
package com.zehong.communication.service.impl;
import com.aliyun.dyvmsapi20170525.Client;
import com.aliyun.dyvmsapi20170525.models.SingleCallByTtsRequest;
import com.aliyun.dyvmsapi20170525.models.SingleCallByTtsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.zehong.communication.common.result.Result;
import com.zehong.communication.common.result.ResultEnum;
import com.zehong.communication.model.SendAliVoiceNotify;
import com.zehong.communication.service.AliVoiceNotifyService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class AliVoiceNotifyServiceImpl implements AliVoiceNotifyService{
@Value("${accessKeyId}")
private String accessKeyId;
@Value("${accessKeySecret}")
private String accessKeySecret;
/**
* 发送阿里语音
* @param sendAliVoiceNotify 阿里语音
* @return sendAliVoiceNotify
*/
@Override
public Result sendAliVoiceNotify(SendAliVoiceNotify sendAliVoiceNotify) throws Exception {
com.aliyun.dyvmsapi20170525.Client client = createClient();
SingleCallByTtsRequest singleCallByTtsRequest = new SingleCallByTtsRequest()
.setCalledNumber(sendAliVoiceNotify.getCalledNumber())
.setTtsCode(sendAliVoiceNotify.getTtsCode())
.setTtsParam(sendAliVoiceNotify.getTtsParam());
if(StringUtils.isNotBlank(sendAliVoiceNotify.getCalledShowNumber())){
singleCallByTtsRequest.setCalledShowNumber(sendAliVoiceNotify.getCalledShowNumber());
}
if(sendAliVoiceNotify.getPlayTimes()>0){
singleCallByTtsRequest.setPlayTimes(sendAliVoiceNotify.getPlayTimes());
}
if(sendAliVoiceNotify.getVolume() != -1){
singleCallByTtsRequest.setVolume(sendAliVoiceNotify.getVolume());
}
if(sendAliVoiceNotify.getSpeed() != 555){
singleCallByTtsRequest.setSpeed(sendAliVoiceNotify.getSpeed());
}
if(StringUtils.isNotBlank(sendAliVoiceNotify.getOutId())){
singleCallByTtsRequest.setOutId(sendAliVoiceNotify.getOutId());
}
SingleCallByTtsResponse response = client.singleCallByTts(singleCallByTtsRequest);
if(response.getBody().getCode() != null && "OK".equals(response.getBody().getCode())){
//请求成功
return new Result(ResultEnum.SUCCESS.getCode(),response.getBody().getMessage());
}
return new Result(ResultEnum.FAIL.getCode(),response.getBody().getMessage());
}
/**
* 使用AK&SK初始化账号Client
* @return Client
* @throws Exception 异常
*/
private Client createClient() throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dyvmsapi.aliyuncs.com";
return new com.aliyun.dyvmsapi20170525.Client(config);
}
}
...@@ -40,7 +40,7 @@ accessKeyId: LTAI5tC2wUxESqPG5FpmP2DX ...@@ -40,7 +40,7 @@ accessKeyId: LTAI5tC2wUxESqPG5FpmP2DX
accessKeySecret: THvYOSu8EKvz3Hk7s8nDTQ11Y8kPH7 accessKeySecret: THvYOSu8EKvz3Hk7s8nDTQ11Y8kPH7
#ip白名单 #ip白名单
iPWhite: 36.148.1.58,121.29.172.242,111.61.77.35,27.128.170.124,36.148.1.58,101.16.95.194 iPWhite: 36.148.1.58,121.29.172.242,111.61.77.35,27.128.170.124,36.148.1.58,101.16.95.194,27.128.113.170,61.55.220.186
#秘钥 #秘钥
secretKey: f225e66813e6d6a663daf919c1935dcb secretKey: f225e66813e6d6a663daf919c1935dcb
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment