-
-
Notifications
You must be signed in to change notification settings - Fork 9.1k
补充企业微信智能机器人 API 模式 JSON 回调消息模型与服务调用入口 #4011
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
...src/main/java/me/chanjar/weixin/cp/bean/intelligentrobot/WxCpIntelligentRobotMessage.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| package me.chanjar.weixin.cp.bean.intelligentrobot; | ||
|
|
||
| import com.google.gson.annotations.SerializedName; | ||
| import lombok.Data; | ||
| import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * 企业微信智能机器人回调消息. | ||
| * | ||
| * <p>官方文档: https://developer.work.weixin.qq.com/document/path/100719</p> | ||
| */ | ||
| @Data | ||
| public class WxCpIntelligentRobotMessage implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| /** | ||
| * 本次回调的唯一性标志. | ||
| */ | ||
| @SerializedName("msgid") | ||
| private String msgId; | ||
|
|
||
| /** | ||
| * 智能机器人id. | ||
| */ | ||
| @SerializedName("aibotid") | ||
| private String aiBotId; | ||
|
|
||
| /** | ||
| * 会话id,仅群聊类型时返回. | ||
| */ | ||
| @SerializedName("chatid") | ||
| private String chatId; | ||
|
|
||
| /** | ||
| * 会话类型,single/group. | ||
| */ | ||
| @SerializedName("chattype") | ||
| private String chatType; | ||
|
|
||
| /** | ||
| * 消息发送者. | ||
| */ | ||
| @SerializedName("from") | ||
| private From from; | ||
|
|
||
| /** | ||
| * 支持主动回复消息的临时url. | ||
| */ | ||
| @SerializedName("response_url") | ||
| private String responseUrl; | ||
|
|
||
| /** | ||
| * 消息类型. | ||
| */ | ||
| @SerializedName("msgtype") | ||
| private String msgType; | ||
|
|
||
| @SerializedName("text") | ||
| private Text text; | ||
|
|
||
| @SerializedName("image") | ||
| private Image image; | ||
|
|
||
| @SerializedName("mixed") | ||
| private Mixed mixed; | ||
|
|
||
| @SerializedName("voice") | ||
| private Voice voice; | ||
|
|
||
| @SerializedName("file") | ||
| private FileInfo file; | ||
|
|
||
| @SerializedName("video") | ||
| private Video video; | ||
|
|
||
| @SerializedName("quote") | ||
| private Quote quote; | ||
|
|
||
| @SerializedName("stream") | ||
| private Stream stream; | ||
|
|
||
| public static WxCpIntelligentRobotMessage fromJson(String json) { | ||
| return WxCpGsonBuilder.create().fromJson(json, WxCpIntelligentRobotMessage.class); | ||
| } | ||
|
|
||
| public String toJson() { | ||
| return WxCpGsonBuilder.create().toJson(this); | ||
| } | ||
|
|
||
| @Data | ||
| public static class From implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("userid") | ||
| private String userid; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Text implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("content") | ||
| private String content; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Image implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("url") | ||
| private String url; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Voice implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("content") | ||
| private String content; | ||
| } | ||
|
|
||
| @Data | ||
| public static class FileInfo implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("url") | ||
| private String url; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Video implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("url") | ||
| private String url; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Stream implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("id") | ||
| private String id; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Mixed implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("msg_item") | ||
| private List<MixedItem> msgItem; | ||
| } | ||
|
|
||
| @Data | ||
| public static class MixedItem implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("msgtype") | ||
| private String msgType; | ||
|
|
||
| @SerializedName("text") | ||
| private Text text; | ||
|
|
||
| @SerializedName("image") | ||
| private Image image; | ||
| } | ||
|
|
||
| @Data | ||
| public static class Quote implements Serializable { | ||
| private static final long serialVersionUID = -1L; | ||
|
|
||
| @SerializedName("msgtype") | ||
| private String msgType; | ||
|
|
||
| @SerializedName("text") | ||
| private Text text; | ||
|
|
||
| @SerializedName("image") | ||
| private Image image; | ||
|
|
||
| @SerializedName("mixed") | ||
| private Mixed mixed; | ||
|
|
||
| @SerializedName("voice") | ||
| private Voice voice; | ||
|
|
||
| @SerializedName("file") | ||
| private FileInfo file; | ||
|
|
||
| @SerializedName("video") | ||
| private Video video; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...test/java/me/chanjar/weixin/cp/bean/intelligentrobot/WxCpIntelligentRobotMessageTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| package me.chanjar.weixin.cp.bean.intelligentrobot; | ||
|
|
||
| import org.testng.annotations.Test; | ||
|
|
||
| import static org.testng.Assert.assertEquals; | ||
| import static org.testng.Assert.assertNotNull; | ||
| import static org.testng.Assert.assertNull; | ||
|
|
||
| /** | ||
| * 智能机器人回调消息测试. | ||
| */ | ||
| public class WxCpIntelligentRobotMessageTest { | ||
|
|
||
| @Test | ||
| public void testFromJsonWithTextMessage() { | ||
| String json = "{" | ||
| + "\"msgid\":\"msg_1\"," | ||
| + "\"aibotid\":\"bot_1\"," | ||
| + "\"chatid\":\"chat_1\"," | ||
| + "\"chattype\":\"group\"," | ||
| + "\"from\":{\"userid\":\"zhangsan\"}," | ||
| + "\"response_url\":\"https://example.com/reply\"," | ||
| + "\"msgtype\":\"text\"," | ||
| + "\"text\":{\"content\":\"@robot hello\"}" | ||
| + "}"; | ||
|
|
||
| WxCpIntelligentRobotMessage message = WxCpIntelligentRobotMessage.fromJson(json); | ||
| assertEquals(message.getMsgId(), "msg_1"); | ||
| assertEquals(message.getAiBotId(), "bot_1"); | ||
| assertEquals(message.getChatId(), "chat_1"); | ||
| assertEquals(message.getChatType(), "group"); | ||
| assertNotNull(message.getFrom()); | ||
| assertEquals(message.getFrom().getUserid(), "zhangsan"); | ||
| assertEquals(message.getResponseUrl(), "https://example.com/reply"); | ||
| assertEquals(message.getMsgType(), "text"); | ||
| assertNotNull(message.getText()); | ||
| assertEquals(message.getText().getContent(), "@robot hello"); | ||
| assertNull(message.getMixed()); | ||
| assertNull(message.getStream()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFromJsonWithMixedAndQuote() { | ||
| String json = "{" | ||
| + "\"msgid\":\"msg_2\"," | ||
| + "\"aibotid\":\"bot_2\"," | ||
| + "\"chattype\":\"single\"," | ||
| + "\"from\":{\"userid\":\"lisi\"}," | ||
| + "\"msgtype\":\"mixed\"," | ||
| + "\"mixed\":{\"msg_item\":[" | ||
| + "{\"msgtype\":\"text\",\"text\":{\"content\":\"hello\"}}," | ||
| + "{\"msgtype\":\"image\",\"image\":{\"url\":\"https://example.com/1.png\"}}" | ||
| + "]}," | ||
| + "\"quote\":{\"msgtype\":\"text\",\"text\":{\"content\":\"quoted\"}}" | ||
| + "}"; | ||
|
|
||
| WxCpIntelligentRobotMessage message = WxCpIntelligentRobotMessage.fromJson(json); | ||
| assertEquals(message.getMsgType(), "mixed"); | ||
| assertNotNull(message.getMixed()); | ||
| assertNotNull(message.getMixed().getMsgItem()); | ||
| assertEquals(message.getMixed().getMsgItem().size(), 2); | ||
| assertEquals(message.getMixed().getMsgItem().get(0).getMsgType(), "text"); | ||
| assertEquals(message.getMixed().getMsgItem().get(0).getText().getContent(), "hello"); | ||
| assertEquals(message.getMixed().getMsgItem().get(1).getMsgType(), "image"); | ||
| assertEquals(message.getMixed().getMsgItem().get(1).getImage().getUrl(), "https://example.com/1.png"); | ||
| assertNotNull(message.getQuote()); | ||
| assertEquals(message.getQuote().getMsgType(), "text"); | ||
| assertEquals(message.getQuote().getText().getContent(), "quoted"); | ||
|
|
||
| String serialized = message.toJson(); | ||
| WxCpIntelligentRobotMessage deserialized = WxCpIntelligentRobotMessage.fromJson(serialized); | ||
| assertEquals(deserialized.getAiBotId(), "bot_2"); | ||
| assertEquals(deserialized.getFrom().getUserid(), "lisi"); | ||
| assertEquals(deserialized.getMixed().getMsgItem().size(), 2); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For image/file/video callbacks, WeCom sends the download URL together with an
aeskeyused to decrypt the downloaded media. These media payload classes only retainurl, sofromJson()silently drops the key and callers receiving encrypted image, file, or video messages cannot use the SDK model to decrypt the media without reparsing the raw JSON.Useful? React with 👍 / 👎.