mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
#921 企业微信发送应用消息接口支持Markdown消息格式
This commit is contained in:
parent
3225311b09
commit
568706f155
@ -11,7 +11,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-common</artifactId>
|
||||
<name>WxJava - Common</name>
|
||||
<name>WxJava - Common Java SDK</name>
|
||||
<description>微信开发Java SDK公共模块</description>
|
||||
|
||||
<dependencies>
|
||||
|
@ -62,6 +62,11 @@ public class WxConsts {
|
||||
* 图文消息(点击跳转到图文消息页面).
|
||||
*/
|
||||
public static final String MPNEWS = "mpnews";
|
||||
/**
|
||||
* markdown消息.
|
||||
* (目前仅支持markdown语法的子集,微工作台(原企业号)不支持展示markdown消息)
|
||||
*/
|
||||
public static final String MARKDOWN = "markdown";
|
||||
/**
|
||||
* 发送文件(CP专用).
|
||||
*/
|
||||
|
@ -11,7 +11,7 @@
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-cp</artifactId>
|
||||
<name>WxJava - CP</name>
|
||||
<name>WxJava - CP Java SDK</name>
|
||||
<description>微信企业号/企业微信 Java SDK</description>
|
||||
|
||||
<dependencies>
|
||||
|
@ -10,7 +10,6 @@ import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParser;
|
||||
|
||||
import me.chanjar.weixin.common.bean.WxJsapiSignature;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
@ -141,9 +140,10 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
|
||||
public WxCpMessageSendResult messageSend(WxCpMessage message) throws WxErrorException {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/message/send";
|
||||
Integer agentId = message.getAgentId();
|
||||
if(null == agentId){
|
||||
if (null == agentId) {
|
||||
message.setAgentId(this.getWxCpConfigStorage().getAgentId());
|
||||
}
|
||||
|
||||
return WxCpMessageSendResult.fromJson(this.post(url, message.toJson()));
|
||||
}
|
||||
|
||||
@ -171,7 +171,7 @@ public abstract class BaseWxCpServiceImpl<H, P> implements WxCpService, RequestH
|
||||
}
|
||||
|
||||
/**
|
||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求
|
||||
* 向微信端发送请求,在这里执行的策略是当发生access_token过期时才去刷新,然后重新执行请求,而不是全局定时请求.
|
||||
*/
|
||||
@Override
|
||||
public <T, E> T execute(RequestExecutor<T, E> executor, String uri, E data) throws WxErrorException {
|
||||
|
@ -10,6 +10,7 @@ import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||
import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder;
|
||||
import me.chanjar.weixin.cp.bean.messagebuilder.ImageBuilder;
|
||||
import me.chanjar.weixin.cp.bean.messagebuilder.MarkdownMsgBuilder;
|
||||
import me.chanjar.weixin.cp.bean.messagebuilder.MpnewsBuilder;
|
||||
import me.chanjar.weixin.cp.bean.messagebuilder.NewsBuilder;
|
||||
import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder;
|
||||
@ -94,6 +95,13 @@ public class WxCpMessage implements Serializable {
|
||||
return new MpnewsBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得markdown消息builder.
|
||||
*/
|
||||
public static MarkdownMsgBuilder MARKDOWN() {
|
||||
return new MarkdownMsgBuilder();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得文件消息builder.
|
||||
*/
|
||||
@ -112,6 +120,7 @@ public class WxCpMessage implements Serializable {
|
||||
* {@link WxConsts.KefuMsgType#VIDEO}
|
||||
* {@link WxConsts.KefuMsgType#NEWS}
|
||||
* {@link WxConsts.KefuMsgType#MPNEWS}
|
||||
* {@link WxConsts.KefuMsgType#MARKDOWN}
|
||||
* </pre>
|
||||
*
|
||||
* @param msgType 消息类型
|
||||
|
@ -0,0 +1,32 @@
|
||||
package me.chanjar.weixin.cp.bean.messagebuilder;
|
||||
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* markdown类型的消息builder
|
||||
* Created by Binary Wang on 2019/1/20.
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public class MarkdownMsgBuilder extends BaseBuilder<MarkdownMsgBuilder> {
|
||||
private String content;
|
||||
|
||||
public MarkdownMsgBuilder() {
|
||||
this.msgType = WxConsts.KefuMsgType.MARKDOWN;
|
||||
}
|
||||
|
||||
public MarkdownMsgBuilder content(String content) {
|
||||
this.content = content;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxCpMessage build() {
|
||||
WxCpMessage m = super.build();
|
||||
m.setContent(this.content);
|
||||
return m;
|
||||
}
|
||||
}
|
@ -8,14 +8,19 @@
|
||||
*/
|
||||
package me.chanjar.weixin.cp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
@ -37,12 +42,19 @@ public class WxCpMessageGsonAdapter implements JsonSerializer<WxCpMessage> {
|
||||
if (StringUtils.isNotBlank(message.getToTag())) {
|
||||
messageJson.addProperty("totag", message.getToTag());
|
||||
}
|
||||
|
||||
if (WxConsts.KefuMsgType.TEXT.equals(message.getMsgType())) {
|
||||
JsonObject text = new JsonObject();
|
||||
text.addProperty("content", message.getContent());
|
||||
messageJson.add("text", text);
|
||||
}
|
||||
|
||||
if (WxConsts.KefuMsgType.MARKDOWN.equals(message.getMsgType())) {
|
||||
JsonObject text = new JsonObject();
|
||||
text.addProperty("content", message.getContent());
|
||||
messageJson.add("markdown", text);
|
||||
}
|
||||
|
||||
if (WxConsts.KefuMsgType.TEXTCARD.equals(message.getMsgType())) {
|
||||
JsonObject text = new JsonObject();
|
||||
text.addProperty("title", message.getTitle());
|
||||
|
@ -1,11 +1,12 @@
|
||||
package me.chanjar.weixin.cp.api;
|
||||
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessage;
|
||||
import me.chanjar.weixin.cp.bean.WxCpMessageSendResult;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
@ -14,7 +15,7 @@ import static org.testng.Assert.*;
|
||||
* @author Daniel Qian
|
||||
*
|
||||
*/
|
||||
@Test(groups = "customMessageAPI")
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpMessageAPITest {
|
||||
|
||||
@ -59,4 +60,32 @@ public class WxCpMessageAPITest {
|
||||
System.out.println(messageSendResult.getInvalidUserList());
|
||||
System.out.println(messageSendResult.getInvalidTagList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSendMessage_markdown() throws WxErrorException {
|
||||
WxCpMessage message = WxCpMessage
|
||||
.MARKDOWN()
|
||||
.toUser(configStorage.getUserId())
|
||||
.content("您的会议室已经预定,稍后会同步到`邮箱` \n" +
|
||||
" >**事项详情** \n" +
|
||||
" >事 项:<font color=\\\"info\\\">开会</font> \n" +
|
||||
" >组织者:@miglioguan \n" +
|
||||
" >参与者:@miglioguan、@kunliu、@jamdeezhou、@kanexiong、@kisonwang \n" +
|
||||
" > \n" +
|
||||
" >会议室:<font color=\\\"info\\\">广州TIT 1楼 301</font> \n" +
|
||||
" >日 期:<font color=\\\"warning\\\">2018年5月18日</font> \n" +
|
||||
" >时 间:<font color=\\\"comment\\\">上午9:00-11:00</font> \n" +
|
||||
" > \n" +
|
||||
" >请准时参加会议。 \n" +
|
||||
" > \n" +
|
||||
" >如需修改会议信息,请点击:[修改会议信息](https://work.weixin.qq.com)")
|
||||
.build();
|
||||
|
||||
WxCpMessageSendResult messageSendResult = this.wxService.messageSend(message);
|
||||
assertNotNull(messageSendResult);
|
||||
System.out.println(messageSendResult);
|
||||
System.out.println(messageSendResult.getInvalidPartyList());
|
||||
System.out.println(messageSendResult.getInvalidUserList());
|
||||
System.out.println(messageSendResult.getInvalidTagList());
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,10 @@
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.3.2.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-miniapp</artifactId>
|
||||
<name>WxJava - MiniApp</name>
|
||||
<description>微信小程序Java SDK</description>
|
||||
<name>WxJava - MiniApp Java SDK</name>
|
||||
<description>微信小程序 Java SDK</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
|
@ -9,8 +9,9 @@
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.3.2.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-mp</artifactId>
|
||||
<name>WxJava - MP</name>
|
||||
<name>WxJava - MP Java SDK</name>
|
||||
<description>微信公众号Java SDK</description>
|
||||
|
||||
<dependencies>
|
||||
|
@ -3,16 +3,17 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java</artifactId>
|
||||
<version>3.3.2.B</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>weixin-java-open</artifactId>
|
||||
<name>WxJava - Open</name>
|
||||
<description>微信开放平台Java SDK</description>
|
||||
<name>WxJava - Open Java SDK</name>
|
||||
<description>微信开放平台 Java SDK</description>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>007</name>
|
||||
|
@ -10,7 +10,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>weixin-java-pay</artifactId>
|
||||
<name>WxJava - PAY</name>
|
||||
<name>WxJava - PAY Java SDK</name>
|
||||
<description>微信支付 Java SDK</description>
|
||||
|
||||
<dependencies>
|
||||
|
Loading…
Reference in New Issue
Block a user