1. 程式人生 > >java微信客服訊息介面

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;publicclassAdvancedUtil{privatestaticLogger log =LoggerFactory.getLogger(AdvancedUtil.class);/** * 組裝傳送文字訊息 * * @return */publicstaticString makeTextCustomMessage(String openId,String content){ content
= content.replace("\"","\\\"");String jsonMsg ="{\"touser\":\"%s\",\"msgtype\":\"text\",\"text\":{\"content\":\"%s\"}}";returnString.format(jsonMsg, openId, content);}/** * 組裝傳送圖片訊息 * * @return */publicstaticString makeImageCustomMessage(String openId,String mediaId){String jsonMsg
="{\"touser\":\"%s\",\"msgtype\":\"image\",\"image\":{\"media_id\":\"%s\"}}";returnString.format(jsonMsg, openId, mediaId);}/** * 組裝傳送語音訊息 * * @return */publicstaticString makeVoiceCustomMessage(String openId,String mediaId){String jsonMsg ="{\"touser\":\"%s\",\"msgtype\":\"voice\",\"voice\":{\"media_id\":\"%s\"}}";returnString.format(jsonMsg, openId, mediaId);}publicstaticString makeVideoCustomMessage(String openId,String mediaId,String thumbMediaId){String jsonMsg ="{\"touser\":\"%s\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"%s\",\"thumb_media_id\":\"%s\"}}";returnString.format(jsonMsg, openId, mediaId, thumbMediaId);}publicstaticString 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;}publicstaticString 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;}publicstaticvoid main(String args[]){System.out.println(makeMusicCustomMessage("xxxxxxxxxx",newMusic()));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.
 */publicclassKefuSendMsg{//只有在48小時內給公眾號傳送過訊息的使用者才能接收到客服訊息publicstaticvoid main(String args[]){String appId = R.appId;String appSecret = R.appsecret;AccessToken at =CommonUtil.getAccessToken(appId, appSecret);//獲取accesstokenString 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 =newArrayList<Article>();Article article =newArticle();
        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.
 */publicclassTokenThread implements Runnable{privatestaticLogger log =LoggerFactory.getLogger(TokenThread.class);publicstaticAccessToken accessToken =null;Actoken ac =newActoken();SqlDao sqlDao =(SqlDao)WebUtil.getWebApplicationContext().getBean("sqlDao");@Transactional(isolation =Isolation.READ_COMMITTED, rollbackFor =Throwable.class)publicString 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(newDate());
        sqlDao.update("pfb_actoken.update", actoken);return"1";}@Transactional(isolation =Isolation.READ_COMMITTED, rollbackFor =Throwable.class)publicString 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(newDate());
        sqlDao.create("pfb_actoken.create", actoken);return"1";}/*  定時客服訊息提醒

    */publicvoidMsg(String acToken){StringBuffer s =newStringBuffer();MytextMessage mytextMessage =newMytextMessage();List<MytextMessage> mytextMessages = sqlDao.list("ivi_mytext_message.queryFromUsers", mytextMessage);//查詢記事人列表if(!StringUtil.isEmpty(mytextMessages)){MytextMessage myNoDone =newMytextMessage();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=F8EC6253E7CB49EA8E866EA3CBC6959Fif(!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);//清空內容,再次迴圈}}}}publicvoid run(){int i =1;
        ac = sqlDao.query("pfb_actoken.query",newActoken());if(StringUtil.isEmpty(ac)){
            create(newActoken());
            ac = sqlDao.query("pfb_actoken.query",newActoken());}while(true){try{String r = update(ac);if("1".equals(r)){
                    ac = sqlDao.query("pfb_actoken.query",newActoken());//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

公眾號二維碼: