zoukankan      html  css  js  c++  java
  • 微信公众平台开发(5) 微信客服消息接口

    API地址:http://mp.weixin.qq.com/wiki/7/12a5a320ae96fecdf0e15cb06123de9f.html

    发送消息请求接口地址:https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

    微信公众号向用户发送消息,根据类型可以分为文本消息、图片消息、语言消息、视频消息、音乐消息、图文消息。这里已文本消息为例,发送其他类型消息,需先将消息文件上传至公众号。

    创建一个base实体:CustomMsg,给CustomMsg类一个空的toJSON方法。具体消息实体继承CustomMsg即可。

    //所有消息类型父类
    public class CustomMsg {
        private String touser;
        private MsgTypeEnum msgtype;//消息类型
        
        //省略getter、setter方法
        
        public String toJSON(){
            return "";
        }
    }
    
    //消息类型枚举
    public enum MsgTypeEnum {
        TEXT("文本","green","text"),
        IMAGE("图片","green","image"),
        VOICE("语言","green","voice"),
        VIDEO("视频","green","video"),
        MUSIC("音乐","green","music"),
        NEWS("图文","green","news"),
        WXCARD("卡券","green","wxcard");
        
        private String label;
        private String color;
        private String value;
        
        private MsgTypeEnum(String label,String color,String value) {
            this.setLabel(label);
            this.setColor(color);
            this.setValue(value);
        }
        
        //省略getter、setter方法
    }

    文本消息实体继承CustomMsg,重写toJSON方法

    public class TextMsg extends CustomMsg{
        private String content;
        
        //指定消息类型
        public TextMsg(){
            this.setMsgtype(MsgTypeEnum.TEXT);
        }
        
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
        
        //重写父类方法
        public String toJSON() {  
            StringBuffer buffer = new StringBuffer();  
            buffer.append("{");  
            buffer.append(String.format(""touser":"%s"", this.getTouser())).append(",");  
            buffer.append(String.format(""msgtype":"%s"", this.getMsgtype().getValue())).append(",");  
            buffer.append(String.format(""%s":{",this.getMsgtype().getValue()));  
            
            buffer.append(String.format(""content":"%s"", this.content));
            
            buffer.append("}");  
            buffer.append("}");  
            return buffer.toString();  
        }  
    }

    发送消息,httpsRequestToJsonObject在前面的工具类中

    TextMsg textMsg = new TextMsg();
            String content = "hello! custom message!";
            textMsg.setContent(content);
            textMsg.setTouser("oRKn_jnBnfKC7bqU6YBPb-3FS8DE");
            JSONObject jsonObject = customMessageService.sendCustomMsg(textMsg);
        public  JSONObject  sendCustomMsg(CustomMsg customMsg){  
            
            //获取token
            String token = accessTokenService.getAccessToken();
              
            String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
            
            requestUrl=requestUrl.replace("ACCESS_TOKEN", token);  
            
            if (log.isDebugEnabled()) {
                log.debug(customMsg.toJSON());
            }
            
            JSONObject jsonObject = CommonUtil.httpsRequestToJsonObject(requestUrl, "POST", customMsg.toJSON(),false);  
            
            return jsonObject;
              
              
        } 

    实现效果:

  • 相关阅读:
    毕业五年后的差距
    基于jsonlib.jar包Json程序 实战篇
    Hashtable
    [转]浏览器是怎样工作的:渲染引擎,HTML解析
    Web.config之连接字介绍
    jQuery选择器全解【转】
    JavaScript document属性和方法
    zoj 2316 Matrix Multiplication 夜
    zoj 2318 Get Out! 夜
    hdu 3666 THE MATRIX PROBLEM 夜
  • 原文地址:https://www.cnblogs.com/zhangxianming/p/5777017.html
Copyright © 2011-2022 走看看