JAVA微信公众号开发之客服消息

微信公众号客服消息
1、用户发送信息
2、点击自定义菜单(仅有点击推事件、扫码推事件、扫码推事件且弹出“消息接收中”提示框这3种菜单类型是会触发客服接口的)
3、关注公众号
4、扫描二维码
5、支付成功
6、用户维权
 

接口调用请求说明

http请求方式: POST
https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

各消息类型所需的JSON数据包如下:

发送文本消息

{
    "touser":"OPENID",
    "msgtype":"text",
    "text":
    {
         "content":"Hello World"
    }
}

发送图片消息

{
    "touser":"OPENID",
    "msgtype":"image",
    "image":
    {
      "media_id":"MEDIA_ID"
    }
}

发送语音消息

{
    "touser":"OPENID",
    "msgtype":"voice",
    "voice":
    {
      "media_id":"MEDIA_ID"
    }
}

发送视频消息

{
    "touser":"OPENID",
    "msgtype":"video",
    "video":
    {
      "media_id":"MEDIA_ID",
      "thumb_media_id":"MEDIA_ID",
      "title":"TITLE",
      "description":"DESCRIPTION"
    }
}

发送音乐消息

{
    "touser":"OPENID",
    "msgtype":"music",
    "music":
    {
      "title":"MUSIC_TITLE",
      "description":"MUSIC_DESCRIPTION",
      "musicurl":"MUSIC_URL",
      "hqmusicurl":"HQ_MUSIC_URL",
      "thumb_media_id":"THUMB_MEDIA_ID" 
    }
}

发送图文消息(点击跳转到外链) 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。

{
    "touser":"OPENID",
    "msgtype":"news",
    "news":{
        "articles": [
         {
             "title":"Happy Day",
             "description":"Is Really A Happy Day",
             "url":"URL",
             "picurl":"PIC_URL"
         },
         {
             "title":"Happy Day",
             "description":"Is Really A Happy Day",
             "url":"URL",
             "picurl":"PIC_URL"
         }
         ]
    }
}

发送图文消息(点击跳转到图文消息页面) 图文消息条数限制在8条以内,注意,如果图文数超过8,则将会无响应。

{
    "touser":"OPENID",
    "msgtype":"mpnews",
    "mpnews":
    {
         "media_id":"MEDIA_ID"
    }
}

发送卡券

{
  "touser":"OPENID", 
  "msgtype":"wxcard",
  "wxcard":{              
           "card_id":"123dsdajkasd231jhksad"        
            },
}
参数 是否必须 说明
button 一级菜单数组,个数应为1~3个
sub_button 二级菜单数组,个数应为1~5个
type 菜单的响应动作类型,view表示网页类型,click表示点击类型,miniprogram表示小程序类型
name 菜单标题,不超过16个字节,子菜单不超过60个字节
key click等点击类型必须 菜单KEY值,用于消息接口推送,不超过128字节
url view、miniprogram类型必须 网页链接,用户点击菜单可打开链接,不超过1024字节。type为miniprogram时,不支持小程序的老版本客户端将打开本url。
media_id media_id类型和view_limited类型必须 调用新增永久素材接口返回的合法media_id
appid miniprogram类型必须 小程序的appid(仅认证公众号可配置)

pagepath

miniprogram类型必须 小程序的页面路径

创建一个客服实体类,然后组装成json字符串发给微信服务器

 
public class Custom { private String touser; private String msgtype; private Text text; public String getTouser() { return touser; } public void setTouser(String touser) { this.touser = touser; } public String getMsgtype() { return msgtype; } public void setMsgtype(String msgtype) { this.msgtype = msgtype; } public Text getText() { return text; } public void setText(Text text) { this.text = text; } }
public static String createCustom(String openId,String content,String accessToken){
		Custom custom = new Custom();
		Text text = new Text();
		text.setContent(content);
		custom.setMsgtype(MESSAGE_TEXT);
		custom.setTouser(openId);
		custom.setText(text);
		String url = CUSTOM.replace("ACCESS_TOKEN", accessToken);//获取accesstoken在地图那篇文章里有介绍
		JSONObject jsonObject = SignUtil.doPostStr(url, JSONObject.fromObject(custom).toString());
	    System.out.println(jsonObject);
		return jsonObject.toString();
	}
/**
	 * post请求
	 */
	public static JSONObject doPostStr(String url,String outStr){

		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		JSONObject jsonObject = null;
		String result="";
		try {
			httpPost.setEntity(new StringEntity(outStr,"utf-8"));
			HttpResponse response = httpClient.execute(httpPost);
			result = EntityUtils.toString(response.getEntity(),"utf-8");
		} catch (Exception e) {
		} 
		jsonObject = JSONObject.fromObject(result);
		return jsonObject;
	}	


所有事件都是返回到在公众号里服务器配置的回调接口方法,需重写httpservlet的doPost方法

 
public void doPost(HttpServletRequest req,HttpServletResponse resp) throws IOException { req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); String serverPath=""; PrintWriter out = resp.getWriter(); //String openId = GetCookie.getCookie(req,resp); Map map; try { map = MessageUtil.xmlToMap(req);//微信以xml形式转过来需要转化,下面给方法 String fromUserName = map.get("FromUserName");//来自谁的消息 System.out.println(fromUserName); String toUserName = map.get("ToUserName");//微信公众号 String msgType = map.get("MsgType");//消息类型,下面有介绍类型 //String content = map.get("Content");//内容 String message = ""; if(MessageUtil.MESSAGE_EVENT.equals(msgType)){ System.out.println("事件"); String eventType = map.get("Event"); System.out.println(eventType); String eventKey = map.get("EventKey"); System.out.println(map.get("EventKey")); if(MessageUtil.MESSAGE_CLICK.equals(eventType)){ String key = map.get("EventKey"); String accessToken = "",//获取accessToken if(key.equals("2")){ String content = "内容";      MessageUtil.createCustom(fromUserName, content, accessToken); }else{ return; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }      MessageUtil.createCustom(fromUserName, content, accessToken); }else{ return; } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } 

最近在整理一些资源工具,放在网站分享 http://tools.maqway.com
欢迎关注公众号:麻雀唯伊 , 不定时更新资源文章,生活优惠,或许有你想看的

JAVA微信公众号开发之客服消息_第1张图片

 

 

你可能感兴趣的:(java,微信公众号开发)