Skip to content

feat(cp-tp): Add WxCpTpMessageService for third-party app message sending#3931

Open
Copilot wants to merge 3 commits intodevelopfrom
copilot/add-enterprise-wechat-sdk-module
Open

feat(cp-tp): Add WxCpTpMessageService for third-party app message sending#3931
Copilot wants to merge 3 commits intodevelopfrom
copilot/add-enterprise-wechat-sdk-module

Conversation

Copy link
Contributor

Copilot AI commented Mar 17, 2026

The tp module was missing a message service — unlike the regular api package which has WxCpMessageService, there was no equivalent for third-party apps to send messages on behalf of authorized corps.

Changes

  • New WxCpTpMessageService interface (tp/service/) — mirrors WxCpMessageService with an extra corpId parameter 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's access_token via getAccessToken(corpId) and calls post(url, data, withoutSuiteAccessToken=true) to prevent inadvertent suite_access_token injection. Consistent with the pattern in WxCpTpOAServiceImpl and WxCpTpUserServiceImpl.

  • WxCpTpService + BaseWxCpTpServiceImpl — wired getWxCpTpMessageService() / setWxCpTpMessageService() with default initialization.

Usage

// corpId is the authorized enterprise's corp ID
WxCpMessage msg = WxCpMessage.TEXT()
    .agentId(agentId)
    .toUser("zhangsan")
    .content("Hello from TP app")
    .build();

WxCpMessageSendResult result = wxCpTpService.getWxCpTpMessageService()
    .send(msg, authCorpId);

The key distinction from the regular WxCpMessageService: every call requires an explicit corpId — there is no default agent context in the TP service, so callers must set agentId on the message themselves.

Original prompt

This section details on the original issue you should resolve

<issue_title>企业微信第三方应用SDK封装模块?</issue_title>
<issue_description>Image
企业微信第三方应用中的service是否未封装完?例如发送消息是service没有找到,还是说公用

Image中的service。如果是公用,是如何初始化对象的?</issue_description>

Comments on the Issue (you are @copilot in this section)

Custom agent used: 全部用中文
需要用中文,包括PR标题和分析总结过程


🔒 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.

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
Copilot AI changed the title [WIP] Add encapsulation module for Enterprise WeChat SDK feat(cp-tp): Add WxCpTpMessageService for third-party app message sending Mar 17, 2026
Copilot AI requested a review from binarywang March 17, 2026 02:39
@binarywang binarywang marked this pull request as ready for review March 17, 2026 02:41
Copilot AI review requested due to automatic review settings March 17, 2026 02:41
@binarywang binarywang added this to the 4.8.3 milestone Mar 17, 2026
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 初始化并暴露消息服务实例

@augmentcode
Copy link

augmentcode bot commented Mar 17, 2026

🤖 Augment PR Summary

总结:本 PR 为企业微信「第三方应用(TP)」模块补齐了缺失的消息发送能力,提供与自建应用 WxCpMessageService 对齐的封装,但每个接口额外要求显式传入授权企业的 corpId

变更内容:

  • 新增 WxCpTpMessageService 接口:覆盖应用消息发送、消息统计、互联企业消息、学校通知、消息撤回等能力,并统一增加 corpId 入参
  • 新增 WxCpTpMessageServiceImpl 实现:通过 configStorage.getAccessToken(corpId) 获取授权企业 access_token,并调用 post(url, data, withoutSuiteAccessToken=true) 避免自动拼接 suite_access_token
  • WxCpTpService / BaseWxCpTpServiceImpl 中新增并初始化消息服务的 getter/setter,便于统一入口访问
  • 新增对应单元测试 WxCpTpMessageServiceImplTest,并加入 TestNG 测试套件

技术要点:TP 场景下没有默认应用上下文,调用方需要自行在消息体中设置 agentId,且接口请求需使用授权企业的 access_token(而非 suite_access_token)。

🤖 Was this summary useful? React with 👍 or 👎

Copy link

@augmentcode augmentcode bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review completed. 3 suggestions posted.

Fix All in Augment

Comment augment review to trigger a new review at any time.

* @return 消息发送结果
* @throws WxErrorException 微信错误异常
*/
WxCpMessageSendResult send(WxCpMessage message, String corpId) throws WxErrorException;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:55
  • weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/WxCpTpMessageService.java:71

Fix This in Augment

🤖 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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里用 + "?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:35
  • weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:44
  • weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:52
  • weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/service/impl/WxCpTpMessageServiceImpl.java:61

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

*
* @throws WxErrorException 微信错误异常
*/
@Test
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

当前单测只覆盖了 send/recallwithoutSuiteAccessToken=true 的断言;为了避免后续改动时遗漏,建议为 getStatistics/sendLinkedCorpMessage/sendSchoolContactMessage 也补充同类校验。

Severity: low

Fix This in Augment

🤖 Was this useful? React with 👍 or 👎, or 🚀 if it prevented an incident/outage.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

企业微信第三方应用SDK封装模块?

3 participants