java微信发送消息_微信企业号发送消息接口-Java

刚写完一遍日志,再接再厉,把发送消息接口也整了,这个消息接口的不同于公众号的,消息类型有很多种,整个类写到了大概200行左右代码吧,用到了比较多的 正则表达式,json封装,网络请求等

企业可以主动发消息给员工,消息量不受限制。

调用接口时,使用Https协议、JSON数据包格式,数据包不需做加密处理。

目前支持文本、图片、语音、视频、文件、图文等消息类型。除了news类型,其它类型的消息可在发送时加上保密选项,保密消息会被打上水印,并且只有接收者才能阅读。

目前有7种消息类型:text\image\voice\video\file\news\mpnews

统一的Post接口地址:https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN

发送消息  SMessage:

package jsp.weixin.msg.Util;

import java.util.ArrayList;

import java.util.List;

import net.sf.json.JSONArray;

import jsp.weixin.ParamesAPI.util.ParamesAPI;

import jsp.weixin.ParamesAPI.util.WeixinUtil;

import jsp.weixin.msg.Resp.Article;

/**

* 发送消息类

* @author Engineer.Jsp

* @date 2014.10.11

*/

public class SMessage {

//发送接口

public static String POST_URL = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=ACCESS_TOKEN";

/**

* text消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:text

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param content 消息内容

* @param safe 表示是否是保密消息,0表示否,1表示是,默认0

* */

public static String STextMsg(String touser,String toparty,String totag,String agentid,String content){

String PostData = "{\"touser\": %s,\"toparty\": %s,\"totag\": %s,\"msgtype\": \"text\",\"agentid\": %s,\"text\": {\"content\": %s},\"safe\":\"0\"}";

return String.format(PostData, touser,toparty,totag,agentid,content);

}

/**

* image消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:image

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param media_id 媒体资源文件ID

* @param safe 表示是否是保密消息,0表示否,1表示是,默认0

* */

public static String SImageMsg(String touser,String toparty,String agentid ,String media_id){

String PostData = "{\"touser\": %s,\"toparty\": %s,\"msgtype\": \"image\",\"agentid\": %s,\"image\": {\"media_id\": %s},\"safe\":\"0\"}";

return String.format(PostData, touser,toparty,agentid,media_id);

}

/**

* voice消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:voice

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param media_id 媒体资源文件ID

* @param safe 表示是否是保密消息,0表示否,1表示是,默认0

* */

public static String SVoiceMsg(String touser,String toparty,String totag,String agentid ,String media_id){

String PostData = "{\"touser\": %s,\"toparty\": %s,\"totag\": %s,\"msgtype\": \"voice\",\"agentid\": %s,\"voice\": {\"media_id\": %s},\"safe\":\"0\"}";

return String.format(PostData, touser,toparty,totag,agentid,media_id);

}

/**

* video消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:video

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param media_id 媒体资源文件ID

* @param title 视频消息的标题

* @param description 视频消息的描述

* @param safe 表示是否是保密消息,0表示否,1表示是,默认0

*/

public static String SVideoMsg(String touser,String toparty,String totag,String agentid,String media_id,String title,String description){

String PostData = "{\"touser\": %s,\"toparty\": %s,\"totag\": %s,\"msgtype\": \"video\",\"agentid\": %s,\" video\": {\"media_id\": %s,\"title\": %s,\"description\": %s},\"safe\":\"0\"}";

return String.format(PostData, touser,toparty,totag,agentid,media_id,title,description);

}

/**

* file消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:file

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param media_id 媒体资源文件ID

* @param safe 表示是否是保密消息,0表示否,1表示是,默认0

* */

public static String SFileMsg(String touser,String toparty,String totag,String agentid ,String media_id){

String PostData = "{\"touser\": %s,\"toparty\": %s,\"totag\": %s,\"msgtype\": \"file\",\"agentid\": %s,\"file\": {\"media_id\": %s},\"safe\":\"0\"}";

return String.format(PostData, touser,toparty,totag,agentid,media_id);

}

/**

* news消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:news

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param articlesList 图文集合

*/

public static String SNewsMsg(String touser,String toparty,String totag,String agentid , String articlesList){

String postData = "{\"touser\": %s,\"toparty\": %s,\"totag\": %s,\"msgtype\": \"news\",\"agentid\": %s,\"news\": {\"articles\":%s}}";

return String.format(postData, touser,toparty,totag,agentid,articlesList);

}

/**

* mpnews消息

* @param touser UserID列表(消息接收者,多个接收者用‘|’分隔)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送————"touser": "UserID1|UserID2|UserID3"

* @param toparty PartyID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"toparty": " PartyID1 | PartyID2 "

* @param totag TagID列表,多个接受者用‘|’分隔。当touser为@all时忽略本参数————"totag": " TagID1 | TagID2 "

* @param msgtype 消息类型,此时固定为:mpnews

* @param agentid 企业应用的id,整型。可在应用的设置页面查看

* @param articlesList mpnews集合

*/

public static String SMpNewsMsg(String touser,String toparty,String totag,String agentid , String articlesList){

String postData = "{\"touser\": %s,\"toparty\": %s,\"totag\": %s,\"msgtype\": \"mpnews\",\"agentid\": %s,\"mpnews\": {\"articles\":%s}\"safe\":\"0\"}";

return String.format(postData, touser,toparty,totag,agentid,articlesList);

}

//示例

public static void main(String[] args) {

/**

* news示例

* */

// 调取凭证

String access_token = WeixinUtil.getAccessToken(ParamesAPI.corpId, ParamesAPI.secret).getToken();

// 新建图文

Article article1 = new Article();

article1.setTitle("news消息测试-1");

article1.setDescription("");

article1.setPicUrl("http://112.124.111.3/weixinClient/images/weather3.png");

article1.setUrl("http://112.124.111.3/weixinClient/images/weather3.png");

Article article2 = new Article();

article2.setTitle("news消息测试-2");

article2.setDescription("");

article2.setPicUrl("http://112.124.111.3/weixinClient/images/weather3.png");

article2.setUrl("http://112.124.111.3/weixinClient/images/weather3.png");

// 整合图文

List list = new ArrayList();

list.add(article1);

list.add(article2);

// 图文转json

String articlesList = JSONArray.fromObject(list).toString();

// Post的数据

String PostData = SNewsMsg("UserID1|UserID2|UserID3", "PartyID1 | PartyID2", "TagID1 | TagID2", "1", articlesList);

int result = WeixinUtil.PostMessage(access_token, "POST", POST_URL, PostData);

// 打印结果

if(0==result){

System.out.println("操作成功");

}

else {

System.out.println("操作失败");

}

}

}

上一篇说到的,项目需要用到的jar如下:

目前项目结构图:

5ea1981e71ae5b9714874eed7a2625df.png

9648c663ec89eb38fa13287fc15f9537.png

权限要求

需要管理员对应用有使用权限,对收件人touser、toparty、totag有查看权限,否则本次调用失败。

返回结果

如果对应用或收件人、部门、标签任何一个无权限,则本次发送失败;如果收件人、部门或标签不存在,发送仍然执行,但返回无效的部分。

{

"errcode": 0,

"errmsg": "ok",

"invaliduser": "UserID1",

"invalidparty":"PartyID1",

"invalidtag":"TagID1"

}

本地测试图:

608f74fe2e763c360e98147e6504d4ff.png

微信端结果图:

f7ee7855a3d7793fcb31ecc08c588766.png

有疑问的可以再次留言,看到第一时间回复,欢迎大伙儿留言和转载~ 谢谢!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值