package cc.mrbird.febs.common.utils; import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.IAcsClient; import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsRequest; import com.aliyuncs.dyvmsapi.model.v20170525.SingleCallByTtsResponse; import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ServerException; import com.aliyuncs.profile.DefaultProfile; import com.aliyuncs.profile.IClientProfile; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.google.gson.Gson; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import java.util.Arrays; import java.util.List; import java.util.Map; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.function.Supplier; /** * @author :qying * @date :Created in 2021/5/19 11:02 * @description:阿里语音通知工具类 * @modified By:` * @version: 1.0 */ @Slf4j public class AliyunVoiceUtils { public static void main(String[] args) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "AccessKeyID", "AccessKeySecret"); IAcsClient client = new DefaultAcsClient(profile); SingleCallByTtsRequest request = new SingleCallByTtsRequest(); request.setRegionId("cn-hangzhou"); request.setEndpoint("dyvmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setCalledNumber("18888888888"); // 呼叫手机号 request.setTtsCode("TTS_21724qwqw2"); // 模板ID request.setTtsParam("{ \"device\": \"烟雾传感器\" }"); // 模板参数 json字符串 request.setPlayTimes(2); request.setSpeed(0); try { SingleCallByTtsResponse response = client.getAcsResponse(request); System.out.println(new Gson().toJson(response)); } catch (ServerException e) { e.printStackTrace(); } catch (ClientException e) { System.out.println("ErrCode:" + e.getErrCode()); System.out.println("ErrMsg:" + e.getErrMsg()); System.out.println("RequestId:" + e.getRequestId()); } } /** * @ClassName 语音呼叫 * @Description * managerPhone 手机号 device :设备名 * @author qying * @date 2021/5/19 \ */ public static void singleCallByTts(Map<String, Object> map, String managerPhone, String device,String TtsCode) throws ClientException { List<String> list = Arrays.asList(managerPhone.split(StringPool.COMMA)); if(list.size()>0){ for (String phone:list) { DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", String.valueOf(map.get("AccessKeyID")), String.valueOf(map.get("AccessKeySecret"))); IAcsClient client = new DefaultAcsClient(profile); SingleCallByTtsRequest request = new SingleCallByTtsRequest(); request.setRegionId("cn-hangzhou"); request.setEndpoint("dyvmsapi.aliyuncs.com"); request.setVersion("2017-05-25"); request.setCalledNumber(phone); request.setTtsCode(TtsCode); request.setTtsParam("{ \"device\": \""+device+"\" }"); request.setPlayTimes(2); request.setSpeed(0); SingleCallByTtsResponse singleCallByTtsResponse = client.getAcsResponse(request); if (singleCallByTtsResponse.getCode() != null && singleCallByTtsResponse.getCode().equals("OK")) { //请求成功 log.info("请求成功"); } else { log.info("语音呼叫失败,code{}", singleCallByTtsResponse.getCode()); if (singleCallByTtsResponse.getCode().equals("isv.BUSINESS_LIMIT_CONTROL")) { log.info("流控限制,3分钟后重试"); ExecutorService executor = Executors.newFixedThreadPool(2); //jdk1.8之前的实现方式 CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() { @Override public String get() { int state = 0; try { ThreadSleepMSG(map, phone,device,TtsCode); } catch (Exception e) { e.printStackTrace(); } return "语音重新呼叫成功,数量:"+state; } }, executor); } } } } } private static void ThreadSleepMSG(Map<String, Object> map, String phone, String tailNumber,String TtsCode){ try { Thread.sleep(3 * 60 * 1000L); singleCallByTts(map, phone, tailNumber,TtsCode); }catch (Exception e){ e.printStackTrace(); } } }
阿里云语音电话工具类
最新推荐文章于 2024-06-14 11:41:44 发布