package com.ruoyi.common.utils; import com.ruoyi.common.utils.huawei.*; import java.util.*; import javax.net.ssl.HttpsURLConnection; public class VoiceNotifyMain { // 接口返回值 private static String status = ""; private static String resultcode = ""; private static String resultdesc = ""; // 语音通知接口返回值 private static String sessionId = ""; // 语音通知业务类实体 public static VoiceNotify callNotifyAPI = new VoiceNotify(); // 调用接口成功标识 private static final String success = "200"; public static void call(String phone) throws Exception { // TODO 程序前端要求发起语音通知呼叫,首先使用getplayInfo构造构造playInfoList参数,然后调用doCallNotify方法. // 以下代码仅供调试使用,实际开发时请删除 // 构造playInfoList参数 List> playInfoList = new ArrayList>(); // 使用音频文件作为第一段放音内容 playInfoList.add(callNotifyAPI.getplayInfo("@DD.wav")); // 使用v2.0版本接口的语音通知模板作为第二段放音内容 // String templateId = "test"; // List templateParas = new ArrayList(); // templateParas.add("3"); // templateParas.add("1栋保安亭"); // playInfoList.add(callNotifyAPI.getplayInfo(templateId, templateParas)); // 调用doCallNotify方法 VoiceNotifyMain.doCallNotify("+862022519218", "+86"+phone, playInfoList); if (status.indexOf(success) != -1) { System.out.println(status); System.out.println(resultcode + " " + resultdesc); System.out.println("The session id is: " + sessionId); } // TODO 需要接收状态和话单时,请参考"呼叫状态和话单通知API"接口实现状态通知和话单的接收和解析 // HostingVoiceEventDemoImpl } public static void call1(String phone) throws Exception { // TODO 程序前端要求发起语音通知呼叫,首先使用getplayInfo构造构造playInfoList参数,然后调用doCallNotify方法. // 以下代码仅供调试使用,实际开发时请删除 // 构造playInfoList参数 List> playInfoList = new ArrayList>(); // 使用音频文件作为第一段放音内容 playInfoList.add(callNotifyAPI.getplayInfo("BB@.wav")); // 使用v2.0版本接口的语音通知模板作为第二段放音内容 // String templateId = "test"; // List templateParas = new ArrayList(); // templateParas.add("3"); // templateParas.add("1栋保安亭"); // playInfoList.add(callNotifyAPI.getplayInfo(templateId, templateParas)); // 调用doCallNotify方法 VoiceNotifyMain.doCallNotify("+862022519218", "+86"+phone, playInfoList); if (status.indexOf(success) != -1) { System.out.println(status); System.out.println(resultcode + " " + resultdesc); System.out.println("The session id is: " + sessionId); } // TODO 需要接收状态和话单时,请参考"呼叫状态和话单通知API"接口实现状态通知和话单的接收和解析 // HostingVoiceEventDemoImpl } public static void call2(String phone) throws Exception { // TODO 程序前端要求发起语音通知呼叫,首先使用getplayInfo构造构造playInfoList参数,然后调用doCallNotify方法. // 以下代码仅供调试使用,实际开发时请删除 // 构造playInfoList参数 List> playInfoList = new ArrayList>(); // 使用音频文件作为第一段放音内容 playInfoList.add(callNotifyAPI.getplayInfo("CC@.wav")); // 使用v2.0版本接口的语音通知模板作为第二段放音内容 // String templateId = "test"; // List templateParas = new ArrayList(); // templateParas.add("3"); // templateParas.add("1栋保安亭"); // playInfoList.add(callNotifyAPI.getplayInfo(templateId, templateParas)); // 调用doCallNotify方法 VoiceNotifyMain.doCallNotify("+862022519218", "+86"+phone, playInfoList); if (status.indexOf(success) != -1) { System.out.println(status); System.out.println(resultcode + " " + resultdesc); System.out.println("The session id is: " + sessionId); } // TODO 需要接收状态和话单时,请参考"呼叫状态和话单通知API"接口实现状态通知和话单的接收和解析 // HostingVoiceEventDemoImpl } /* * 前端需要发起语音通知呼叫时,调用此方法 该示例只仅体现必选参数,可选参数根据接口文档和实际情况配置. */ public static void doCallNotify(String displayNbr, String calleeNbr, List> playInfoList) throws Exception { Boolean retry = false; // 调用语音通知接口,直至成功 do { status = callNotifyAPI.callNotifyAPI(displayNbr, calleeNbr, playInfoList); if (status.indexOf(success) != -1) { retry = false; // 调用成功,记录返回的信息. resultcode = callNotifyAPI.getResponsePara("resultcode"); resultdesc = callNotifyAPI.getResponsePara("resultdesc"); sessionId = callNotifyAPI.getResponsePara("sessionId"); } else { retry = true; // 调用失败,获取错误码和错误描述. resultcode = callNotifyAPI.getResponsePara("resultcode"); resultdesc = callNotifyAPI.getResponsePara("resultdesc"); // 处理错误 VoiceNotifyMain.processError(); } } while (retry); } // 当API的返回值不是200时,处理错误. private static void processError() throws InterruptedException { // TODO 根据错误码和错误码描述处理问题 // 以下代码仅供调试使用,实际开发时请删除 System.out.println(status); System.out.println(resultcode + " " + resultdesc); System.exit(-1); } } class VoiceNotify { // 语音通知API的调用地址 private String urlCallNotify; // 接口响应的消息体 private Map Responsebody; // Https实体 private HttpsUtil httpsUtil; public VoiceNotify() { // 商用地址 urlCallNotify = Constant.CALL_NOTIFY_COMERCIAL; Responsebody = new HashMap<>(); } @SuppressWarnings("unchecked") /* * 该示例只仅体现必选参数,可选参数根据接口文档和实际情况配置. 该示例不体现参数校验,请根据各参数的格式要求自行实现校验功能. * playInfoList为最大个数为5的放音内容参数列表,每个放音内容参数以Map格式存储, * 放音内容参数的构造方法请参考getplayInfo方法. */ public String callNotifyAPI(String displayNbr, String calleeNbr, List> playInfoList) throws Exception { httpsUtil = new HttpsUtil(); // 忽略证书信任问题 httpsUtil.trustAllHttpsCertificates(); HttpsURLConnection.setDefaultHostnameVerifier(httpsUtil.hv); // 请求Headers Map headerMap = new HashMap<>(); headerMap.put(Constant.HEADER_APP_AUTH, Constant.AUTH_HEADER_VALUE); headerMap.put(Constant.HEADER_APP_AKSK, StringUtil.buildAKSKHeader(Constant.CALLNOTIFY_APPID, Constant.CALLNOTIFY_SECRET)); // 构造消息体 Map bodys = new HashMap<>(); bodys.put("displayNbr", displayNbr);//主叫用户手机终端的来电显示号码。 bodys.put("calleeNbr", calleeNbr);//发起呼叫时所拨打的被叫号码。 bodys.put("playInfoList", playInfoList);//播放信息列表,最大支持5个,每个播放信息携带的参数都可以不相同。 String jsonRequest = JsonUtil.jsonObj2Sting(bodys); /* * Content-Type为application/json且请求方法为post时, 使用doPostJsonGetStatusLine方法构造http * request并获取响应. */ StreamClosedHttpResponse responseCallNotify = httpsUtil.doPostJsonGetStatusLine(urlCallNotify, headerMap, jsonRequest); // 响应的消息体写入Responsebody. Responsebody = JsonUtil.jsonString2SimpleObj(responseCallNotify.getContent(), Responsebody.getClass()); // 返回响应的status. return responseCallNotify.getStatusLine().toString(); } /* * 构造playInfoList中携带的放音内容参数 使用语音文件或者v1.0版本接口的TTS文本作为放音内容 */ public Map getplayInfo(String fileorTTS) { Map body = new HashMap(); // 音频文件只支持wave格式,文件名以.wav结尾 if (fileorTTS.endsWith(".wav")) { body.put("notifyVoice", fileorTTS); } else { System.out.println("Only .wav file is supported."); } return body; } /* * 构造playInfoList中携带的放音内容参数 使用v2.0版本接口的TTS模板作为放音内容 重构getplayInfo方法 */ public Map getplayInfo(String templateId, List templateParas) { Map bodys = new HashMap(); bodys.put("templateId", templateId); bodys.put("templateParas", templateParas); return bodys; } // 获取整个响应消息体 public Map getResponsebody() { return this.Responsebody; } // 获取响应消息体中的单个参数 public String getResponsePara(String ParaName) { return this.Responsebody.get(ParaName); } }