java微信客服消息接口

原文链接:http://115.159.110.224/sys/myhref_viewThis.do?f_gid=C87024928E09415BA5AA3D7FA69D91B8&f_type=ly

2015-11-06 / 分类:微信开发

微信客服消息

微信客服消息:


package com.dm.wx.util;

import com.dm.wx.domain.respmessage.Article;
import com.dm.wx.domain.respmessage.Music;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class AdvancedUtil {
    private static Logger log = LoggerFactory.getLogger(AdvancedUtil.class);

    /**
     * 组装发送文本消息
     *
     * @return
     */
    public static String makeTextCustomMessage(String openId, String content) {
        content = content.replace("\"", "\\\"");
        String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"text\",\"text\":{\"content\":\"%s\"}}";
        return String.format(jsonMsg, openId, content);
    }

    /**
     * 组装发送图片消息
     *
     * @return
     */
    public static String makeImageCustomMessage(String openId, String mediaId) {
        String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"%s\"}}";
        return String.format(jsonMsg, openId, mediaId);
    }

    /**
     * 组装发送语音消息
     *
     * @return
     */
    public static String makeVoiceCustomMessage(String openId, String mediaId) {
        String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"%s\"}}";
        return String.format(jsonMsg, openId, mediaId);
    }

    public static String makeVideoCustomMessage(String openId, String mediaId, String thumbMediaId) {
        String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"%s\",\"thumb_media_id\":\"%s\"}}";
        return String.format(jsonMsg, openId, mediaId, thumbMediaId);
    }

    public static String makeMusicCustomMessage(String openId, Music music) {
        String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"music\",\"music\":%s}";
        jsonMsg = String.format(jsonMsg, openId, JSONObject.fromObject(music).toString());
        jsonMsg = jsonMsg.replace("thumbmediaid", "thumb_media_id");
        return jsonMsg;
    }

    public static String makeNewsCustomMessage(String openId, List<Article> articleList) {
        String jsonMsg = "{\"touser\":\"%s\",\"msgtype\":\"news\",\"news\":{\"articles\":%s}}";
        jsonMsg = String.format(jsonMsg, openId, JSONArray.fromObject(articleList).toString().replaceAll("\"","\\\""));
        jsonMsg = jsonMsg.replace("picUrl", "picurl");
        return jsonMsg;
    }

  public static void main(String args[]){
      System.out.println(makeMusicCustomMessage("xxxxxxxxxx",new Music()));
      System.out.println(makeVideoCustomMessage("xxxxxxxxxx","m_id","t_m_id"));
      System.out.println(makeVoiceCustomMessage("xxxxxxxxxx","m_id"));
      System.out.println(makeImageCustomMessage("xxxxxxxxxx","m_id"));
  }
}


测试客服消息,发送文本和图文到用户。

package com.dm.wx.service;

import com.dm.wx.domain.respmessage.Article;
import com.dm.wx.domain.token.AccessToken;
import com.dm.wx.util.AdvancedUtil;
import com.dm.wx.util.CommonUtil;
import com.dm.wx.util.R;
import freemarker.cache.StrongCacheStorage;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 15-5-19
 * Time: 上午12:35
 * To change this template use File | Settings | File Templates.
 */
public class KefuSendMsg {    //只有在48小时内给公众号发送过消息的用户才能接收到客服消息

    public static void main(String args[]) {
        String appId = R.appId;
        String appSecret = R.appsecret;
        AccessToken at = CommonUtil.getAccessToken(appId, appSecret);  //获取accesstoken
        String jsonTextMsg = AdvancedUtil.makeTextCustomMessage("oO5Cbs6pJ6BK08gsrKH5ZPoD_BHM", "测试客服消息!小子,你厉害");//发送文本信息 到1用户
        String jsonTextMsg2 = AdvancedUtil.makeTextCustomMessage("oO5Cbs-KyEXBcLGM4tW_7QS0EJ2Y", "测试客服消息!"); //客服消息 组装文本
        // CommonUtil.sendCustomMessage(at.getAccessToken(),jsonTextMsg);
        // CommonUtil.sendCustomMessage(at.getAccessToken(),jsonTextMsg2);    //客服消息,发送文本

        List<Article> articleList = new ArrayList<Article>();
        Article article = new Article();
        article.setTitle("信息服务站");
        article.setDescription("信息服务站提供及时的各种信息,包括招聘,出租,兼职,美食,美女,帅哥,招租等。为你及时就地的服务。");
        article.setPicUrl("http://7xjjge.com1.z0.glb.clouddn.com/xxfw.jpg");
        article.setUrl("http://" + R.dns + "/chatman/msg_list.do?fromuser=oO5Cbs-KyEXBcLGM4tW_7QS0EJ2Y");
        articleList.add(article);
        String newsMessage = AdvancedUtil.makeNewsCustomMessage("oO5Cbs-KyEXBcLGM4tW_7QS0EJ2Y", articleList);   //客服消息 组装图文  发送图文消息给用户

        CommonUtil.sendCustomMessage(at.getAccessToken(), newsMessage);

    }
}

使用定时器发送定时任务提醒

package com.dm.wx.domain;

import com.dm.core.dao.SqlDao;

import com.dm.core.util.DateUtil;
import com.dm.core.util.StringUtil;
import com.dm.core.util.WebUtil;
import com.dm.ivi.domain.MytextMessage;
import com.dm.pfb.domain.Actoken;

import com.dm.wx.domain.token.AccessToken;

import com.dm.wx.util.AdvancedUtil;
import com.dm.wx.util.CommonUtil;

import com.dm.wx.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


/**
 * Created by IntelliJ IDEA.
 * User: ZZY
 * Date: 15-6-9
 * Time: 下午4:35
 * To change this template use File | Settings | File Templates.
 */
public class TokenThread implements Runnable {

    private static Logger log = LoggerFactory.getLogger(TokenThread.class);

    public static AccessToken accessToken = null;
    Actoken ac = new Actoken();
    SqlDao sqlDao = (SqlDao) WebUtil.getWebApplicationContext().getBean("sqlDao");

    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public String update(Actoken actoken) {
        System.out.println("执行更新--");
        String appId = R.appId;
        String appSerect = R.appsecret;
        String ac = CommonUtil.getAccessToken(appId, appSerect).getAccessToken();
        actoken.setAccesstoken(ac);
        actoken.setJs_ticket(CommonUtil.getJsTicket(ac));
        actoken.setCreate_time(new Date());
        sqlDao.update("pfb_actoken.update", actoken);
        return "1";
    }

    @Transactional(isolation = Isolation.READ_COMMITTED, rollbackFor = Throwable.class)
    public String create(Actoken actoken) {
        String appId = R.appId;
        String appSerect = R.appsecret;
        String ac = CommonUtil.getAccessToken(appId, appSerect).getAccessToken();
        actoken.setAppid(appId);
        actoken.setAppserect(appSerect);
        actoken.setAccesstoken(ac);
        actoken.setJs_ticket(CommonUtil.getJsTicket(ac));
        actoken.setCreate_time(new Date());
        sqlDao.create("pfb_actoken.create", actoken);
        return "1";
    }

    /*  定时客服消息提醒

    */
    public void Msg(String acToken) {

        StringBuffer s = new StringBuffer();
        MytextMessage mytextMessage = new MytextMessage();
        List<MytextMessage> mytextMessages = sqlDao.list("ivi_mytext_message.queryFromUsers", mytextMessage);//查询记事人列表
        if (!StringUtil.isEmpty(mytextMessages)) {
            MytextMessage myNoDone = new MytextMessage();
            for (MytextMessage my : mytextMessages) {
                String fromuser = my.getFromuser();
                myNoDone.setFromuser(fromuser);
                myNoDone.setMode("fromuser");
                myNoDone.setOrder("end_time asc");
                List<MytextMessage> mytextNoDoneMessageList = sqlDao.list("ivi_mytext_message.queryNoDone", myNoDone); //查询未完成的任务   http://182.254.188.241/ivi/mytextMessage_appmsview.do?t_id=F8EC6253E7CB49EA8E866EA3CBC6959F
                if (!StringUtil.isEmpty(mytextNoDoneMessageList)) {
                    s.append(mytextNoDoneMessageList.get(0).getCreate_user_name() + "您有待完成事件:\n");
                    for (int j = 0; j < mytextNoDoneMessageList.size(); j++) {
                        s.append((j+1) + ":【" +"<a href=\"http://" + R.dns + "/ivi/mytextMessage_appmsview.do?t_id=" + mytextNoDoneMessageList.get(j).getT_id() + "\">"+mytextNoDoneMessageList.get(j).getT_title()+"</a>").append("】截止:" + DateUtil.formatDateYMD(mytextNoDoneMessageList.get(j).getEnd_time())).append("\n");
                    }
                    String jsonTextMsg = AdvancedUtil.makeTextCustomMessage(fromuser, s.toString());
                    CommonUtil.sendCustomMessage(acToken, jsonTextMsg);    //客服消息,发送文本
                    s.setLength(0);//清空内容,再次循环
                }
            }
        }

    }

    public void run() {
        int i = 1;
        ac = sqlDao.query("pfb_actoken.query", new Actoken());
        if (StringUtil.isEmpty(ac)) {
            create(new Actoken());
            ac = sqlDao.query("pfb_actoken.query", new Actoken());
        }
        while (true) {
            try {

               String r = update(ac);

                if ("1".equals(r)) {
                    ac = sqlDao.query("pfb_actoken.query", new Actoken());
                    //System.out.println("更新时间:" + ac.getCreate_time());
                   // System.out.println("更新成功:" + ac.getAccesstoken());
                    log.info("更改后了js_ticket:" + ac.getJs_ticket() + "..." + "accessToken:" + ac.getAccesstoken());
                }
                /*
                定时器群发任务
                 */
                if (i % 3 == 0) {
                    Msg(ac.getAccesstoken());//字符提示 记事本未完成的消息 主动发送客服文本消息
                }
                i++;
                Thread.sleep((7200 - 200) * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

}

使用定时器控制,每日推送消息 


我的博客:115.159.110.224

我的微信公众号:my528xx

公众号二维码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jimin_zhou

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值