原本开发的企业微信的功能,但是有客户使用微信企业号,其他的99%的API都一样,就是主动发送消息有些不通:
微信企业号的消息格式为:
- text消息
- image消息
- voice消息
- video消息
- file消息
- news消息
- mpnews消息
企业微信的消息格式:
- 文本消息
- 图片消息
- 语音消息
- 视频消息
- 文件消息
- 文本卡片消息
- 图文消息
- 图文消息(mpnews)
- 小程序通知消息
原本在企业微信里面给用户发送通知时是发送textcard类型,但是微信企业号里面没有这个的类型,最终同一发送news类型。详细代码:
- 消息类型封装
/**
* 企业号发送消息采用的自定义模板
*/
public class CorpMsgTemplate {
private String touser;
private String toparty;
private String totag;
private String msgtype;
private String agentid;
public String getMsgtype() {
return msgtype;
}
public void setMsgtype(String msgtype) {
this.msgtype = msgtype;
}
public String getTouser() {
return touser;
}
public void setTouser(String touser) {
this.touser = touser;
}
public String getToparty() {
return toparty;
}
public void setToparty(String toparty) {
this.toparty = toparty;
}
public String getTotag() {
return totag;
}
public void setTotag(String totag) {
this.totag = totag;
}
public String getAgentid() {
return agentid;
}
public void setAgentid(String agentid) {
this.agentid = agentid;
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
}
}
/**
* @Description: news类型消息格式封装
*/
public class CorpNewsTemplate extends CorpMsgTemplate {
private Map<String,Textcard[]> news;
public static final String ARTICLES = "articles";
private String msgtype = "news";
public CorpNewsTemplate() {
super();
super.setMsgtype(msgtype);
news = new HashMap<>();
news.put(ARTICLES,null);
}
public Map<String, Textcard[]> getNews() {
return news;
}
public void setNews(Map<String, Textcard[]> news) {
this.news = news;
}
}
/**
* @Description: 文字卡片类型封装
*/
public class CorpTextCardTemplate extends CorpMsgTemplate {
private Textcard textcard;
private String msgtype = "textcard";
public CorpTextCardTemplate() {
super();
super.setMsgtype(msgtype);
this.textcard = new Textcard();
}
public Textcard getTextcard() {
return textcard;
}
public void setTextcard(Textcard textcard) {
this.textcard = textcard;
}
}
public class Textcard {
private String title;
private String description;
private String url;
private String btntxt;
/**
* 图文消息类型专用属性
*/
private String picurl;
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getBtntxt() {
return btntxt;
}
public Textcard() {
}
public void setBtntxt(String btntxt) {
this.btntxt = btntxt;
}
@Override
public String toString() {
return "Textcard{" +
"title='" + title + '\'' +
", description='" + description + '\'' +
", url='" + url + '\'' +
", btntxt='" + btntxt + '\'' +
", picurl='" + picurl + '\'' +
'}';
}
}
- 构建news消息json字符串参数
// 构建news消息json字符串
public String createNewsMsgByTemplate(TblNoticeMessageInfo msg, String userId, OAuth2ParamsBO paramsBO , FlowBaseObject baseObject){
CorpNewsTemplate template = new CorpNewsTemplate();
template.setTouser(userId);
template.setAgentid(paramsBO.getAgentId() + "");
Textcard textcard = new Textcard();
textcard.setTitle(msg.getMsgTitle());
textcard.setDescription(msg.getMsgContent());
textcard.setBtntxt("详情");
textcard.setUrl(wechatConfig.getEventDetailsUrl().trim() + "?id=" + msg.getObjectId());
template.getNews().put(CorpNewsTemplate.ARTICLES,new Textcard[]{textcard});
return JSON.toJSONString(template);
}