1
0
mirror of synced 2025-12-23 18:48:00 +08:00

🆕 #2725【企业微信】增加家校沟通-发送「学校通知」的接口

This commit is contained in:
0katekate0
2022-07-06 23:50:32 +08:00
committed by GitHub
parent 918daa2a55
commit f30ac6be6c
17 changed files with 1188 additions and 4 deletions

View File

@@ -52,6 +52,86 @@ public class WxCpSchoolUserTest {
final String exUserId = "wmOQpTDwAAJFHrryZ8I8ALLEZuLHIUKA";
// 返回值
String batchResult = "{\n" +
"\t\"errcode\": 1,\n" +
"\t\"errmsg\": \"invalid student_userid: zhangsan\",\n" +
"\t\"result_list\": [\n" +
"\t\t{\n" +
"\t\t\t\"student_userid\": \"zhangsan\",\n" +
"\t\t\t\"errcode\": 1,\n" +
"\t\t\t\"errmsg\": \"invalid student_userid: zhangsan\"\n" +
"\t\t}\n" +
"\t]\n" +
"}";
WxCpBatchResultList batchResultList = WxCpBatchResultList.fromJson(batchResult);
log.info("batchResultList: {}", batchResultList.toJson());
/**
* 批量更新学生
* https://developer.work.weixin.qq.com/document/path/92330
*
* 请求方式POSTHTTPS
* 请求地址https://qyapi.weixin.qq.com/cgi-bin/school/user/batch_update_student?access_token=ACCESS_TOKEN
*/
String batchUpdateStudent = "{\n" +
"\t\"students\":[\n" +
" {\n" +
"\t\t\t\"student_userid\": \"zhangsan\",\n" +
"\t\t\t\"new_student_userid\":\"zhangsan_new\",\n" +
"\t\t\t\"name\": \"张三\",\n" +
"\t\t\t\"department\": [1, 2]\n" +
"\t\t},\n" +
" {\n" +
"\t\t\t\"student_userid\": \"lisi\",\n" +
"\t\t\t\"name\": \"李四\",\n" +
"\t\t\t\"department\": [3, 4]\n" +
"\t\t}\n" +
" ]\n" +
"}";
WxCpBatchUpdateStudentRequest batchUpdateStudentRequest = WxCpBatchUpdateStudentRequest.fromJson(batchUpdateStudent);
WxCpBatchResultList list3 = cpService.getSchoolUserService().batchUpdateStudent(batchUpdateStudentRequest);
log.info("list3: {}", list3.toJson());
/**
* 批量删除学生
* https://developer.work.weixin.qq.com/document/path/92329
*
* 请求方式POSTHTTPS
* 请求地址https://qyapi.weixin.qq.com/cgi-bin/school/user/batch_delete_student?access_token=ACCESS_TOKEN
*/
String batchDeleteStudent = "{\n" +
"\t\"useridlist\": [\"zhangsan\", \"lisi\"]\n" +
"}\n";
WxCpBatchDeleteStudentRequest batchDeleteStudentRequest = WxCpBatchDeleteStudentRequest.fromJson(batchDeleteStudent);
WxCpBatchResultList list2 = cpService.getSchoolUserService().batchDeleteStudent(batchDeleteStudentRequest);
log.info("list2: {}", list2.toJson());
/**
* 批量创建学生
* https://developer.work.weixin.qq.com/document/path/92328
*/
String batchCreateStudent = "{\n" +
"\t\"students\":[\n" +
" {\n" +
"\t\t\t\"student_userid\": \"zhangsan\",\n" +
"\t\t\t\"name\": \"张三\",\n" +
"\t\t\t\"department\": [1, 2]\n" +
"\t\t},\n" +
" {\n" +
"\t\t\t\"student_userid\": \"lisi\",\n" +
"\t\t\t\"name\": \"李四\",\n" +
"\t\t\t\"department\": [3, 4]\n" +
"\t\t}\n" +
" ]\n" +
"}";
WxCpBatchCreateStudentRequest batchCreateStudentRequest = WxCpBatchCreateStudentRequest.fromJson(batchCreateStudent);
WxCpBatchResultList list1 = cpService.getSchoolUserService().batchCreateStudent(batchCreateStudentRequest);
log.info("list1: {}", list1.toJson());
// String changeContact = WxCpConsts.EventType.CHANGE_CONTACT;
/**
* 增加变更事件类型:

View File

@@ -7,7 +7,6 @@ import com.google.inject.Inject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.ApiTestModule;
import me.chanjar.weixin.cp.api.ApiTestModuleWithMockServer;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.message.WxCpLinkedCorpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;

View File

@@ -0,0 +1,395 @@
package me.chanjar.weixin.cp.bean.message;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.util.json.GsonParser;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
import me.chanjar.weixin.cp.bean.article.NewArticle;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.demo.WxCpDemoInMemoryConfigStorage;
import org.testng.annotations.Test;
import java.io.InputStream;
import static org.assertj.core.api.Assertions.assertThat;
/**
* 发送「学校通知」消息测试类
* https://developer.work.weixin.qq.com/document/path/92321
*
* @author <a href="https://github.com/0katekate0">Wang_Wong</a>
* @date 2022-06-29
*/
@Slf4j
public class WxCpSchoolContactMessageTest {
private static WxCpConfigStorage wxCpConfigStorage;
private static WxCpService cpService;
/**
* 发送「学校通知」
* 学校可以通过此接口来给家长发送不同类型的学校通知,来满足多种场景下的学校通知需求。目前支持的消息类型为文本、图片、语音、视频、文件、图文。
*
* https://developer.work.weixin.qq.com/document/path/92321
*
* 消息体类型请参考测试类
* WxCpSchoolContactMessageTest
* {@link WxCpSchoolContactMessageTest}
* @throws WxErrorException
*/
@Test
public void testSendSchoolContactMessage() throws WxErrorException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("test-config.xml");
WxCpDemoInMemoryConfigStorage config = WxCpDemoInMemoryConfigStorage.fromXml(inputStream);
wxCpConfigStorage = config;
cpService = new WxCpServiceImpl();
cpService.setWxCpConfigStorage(config);
WxCpSchoolContactMessageSendResult sendResult = this.cpService.getMessageService().sendSchoolContactMessage(
WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.TEXT)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.content("你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。")
.enableIdTrans(false)
.enableDuplicateCheck(false)
.duplicateCheckInterval(1800)
.build()
);
log.info("sendResult: {}", sendResult.toJson());
}
// WxCpConsts.SchoolContactChangeType
@Test
public void testToJson_text() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.TEXT)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.content("你的快递已到,请携带工卡前往邮件中心领取。\n出发前可查看<a href=\"http://work.weixin.qq.com\">邮件中心视频实况</a>,聪明避开排队。")
.enableIdTrans(false)
.enableDuplicateCheck(false)
.duplicateCheckInterval(1800)
.build();
WxCpSchoolContactMessage schoolContactMessage1 = new WxCpSchoolContactMessage();
schoolContactMessage1.setMsgType(WxConsts.SchoolContactMsgType.TEXT);
schoolContactMessage1.setRecvScope(0);
schoolContactMessage1.setToParentUserId(new String[]{"parent_userid1", "parent_userid2"});
schoolContactMessage1.setToStudentUserId(new String[]{"student_userid1", "student_userid2"});
schoolContactMessage1.setToParty(new String[]{"partyid1", "partyid2"});
schoolContactMessage1.setToAll(false);
schoolContactMessage1.setAgentId(1);
schoolContactMessage1.setContent("你的快递已到,请携带工卡前往邮件中心领取");
schoolContactMessage1.setEnableIdTrans(false);
schoolContactMessage1.setEnableDuplicateCheck(false);
schoolContactMessage1.setDuplicateCheckInterval(1800);
final String jsonMsg = schoolContactMessage1.toJson();
final String json = message.toJson();
String expectedJson = "{\n" +
"\t\"recv_scope\" : 0,\n" +
"\t\"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
"\t\"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
"\t\"to_party\": [\"partyid1\", \"partyid2\"],\n" +
"\t\"toall\" : 0,\n" +
"\t\"msgtype\" : \"text\",\n" +
"\t\"agentid\" : 1,\n" +
"\t\"text\" : {\n" +
"\t\t\"content\" : \"你的快递已到,请携带工卡前往邮件中心领取。\\n出发前可查看<a href=\\\"http://work.weixin.qq.com\\\">邮件中心视频实况</a>,聪明避开排队。\"\n" +
"\t},\n" +
"\t\"enable_id_trans\": 0,\n" +
"\t\"enable_duplicate_check\": 0,\n" +
"\t\"duplicate_check_interval\": 1800\n" +
"}";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_image() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.IMAGE)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.mediaId("MEDIA_ID")
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
"\t\"recv_scope\" : 0,\n" +
"\t\"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
"\t\"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
"\t\"to_party\": [\"partyid1\", \"partyid2\"],\n" +
"\t\"toall\" : 0,\n" +
"\t\"msgtype\" : \"image\",\n" +
"\t\"agentid\" : 1,\n" +
"\t\"image\" : {\n" +
"\t\t\"media_id\" : \"MEDIA_ID\"\n" +
"\t},\n" +
"\t\"enable_duplicate_check\": 0,\n" +
"\t\"duplicate_check_interval\": 1800\n" +
"}";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_voice() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.VOICE)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.mediaId("MEDIA_ID")
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
"\t\"recv_scope\" : 0,\n" +
"\t\"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
"\t\"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
"\t\"to_party\": [\"partyid1\", \"partyid2\"],\n" +
"\t\"toall\" : 0,\n" +
"\t\"msgtype\" : \"voice\",\n" +
"\t\"agentid\" : 1,\n" +
"\t\"voice\" : {\n" +
"\t\t\"media_id\" : \"MEDIA_ID\"\n" +
"\t},\n" +
"\t\"enable_duplicate_check\": 0,\n" +
"\t\"duplicate_check_interval\": 1800\n" +
"}";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_video() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.VIDEO)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.mediaId("MEDIA_ID")
.title("Title")
.description("Description")
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
"\t\"recv_scope\" : 0,\n" +
"\t\"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
"\t\"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
"\t\"to_party\": [\"partyid1\", \"partyid2\"],\n" +
"\t\"toall\" : 0,\n" +
"\t\"msgtype\" : \"video\",\n" +
"\t\"agentid\" : 1,\n" +
"\t\"video\" : {\n" +
" \"media_id\" : \"MEDIA_ID\",\n" +
" \"title\" : \"Title\",\n" +
" \"description\" : \"Description\"\n" +
"\t},\n" +
"\t\"enable_duplicate_check\": 0,\n" +
"\t\"duplicate_check_interval\": 1800\n" +
"}";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_file() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.FILE)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.mediaId("1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o")
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
"\t\"recv_scope\" : 0,\n" +
"\t\"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
"\t\"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
"\t\"to_party\": [\"partyid1\", \"partyid2\"],\n" +
"\t\"toall\" : 0,\n" +
"\t\"msgtype\" : \"file\",\n" +
"\t\"agentid\" : 1,\n" +
"\t\"file\" : {\n" +
" \"media_id\" : \"1Yv-zXfHjSjU-7LH-GwtYqDGS-zz6w22KmWAT5COgP7o\"\n" +
"\t},\n" +
"\t\"enable_duplicate_check\": 0,\n" +
"\t\"duplicate_check_interval\": 1800\n" +
"}";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_news() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.NEWS)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.articles(Lists.newArrayList(NewArticle.builder()
.title("中秋节礼品领取")
.description("今年中秋节公司有豪礼相送")
.url("URL")
.picUrl("http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png")
.build()))
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
" \"recv_scope\" : 0,\n" +
" \"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
" \"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
" \"to_party\": [\"partyid1\", \"partyid2\"],\n" +
" \"toall\" : 0,\n" +
" \"msgtype\" : \"news\",\n" +
" \"agentid\" : 1,\n" +
" \"news\" : {\n" +
" \"articles\" : [\n" +
" {\n" +
" \"title\" : \"中秋节礼品领取\",\n" +
" \"description\" : \"今年中秋节公司有豪礼相送\",\n" +
" \"url\" : \"URL\",\n" +
" \"picurl\" : \"http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png\"\n" +
" }\n" +
"\t\t]\n" +
" },\n" +
" \"enable_id_trans\": 0,\n" +
" \"enable_duplicate_check\": 0,\n" +
" \"duplicate_check_interval\": 1800\n" +
"}";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_mpnews() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.MPNEWS)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.mpNewsArticles(Lists.newArrayList(MpnewsArticle.newBuilder()
.title("Title")
.thumbMediaId("MEDIA_ID")
.author("Author")
.contentSourceUrl("URL")
.content("Content")
.digest("Digest description")
.build()))
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
" \"recv_scope\" : 0,\n" +
" \"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
" \"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
" \"to_party\": [\"partyid1\", \"partyid2\"],\n" +
" \"toall\" : 0,\n" +
" \"msgtype\" : \"mpnews\",\n" +
" \"agentid\" : 1,\n" +
" \"mpnews\" : {\n" +
" \"articles\":[\n" +
" {\n" +
" \"title\": \"Title\", \n" +
" \"thumb_media_id\": \"MEDIA_ID\",\n" +
" \"author\": \"Author\",\n" +
" \"content_source_url\": \"URL\",\n" +
" \"content\": \"Content\",\n" +
" \"digest\": \"Digest description\"\n" +
" }\n" +
" ]\n" +
" },\n" +
" \"enable_id_trans\": 0,\n" +
" \"enable_duplicate_check\": 0,\n" +
" \"duplicate_check_interval\": 1800\n" +
"}\n";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
@Test
public void testToJson_miniProgram() {
WxCpSchoolContactMessage message = WxCpSchoolContactMessage.builder()
.recvScope(0)
.msgType(WxConsts.SchoolContactMsgType.MINIPROGRAM)
.toParentUserId(new String[]{"parent_userid1", "parent_userid2"})
.toStudentUserId(new String[]{"student_userid1", "student_userid2"})
.toParty(new String[]{"partyid1", "partyid2"})
.toAll(false)
.agentId(1)
.appId("APPID")
.title("欢迎报名夏令营")
.thumbMediaId("MEDIA_ID")
.pagePath("PAGE_PATH")
.build();
final String json = message.toJson();
String expectedJson = "{\n" +
" \"recv_scope\" : 0,\n" +
" \"to_parent_userid\": [\"parent_userid1\", \"parent_userid2\"],\n" +
" \"to_student_userid\": [\"student_userid1\", \"student_userid2\"],\n" +
" \"to_party\": [\"partyid1\", \"partyid2\"],\n" +
" \"toall\" : 0,\n" +
" \"agentid\" : 1,\n" +
" \"msgtype\" : \"miniprogram\",\n" +
" \"miniprogram\" : {\n" +
" \"appid\": \"APPID\",\n" +
" \"title\": \"欢迎报名夏令营\",\n" +
" \"thumb_media_id\": \"MEDIA_ID\",\n" +
" \"pagepath\": \"PAGE_PATH\"\n" +
" },\n" +
" \"enable_id_trans\": 0,\n" +
" \"enable_duplicate_check\": 0,\n" +
" \"duplicate_check_interval\": 1800\n" +
"}\n";
assertThat(json).isEqualTo(GsonParser.parse(expectedJson).toString());
}
}