java實現自動撥打電話語音提示
阿新 • • 發佈:2018-11-23
本貼只做記錄,感興趣的朋友可以私信我或加我Q詢問464744895
需求:根據電話號碼,自動撥打電話TTS生成語音進行提示
用的是提供外呼技術支援的服務商,測試程式碼可用:
package weixin.dianhua; import java.net.Socket; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import net.sf.json.JSONObject; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.PostMethod; import java.io.*; import Decoder.BASE64Encoder; import java.util.TreeMap; public class dmdemo_10256_test { // public static void main(String[] args) throws Exception // { public void dianhua(String userstr,String tel)throws Exception{ HttpClient client = new HttpClient(); client.getHostConfiguration().setHost("***.******.***", 80, "http"); //client.getHostConfiguration().setHost("114.55.234.215", 80, "http"); HttpMethod method = getPostMethod(userstr,tel); client.executeMethod(method); //System.out.println(method.getStatusLine()); /* String response = new String(method.getResponseBodyAsString().getBytes("utf-8")); System.out.println(response); */ /* getResponseBodyAsStream start */ InputStream inputStream = method.getResponseBodyAsStream(); BufferedReader br = new BufferedReader(new InputStreamReader(inputStream)); StringBuffer response = new StringBuffer(); String read = ""; while((read = br.readLine()) != null){ response.append(read); } System.out.println(response); /* getResponseBodyAsStream start */ method.releaseConnection(); } public static String encrypt(String strKey, String strIn) throws Exception { String ivstr = strKey.substring(0, 16); SecretKeySpec skeySpec = getKey(strKey); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(ivstr.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv); byte[] encrypted = cipher.doFinal(strIn.getBytes()); return new BASE64Encoder().encode(encrypted); } private static SecretKeySpec getKey(String strKey) throws Exception { byte[] arrBTmp = strKey.getBytes(); byte[] arrB = new byte[16]; // 建立一個空的16位位元組陣列(預設值為0) for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; } SecretKeySpec skeySpec = new SecretKeySpec(arrB, "AES"); return skeySpec; } private static HttpMethod getPostMethod(String userstr,String tel) throws Exception { PostMethod post = new UTF8PostMethod("/api/v2/data"); // 資料請求介面 //PostMethod post = new UTF8PostMethod("/api/dm_1.php"); // 資料請求介面 int vccId = 200027; int proId = 10356; String token = "631ddb2c05e054303c318d06d2202420c2ee1691"; Map templateInfo = new LinkedHashMap(); //templateInfo.put("productPrice", "99.8"); //templateInfo.put("productName", "產品名稱"); // String userstr = "今天下午三點開會"; templateInfo.put("remindMsg", userstr); // LinkedHashMap 是HashMap的一個子類,如果需要輸出的順序和輸入的相同 Map data = new LinkedHashMap(); // 順序是完全隨機,具有很快的訪問速度 Map map = new TreeMap(); // 預設是按鍵值的升序排序 map.put("phoneNum", tel); map.put("callInterval", 20); map.put("callNum", 0); map.put("templateInfo", templateInfo); data.put("vccId", vccId); data.put("proId", proId); String mapStr = JSONObject.fromObject(map).toString(); String str = mapStr + token; String sign = SHA1(str); data.put("sign", sign); //data.put("data", encrypt(token, mapStr)); data.put("data", map); //MessageDigest md = MessageDigest.getInstance("SHA-1"); //md.reset(); JSONObject json = JSONObject.fromObject(data); post.setRequestBody(json.toString()); System.out.println(json); //System.exit(0); return post; } public static String SHA1(String decript) { try { MessageDigest digest = java.security.MessageDigest.getInstance("SHA-1"); digest.update(decript.getBytes()); byte messageDigest[] = digest.digest(); // Create Hex String StringBuffer hexString = new StringBuffer(); // 位元組陣列轉換為 十六進位制 數 for (int i = 0; i < messageDigest.length; i++) { String shaHex = Integer.toHexString(messageDigest[i] & 0xFF); if (shaHex.length() < 2) { hexString.append(0); } hexString.append(shaHex); } return hexString.toString(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return ""; } /* * 中文轉unicode編碼 */ public static String gbEncoding(final String gbString) { char[] utfBytes = gbString.toCharArray(); String unicodeBytes = ""; for (int i = 0; i < utfBytes.length; i++) { String hexB = Integer.toHexString(utfBytes[i]); if (hexB.length() <= 2) { hexB = "00" + hexB; } unicodeBytes = unicodeBytes + "\\u" + hexB; } return unicodeBytes; } /* * unicode編碼轉中文 */ public static String decodeUnicode(final String dataStr) { int start = 0; int end = 0; final StringBuffer buffer = new StringBuffer(); while (start > -1) { end = dataStr.indexOf("\\u", start + 2); String charStr = ""; if (end == -1) { charStr = dataStr.substring(start + 2, dataStr.length()); } else { charStr = dataStr.substring(start + 2, end); } char letter = (char) Integer.parseInt(charStr, 16); // 16進位制parse整形字串。 buffer.append(new Character(letter).toString()); start = end; } return buffer.toString(); } }
UTF8工具
package weixin.dianhua; import org.apache.commons.httpclient.methods.PostMethod; public class UTF8PostMethod extends PostMethod{ public UTF8PostMethod(String url){ super(url); } @Override public String getRequestCharSet() { //return super.getRequestCharSet(); return "utf-8"; } }