Java專案整合Luosimao簡訊介面傳送簡訊
public class SendMessage {
private static final String LUOSIMAO_USER = “api”;
private static final String urlSendSingle = “http://sms-api.luosimao.com/v1/send.json”; //傳送單條簡訊介面
private static final String urlSendBatch = “http://sms-api.luosimao.com/v1/send_batch.json”; //傳送批量簡訊介面
private static final String urlSerchString = “
private static final String MESSAGE_MODEL_STRING = “【簡訊簽名】”; //簡訊簽名 應與luosimao賬戶中設定的簽名一致,如果不一致則不可用
public static Map responseMap =new HashMap();
private static final String keyString = “輸入你的驗證密碼”; //驗證密碼
static{
if(responseMap.isEmpty()){
responseMap.put(“0”,“傳送成功”);
responseMap.put("-10",“驗證資訊失敗”);
responseMap.put("-11",“使用者介面被禁用”);
responseMap.put("-20",“簡訊餘額不足”);
responseMap.put("-30 “,“簡訊內容為空”);
responseMap.put(”-31",“簡訊內容存在敏感詞”);
responseMap.put("-32",“簡訊內容缺少簽名信息”);
responseMap.put("-33",“簡訊過長,超過300字(含簽名)”);
responseMap.put("-34",“簽名不可用”);
responseMap.put("-40",“錯誤的手機號”);
responseMap.put("-41",“號碼在黑名單中”);
responseMap.put("-42",“驗證碼類簡訊傳送頻率過快”);
responseMap.put("-43",“號碼數量太多”);
responseMap.put("-50",“請求傳送IP不在白名單內”);
responseMap.put("-60",“定時時間為過去”);
}
}
/**
* 執行方法入口—傳送簡訊
* @return 返回傳送狀態資訊
*/
public static String MessageExecuteMethod(String mobile,String message){
String resultString = null;
String messageString = message+MESSAGE_MODEL_STRING; //傳送簡訊內容 MultivaluedMapImpl formData = new MultivaluedMapImpl(); formData.add("message", messageString); //必須要在luosimao官網上新增簡訊模板,並且這裡的資訊要符合簡訊模板的要求,這樣傳送的時候就不會經過他們進行稽核,傳送速度快 if(!StringUtils.isEmpty(mobile)){ //手機號碼不為空 if(mobile.split(",").length<2){ //單條資訊 formData.add("mobile", mobile); resultString = SendMessageMethod(formData,urlSendSingle); }else{ formData.add("mobile_list", mobile); resultString = SendMessageMethod(formData,urlSendBatch); } }else{ formData.add("moblie", ""); resultString = SendMessageMethod(formData,urlSendSingle); } return resultString; } /** * 執行方法入口----賬戶查詢 * @return map 如果互動成功會有兩個key "code"表示返回狀態程式碼,"msg"表示返回資訊 * */ public static Map UserInfoSearchMethod(){ Map resultMap = SearchUserDetails(urlSerchString); return resultMap; } /** * 傳送簡訊邏輯 */ public static String SendMessageMethod(MultivaluedMapImpl formData,String urlString){ String responseString = null; Client client = Client.create(); client.addFilter(new HTTPBasicAuthFilter(LUOSIMAO_USER,"key-"+keyString)); WebResource webResource = client.resource(urlString); ClientResponse response = webResource.type( MediaType.APPLICATION_FORM_URLENCODED ).post(ClientResponse.class, formData); String textEntity = response.getEntity(String.class); try { JSONObject jsonObj = new JSONObject( textEntity ); String error_code = jsonObj.getInt("error")+""; responseString= (String) responseMap.get(error_code); } catch (JSONException ex) { Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex); } return responseString; } /** * 賬戶資訊(查詢餘額) */ private static Map SearchUserDetails(String urlString){ Map resultMap = new HashMap(); Client client = Client.create(); client.addFilter(new HTTPBasicAuthFilter(LUOSIMAO_USER,"key-"+keyString)); WebResource webResource = client.resource(urlString); ClientResponse response = webResource.get( ClientResponse.class ); String textEntity = response.getEntity(String.class); try { JSONObject jsonObj = new JSONObject( textEntity ); String error_code = jsonObj.getInt("error")+""; String deposit = jsonObj.getInt("deposit")+""; resultMap.put("code", responseMap.get(error_code)); resultMap.put("counts", deposit); } catch (JSONException ex) { Logger.getLogger(SendMessage.class.getName()).log(Level.SEVERE, null, ex); } return resultMap; }
public static void main(String[] args) {
Map map = new HashMap();
map.put(“mobile”, “手機號”);
map.put(“message”, “斯賓特體育提醒您,距離您的參賽時間還剩2天!【斯賓特體育】”); //符合該註冊賬號中的簡訊模板會提高發送速度
String telResponse = SendMessage.MessageExecuteMethod("","");
System.out.println(telResponse);
Map userResponse = SendMessage.UserInfoSearchMethod();
System.out.println(“傳送狀態:”+userResponse.get(“code”));
System.out.println(“賬戶餘額:”+userResponse.get(“counts”));
}
}
luosimao網站:https://luosimao.com/docs/api/