Skip to content

Commit

Permalink
Merge pull request #459 from sxci/sms_add_sendmsg
Browse files Browse the repository at this point in the history
Sms add sendmsg
  • Loading branch information
sxci authored Oct 17, 2019
2 parents 2ad7b46 + 1f6a21e commit 98e6d94
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 6 deletions.
62 changes: 58 additions & 4 deletions src/main/java/com/qiniu/sms/SmsManager.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.qiniu.sms;

import java.util.Map;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.MethodType;
Expand All @@ -13,6 +11,8 @@
import com.qiniu.util.StringMap;
import com.qiniu.util.UrlUtils;

import java.util.Map;

public class SmsManager {

/**
Expand All @@ -35,14 +35,19 @@ public class SmsManager {
* 构建一个新的 SmsManager 对象
*
* @param auth Auth对象
* @param cfg Configuration对象
*/
public SmsManager(Auth auth) {
this.auth = auth;
this.configuration = new Configuration();
client = new Client(this.configuration);
}

/**
* 构建一个新的 SmsManager 对象
*
* @param auth Auth对象
* @param cfg Configuration对象
*/
public SmsManager(Auth auth, Configuration cfg) {
this.auth = auth;
this.configuration = cfg.clone();
Expand All @@ -54,7 +59,7 @@ public SmsManager(Auth auth, Configuration cfg) {
*
* @param templateId 模板Id,必填
* @param mobiles 手机号码数组,必填
* @param parameter 参数,必填
* @param parameters 参数,必填
*/
public Response sendMessage(String templateId, String[] mobiles, Map<String, String> parameters)
throws QiniuException {
Expand All @@ -66,6 +71,55 @@ public Response sendMessage(String templateId, String[] mobiles, Map<String, Str
return post(requestUrl, Json.encode(bodyMap).getBytes());
}

/**
* 发送单条短信
*
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,必填
*/
public Response sendSingleMessage(String templateId, String mobile, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message/single", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.put("parameters", parameters);
return post(requestUrl, Json.encode(bodyMap).getBytes());
}

/**
* 发送国际短信
*
* @param templateId 模板Id,必填
* @param mobile 手机号码,必填
* @param parameters 参数,必填
*/
public Response sendOverseaMessage(String templateId, String mobile, Map<String, String> parameters)
throws QiniuException {
String requestUrl = String.format("%s/v1/message/oversea", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("template_id", templateId);
bodyMap.put("mobile", mobile);
bodyMap.put("parameters", parameters);
return post(requestUrl, Json.encode(bodyMap).getBytes());
}

/**
* 发送全文本短信(不需要传模版 ID)
*
* @param mobiles 手机号码数组,必填
* @param content 短信内容,必须是已经审核通过的签名和模版,必填。
* 例如:【七牛云】您的验证码是 287712,5分钟内有效
*/
public Response sendFulltextMessage(String[] mobiles, String content) throws QiniuException {
String requestUrl = String.format("%s/v1/message/fulltext", configuration.smsHost());
StringMap bodyMap = new StringMap();
bodyMap.put("mobiles", mobiles);
bodyMap.put("content", content);
return post(requestUrl, Json.encode(bodyMap).getBytes());
}

/**
* 查询签名
*
Expand Down
57 changes: 55 additions & 2 deletions src/test/java/test/com/qiniu/sms/SmsTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package test.com.qiniu.sms;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Client;
import com.qiniu.http.Response;
Expand All @@ -22,6 +23,8 @@

public class SmsTest {
private SmsManager smsManager;
private String mobile = ""; // 一国内手机号
private String[] mobiles = new String[]{mobile,};

/**
* 初始化
Expand All @@ -33,14 +36,64 @@ public void setUp() throws Exception {
this.smsManager = new SmsManager(Auth.create(TestConfig.smsAccessKey, TestConfig.smsSecretKey));
}

@Test
public void testJson() {
Map<String, String> paramMap5 = new HashMap<String, String>();
paramMap5.put("bbbb", "sdsdsd");
String json5 = new Gson().toJson(paramMap5);
Assert.assertTrue("{\"bbbb\":\"sdsdsd\"}".equals(json5));

Map<String, String> paramMap6 = new HashMap<String, String>() {{
this.put("bbbb", "sdsdsd");
}};
String json6 = new Gson().toJson(paramMap6);
Assert.assertTrue("null".equals(json6));
}

@Test
public void testSendMessage() {
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("code", "12945");
try {
Response response = smsManager.sendMessage("1138278041873555456", mobiles, paramMap);
Assert.assertNotNull(response);
} catch (QiniuException e) {
Assert.assertTrue(ResCode.find(e.code(), ResCode.getPossibleResCode(401)));
}
}

@Test
public void sendSingleMessage() {
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("code", "9signle78");
try {
Response response = smsManager.sendSingleMessage("1138278041873555456", mobile, paramMap);
Assert.assertNotNull(response);
} catch (QiniuException e) {
Assert.assertTrue(ResCode.find(e.code(), ResCode.getPossibleResCode(401)));
}
}

@Test
public void sendOverseaMessage() {
Map<String, String> paramMap = new HashMap<String, String>();
paramMap.put("code", "8612345");
try {
// 测试手机后为 国内一手机号,加上 +86
Response response = smsManager.sendOverseaMessage("1184679569681027072", "+86" + mobile, paramMap);
Assert.assertNotNull(response);
} catch (QiniuException e) {
Assert.assertTrue(ResCode.find(e.code(), ResCode.getPossibleResCode(401)));
}
}

@Test
public void sendFulltextMessage() {
try {
Map<String, String> paramMap = new HashMap<String, String>();
Response response = smsManager.sendMessage("test", new String[]{"10086"}, paramMap);
Response response = smsManager.sendFulltextMessage(mobiles, "【七牛云】尊敬的用户你好,您的验证码是 38232");
Assert.assertNotNull(response);
} catch (QiniuException e) {
e.printStackTrace();
Assert.assertTrue(ResCode.find(e.code(), ResCode.getPossibleResCode(401)));
}
}
Expand Down

0 comments on commit 98e6d94

Please sign in to comment.