zoukankan      html  css  js  c++  java
  • 微信客服接口发消息 -- 微信客服系列文章(一)

    • 概述

      本文主要介绍调用微信客服接口的简单实现,帮助有需要的同学快速开发,需要注意的是目前微信限制客服接口的日调用频率为500000次。除此之外,客服接口的调用微信也规定了一些前置条件,只有满足这些条件中的任何一个,客服接口才能够成功调用。

      目前允许的动作列表如下(公众平台会根据运营情况更新该列表,不同动作触发后,允许的客服接口下发消息条数不同,下发条数达到上限后,会遇到错误返回码,具体请见返回码说明页):  

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

    微信客服接口详情,请查阅微信公众号开发文档

    • 消息类型工具类
    /**
     * 微信消息类型
     */
    public enum MsgTypeEnum implements ICodeEnum {
        TEXT("text"),           //文本消息
        IMAGE("image"),         //图片消息
        VOICE("voice"),         //语音消息
        VIDEO("video"),         //视频消息
        MUSIC("music"),         //音乐消息
        NEWS("news"),           //图文消息(点击跳转到外链)
        MPNEWS("mpnews"),       //图文消息(点击跳转到图文消息页面)
        WXCARD("wxcard"),;      //卡券
    
        private String name;
        
        private MsgTypeEnum(String name){
            this.name = name;
        }
        
        @Override
        public String toCode() {
            return Integer.toString(this.ordinal());
        }
    
        public String toName() {
            return this.name;
        }
    
        public static MsgTypeEnum fromCode(String code) {
            try {
                return values()[Integer.parseInt(code)];
            } catch (Exception e) {
                return null;
            }
        }
    }
    • 基础模板
    /**
     * Created by Administrator on 2017/3/20.
     */
    public class CustomerServiceBaseMessageVo {
        private String touser;
    
        private String msgtype;
    
        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 class TextContentVo {
        private String content;
    
        public String getContent() {
            return content;
        }
    
        public void setContent(String content) {
            this.content = content;
        }
    }
    • 图文消息模板
    /**
     * 图文消息(点击跳转到外链)
     */
    public class ArticleVo {
        private String title;
        
        private String description;
        
        private String url;
        
        private String 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 getPicurl() {
            return picurl;
        }
    
        public void setPicurl(String picurl) {
            this.picurl = picurl;
        }
    }
    import java.util.List;
    
    /**
     * Created by Administrator on 2017/3/20.
     */
    public class NewsVo {
        private List<ArticleVo> articles;
    
        public List<ArticleVo> getArticles() {
            return articles;
        }
    
        public void setArticles(List<ArticleVo> articles) {
            this.articles = articles;
        }
    }
    /**
     * 客服接口图文消息模板
     */
    public class CustomerServiceNewsMessageVo extends CustomerServiceBaseMessageVo {
        private NewsVo news;
    
        public NewsVo getNews() {
            return news;
        }
    
        public void setNews(NewsVo news) {
            this.news = news;
        }
    }
    • 消息推送工具类
    import org.apache.log4j.Logger;
    
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * 微信客服消息工具类
     */
    public class WxCustomerServiceMessageUtil {
        private static final Logger logger = Logger.getLogger(WxCustomerServiceMessageUtil.class);
        private static final String REQUEST_URL = "https://api.weixin.qq.com/cgi-bin/message/custom/send";
    
        /**
         * 推送客服图文消息
         * @param touser 图文消息接受对象 opendId
         * @param title 图文消息标题
         * @param description 图文消息描述
         * @param url 图文消息跳转链接
         * @param picurl 图文消息封面图片
         * @return
         */
        public static String sendNewsMessage(String touser, String title, String description, String url, String picurl){
            try {
                if(StringUtils.isBlank(touser) || StringUtils.isBlank(title) || StringUtils.isBlank(description)){
                    logger.error("推送客服图文消息异常,touser:" + touser + ",title:" + title  + ",description:" + description);
                    return null;
                }else{
                    String accessToken = ServiceManager.weixinApiService.getAccessToken();
                    if(StringUtils.isBlank(accessToken)){
                        logger.error("accessToken:" + accessToken);
                        throw new BusinessException(ResGlobal.ERRORS_USER_DEFINED, new String[]{"accessToken为空!"});
                    }else{
                        Map<String, String> paramMap = new HashMap<String, String>();
                        paramMap.put("access_token", accessToken);
    
                        ArticleVo articleVo = new ArticleVo();
                        articleVo.setDescription(description);
                        articleVo.setTitle(title);
                        if(StringUtils.isNotBlank(url)){
                            articleVo.setUrl(url);
                        }
                        if(StringUtils.isNotBlank(picurl)){
                            articleVo.setPicurl(picurl);
                        }
                        List<ArticleVo> articles = new ArrayList<ArticleVo>();
                        articles.add(articleVo);
    
                        NewsVo newsVo = new NewsVo();
                        newsVo.setArticles(articles);
    
                        CustomerServiceNewsMessageVo customerServiceNewsMessageVo = new CustomerServiceNewsMessageVo();
                        customerServiceNewsMessageVo.setNews(newsVo);
                        customerServiceNewsMessageVo.setMsgtype(MsgTypeEnum.NEWS.toName());
                        customerServiceNewsMessageVo.setTouser(touser);
                        String jsonParam = JsonBinder.buildNonNullBinder().toJson(customerServiceNewsMessageVo);
                        String resultStr = WeixinWebUtil.doPost(REQUEST_URL, paramMap, jsonParam, "UTF-8", 3000, 3000);
                        logger.debug("推送客服图文消息结果:" + resultStr);
                        return resultStr;
                    }
                }
            }catch (IOException e) {
                logger.error("推送客服图文消息失败");
                throw new BusinessException(ResGlobal.ERRORS_USER_DEFINED, new String[]{e.getMessage()});
            }
        }
    
        /**
         * 推送客服文本消息
         * @param msg 文本消息内容
         * @param touser 文本消息接受对象 opendId
         * @return
         */
        public static String sendTextMessage(String msg, String touser) {
            try {
                if(StringUtils.isBlank(msg) || StringUtils.isBlank(touser)){
                    logger.error("推送客服文本消息异常,msg:" + msg + ",touser:" + touser);
                    return null;
                }else{
                    String accessToken = ServiceManager.weixinApiService.getAccessToken();
                    if(StringUtils.isBlank(accessToken)){
                        logger.error("accessToken:" + accessToken);
                        throw new BusinessException(ResGlobal.ERRORS_USER_DEFINED, new String[]{"accessToken为空!"});
                    }else{
                        Map<String, String> paramMap = new HashMap<String, String>();
                        paramMap.put("access_token", accessToken);
    
                        TextContentVo textContentVo = new TextContentVo();
                        textContentVo.setContent(msg);
                        CustomerServiceTextMessageVo customerServiceTextMessageVo = new CustomerServiceTextMessageVo();
                        customerServiceTextMessageVo.setMsgtype(MsgTypeEnum.TEXT.toName());
                        customerServiceTextMessageVo.setText(textContentVo);
                        customerServiceTextMessageVo.setTouser(touser);
                        String jsonParam = JsonBinder.buildNonNullBinder().toJson(customerServiceTextMessageVo);
                        String resultStr = WeixinWebUtil.doPost(REQUEST_URL, paramMap, jsonParam, "UTF-8", 3000, 3000);
                        logger.debug("推送客服文本消息结果:" + resultStr);
                        return resultStr;
                    }
                }
            }catch (IOException e) {
                logger.error("推送客服文本消息失败");
                throw new BusinessException(ResGlobal.ERRORS_USER_DEFINED, new String[]{e.getMessage()});
            }
        }
    }

    欢迎转载,转载必须标明出处

  • 相关阅读:
    【代码笔记】Web-HTML-布局
    【代码笔记】Web-HTML-列表
    【代码笔记】Web-HTML-表格
    【代码笔记】Web-HTML-图像
    【代码笔记】Web-HTML-CSS
    【代码笔记】Web-HTML-头部
    【代码笔记】Web-HTML-标题
    优先解决关键路径上的关键问题!
    【转】svn:is not under version control and is not part of the commit, yet its child解决办法
    回忆:NVelocity--基于.NET的模板引擎
  • 原文地址:https://www.cnblogs.com/rexfang/p/6593821.html
Copyright © 2011-2022 走看看