微信公众号发送消息之发送客服消息基类封装

时间:2025-03-18 08:00:53

微信公众号发送消息之发送客服消息基类封装

当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。

http请求方式: POST
/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN

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


package ;

import ;

import ;
import ;

import ;
import ;

/**
 * 发送客服消息基类
 * 
 * @author sunlight
 *
 */
public class CustomMessage {
    /**
     * 文本客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param content
     *            文本消息内容
     * @return
     */
    public static String TextMsg(String openId, String content) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joContent = new JSONObject();
        ("content", content);
        ("text", joContent);
        return ();
    }

    /**
     * 图片客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param mediaId
     *            媒体文件id
     * @return
     */
    public static String ImageMsg(String openId, String mediaId) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        ("media_id", mediaId);
        ("image", joMedia);
        return ();
    }

    /**
     * 语音客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param mediaId
     *            媒体文件id
     * @return
     */
    public static String VoiceMsg(String openId, String mediaId) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        ("media_id", mediaId);
        ("voice", joMedia);
        return ();
    }

    /**
     * 
     * 视频客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param mediaId
     *            媒体文件id
     * @param thumb_media_id
     *            缩略图的媒体ID
     * @param title
     *            视频消息的标题(非必须)
     * @param description
     *            视频消息的描述(非必须)
     * @return
     */
    public static String VideoMsg(String openId, String mediaId,
            String thumb_media_id, String title, String description) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        ("media_id", mediaId);
        ("thumb_media_id", thumb_media_id);
        ("title", title);
        ("description", description);
        ("video", joMedia);
        return ();
    }

    /**
     * 
     * 视频客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param mediaId
     *            媒体文件id
     * @param thumb_media_id
     *            缩略图的媒体ID
     * @return
     */
    public static String VideoMsg(String openId, String mediaId,
            String thumb_media_id) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        ("media_id", mediaId);
        ("thumb_media_id", thumb_media_id);
        ("title", "");
        ("description", "");
        ("video", joMedia);
        return ();
    }

    /**
     * 
     * 音乐客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param mediaId
     *            媒体文件id
     * @param thumb_media_id
     *            缩略图的媒体ID
     * @param title
     *            音乐消息的标题(非必须)
     * @param description
     *            音乐消息的描述(非必须)
     * @param musicurl
     *            音乐链接
     * @param hqmusicurl
     *            高品质音乐链接,wifi环境优先使用该链接播放音乐
     * @param thumb_media_id
     *            缩略图的媒体ID
     * @return
     */
    public static String MusicMsg(String openId, String title,
            String description, String musicurl, String hqmusicurl,
            String thumb_media_id) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        ("title", title);
        ("description", description);
        ("musicurl", musicurl);
        ("hqmusicurl", hqmusicurl);
        ("thumb_media_id", thumb_media_id);
        ("music", joMedia);
        return ();
    }

    /**
     * 
     * 音乐客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param mediaId
     *            媒体文件id
     * @param thumb_media_id
     *            缩略图的媒体ID
     * @param musicurl
     *            音乐链接
     * @param hqmusicurl
     *            高品质音乐链接,wifi环境优先使用该链接播放音乐
     * @param thumb_media_id
     *            缩略图的媒体ID
     * @return
     */
    public static String MusicMsg(String openId, String musicurl,
            String hqmusicurl, String thumb_media_id) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        ("title", "");
        ("description", "");
        ("musicurl", musicurl);
        ("hqmusicurl", hqmusicurl);
        ("thumb_media_id", thumb_media_id);
        ("music", joMedia);
        return ();
    }

    /**
     * 发送图文客服消息
     * 
     * @param openId
     *            消息接收者openId
     * @param articles
     *            图文消息列表
     * @return
     */
    public static String NewsMsg(String openId, List<Article> articles) {
        JSONObject jo = new JSONObject();
        ("touser", openId);
        ("msgtype", ());
        JSONObject joMedia = new JSONObject();
        JSONArray ja = new JSONArray();
        for (Article article : articles) {
            JSONObject joChild = new JSONObject();
            ("title", ());
            ("description", ());
            ("url", ());
            ("picurl", ());
            (joChild);
        }
        ("articles", ja);
        ("news", joMedia);
        return ();
    }
}


个人笔记~~