feat(cp-tp): Add WxCpTpMessageService for third-party app message sending#3931
feat(cp-tp): Add WxCpTpMessageService for third-party app message sending#3931
Conversation
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
本次变更在 weixin-java-cp 的第三方应用(TP)能力中补齐“代授权企业发送/撤回应用消息”等消息推送相关接口,并将其挂载到 WxCpTpService 体系中,便于调用方通过统一入口获取消息服务。
Changes:
- 新增
WxCpTpMessageService及其实现WxCpTpMessageServiceImpl,支持发送消息、查询统计、互联企业消息、学校通知、撤回消息(均按 corpId 使用对应 access_token,并以withoutSuiteAccessToken=true发起请求)。 - 在
WxCpTpService/BaseWxCpTpServiceImpl中新增并初始化消息服务的 getter/setter。 - 补充对应的 TestNG 用例并纳入
testng.xml测试套件。
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| weixin-java-cp/src/test/resources/testng.xml | 将新增的消息服务测试类加入 TestNG 执行列表 |
| weixin-java-cp/src/test/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImplTest.java | 新增消息服务单测,验证调用时 withoutSuiteAccessToken=true 且使用 corpId 对应 access_token |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java | 在 TP 总入口接口上新增消息服务的 get/set 声明 |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpMessageService.java | 新增第三方应用消息推送服务接口定义 |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java | 新增接口实现,封装消息相关 API 调用 |
| weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/BaseWxCpTpServiceImpl.java | 初始化并暴露消息服务实例 |
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpService.java
Outdated
Show resolved
Hide resolved
🤖 Augment PR Summary总结:本 PR 为企业微信「第三方应用(TP)」模块补齐了缺失的消息发送能力,提供与自建应用 变更内容:
技术要点:TP 场景下没有默认应用上下文,调用方需要自行在消息体中设置 🤖 Was this summary useful? React with 👍 or 👎 |
| * @return 消息发送结果 | ||
| * @throws WxErrorException 微信错误异常 | ||
| */ | ||
| WxCpMessageSendResult send(WxCpMessage message, String corpId) throws WxErrorException; |
There was a problem hiding this comment.
TP 场景下如果消息体未显式设置 agentId(例如 WxCpMessage/WxCpLinkedCorpMessage/WxCpSchoolContactMessage),接口通常会直接返回参数错误;这里建议在接口 Javadoc 里明确“调用方必须自行设置 agentId”,避免误用。
Severity: low
Other Locations
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpMessageService.java:55weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpMessageService.java:71
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
|
|
||
| @Override | ||
| public WxCpMessageSendResult send(WxCpMessage message, String corpId) throws WxErrorException { | ||
| String url = mainService.getWxCpTpConfigStorage().getApiUrl(MESSAGE_SEND) |
There was a problem hiding this comment.
这里用 + "?access_token=" 直接拼接 URL,若 getApiUrl(...) 在某些私有化/网关场景下已带查询参数,可能生成 ?? 导致请求失败;可以考虑按 uri.contains("?") ? "&" : "?" 的方式拼接以规避边界问题。
Severity: low
Other Locations
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:35weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:44weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:52weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:61
🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.
| * | ||
| * @throws WxErrorException 微信错误异常 | ||
| */ | ||
| @Test |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
The
tpmodule was missing a message service — unlike the regularapipackage which hasWxCpMessageService, there was no equivalent for third-party apps to send messages on behalf of authorized corps.Changes
New
WxCpTpMessageServiceinterface (tp/service/) — mirrorsWxCpMessageServicewith an extracorpIdparameter on every method:send(WxCpMessage, String corpId)getStatistics(int timeType, String corpId)sendLinkedCorpMessage(WxCpLinkedCorpMessage, String corpId)sendSchoolContactMessage(WxCpSchoolContactMessage, String corpId)recall(String msgId, String corpId)New
WxCpTpMessageServiceImpl— fetches the authorized corp'saccess_tokenviagetAccessToken(corpId)and callspost(url, data, withoutSuiteAccessToken=true)to prevent inadvertentsuite_access_tokeninjection. Consistent with the pattern inWxCpTpOAServiceImplandWxCpTpUserServiceImpl.WxCpTpService+BaseWxCpTpServiceImpl— wiredgetWxCpTpMessageService()/setWxCpTpMessageService()with default initialization.Usage
The key distinction from the regular
WxCpMessageService: every call requires an explicitcorpId— there is no default agent context in the TP service, so callers must setagentIdon the message themselves.Original prompt
🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.