微信公眾平台開發(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;
          
          
    } 

實現效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM