微信企业号和企业微信消息发送区别

原本开发的企业微信的功能,但是有客户使用微信企业号,其他的99%的API都一样,就是主动发送消息有些不通:
微信企业号的消息格式为:

  1. text消息
  2. image消息
  3. voice消息
  4. video消息
  5. file消息
  6. news消息
  7. mpnews消息

企业微信的消息格式:

  1. 文本消息
  2. 图片消息
  3. 语音消息
  4. 视频消息
  5. 文件消息
  6. 文本卡片消息
  7. 图文消息
  8. 图文消息(mpnews)
  9. 小程序通知消息

原本在企业微信里面给用户发送通知时是发送textcard类型,但是微信企业号里面没有这个的类型,最终同一发送news类型。详细代码:

  1. 消息类型封装
/**
 * 企业号发送消息采用的自定义模板
 */
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 + '\'' +
                '}';
    }
}
  1. 构建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);
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值