1. 程式人生 > >微信開發六 傳送訊息(客服/群發/模板)

微信開發六 傳送訊息(客服/群發/模板)

本例中有些引用的類與方法不做過多介紹,之後會提供完整原始碼下載,請自行檢視。

客服介面

當用戶主動發訊息給公眾號的時候(包括髮送資訊、點選自定義選單、訂閱事件、掃描二維碼事件、支付成功事件、使用者維權),微信將會把訊息資料推送給開發者,開發者在一段時間內(目前修改為48小時)可以呼叫客服訊息介面,通過POST一個JSON資料包來發送訊息給普通使用者,在48小時內不限制傳送次數。此介面主要用於客服等有人工訊息處理環節的功能,方便開發者為使用者提供更加優質的服務。

為了幫助公眾號使用不同的客服身份服務不同的使用者群體,客服介面進行了升級,開發者可以管理客服賬號,並設定客服賬號的頭像和暱稱。該能力針對所有擁有客服介面許可權的公眾號開放。

/*
 * 客服
 * http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
 */
public class KFAccount {

    private String kf_account;      //是 完整客服賬號,格式為:賬號字首@公眾號微訊號
    private String kf_nick;         //是 客服暱稱
    private String kf_id;           //是 客服工號
    private String nickname;        //是 客服暱稱,最長6個漢字或12個英文字元
private String password; //否 客服賬號登入密碼,格式為密碼明文的32位加密MD5值。該密碼僅用於在公眾平臺官網的多客服功能中使用,若不使用多客服功能,則不必設定密碼 private String media; //是 該引數僅在設定客服頭像時出現,是form-data中媒體檔案標識,有filename、filelength、content-type等資訊 }
@RunWith(SpringJUnit4ClassRunner.class)        // 表示繼承了 SpringJUnit4ClassRunner 類
@ContextConfiguration("classpath:spring-mybatis.xml") public class TestWXCustomService { @Resource private CustomServiceService customServiceService; // @Test public void addKFAccount() { KFAccount kfAccount = new KFAccount(); kfAccount.setKf_account("[email protected]"); kfAccount.setNickname("小白"); kfAccount.setPassword("hq246512"); System.out.println(">>>"+customServiceService.addKFAccount(kfAccount)); } // @Test public void updateKFAccount() { KFAccount kfAccount = new KFAccount(); kfAccount.setKf_account("[email protected]"); kfAccount.setNickname("大白"); kfAccount.setPassword("hq246512"); System.out.println(">>>"+customServiceService.updateKFAccount(kfAccount)); } // @Test public void deleteKFAccount() { KFAccount kfAccount = new KFAccount(); kfAccount.setKf_account("[email protected]"); kfAccount.setNickname("大白"); kfAccount.setPassword("hq246512"); System.out.println(">>>"+customServiceService.deleteKFAccount(kfAccount)); } // @Test public void setKFAccountHeadImg() { System.out.println(">>>"+customServiceService.setKFAccountHeadImg("D:\\test\\head.jpg", "[email protected]")); } // @Test public void getAllKFAccount() { System.out.println(">>>"+customServiceService.getAllKFAccount()); } // @Test public void sendMessageByKF() { KFResponse kfResponse = new KFResponse(); kfResponse.setTouser(WeChatCertificate.OPENID1); //傳送文字訊息 kfResponse.setMsgtype("text"); KFResponse.Text text = kfResponse.new Text(); text.setContent("hello, this is kf response!"); kfResponse.setText(text); //傳送音樂訊息 注:40007需重新上傳素材,Remark->第三條。 // kfResponse.setMsgtype("music"); // KFResponse.Music music = kfResponse.new Music(); // music.setTitle("殘酷月光"); // music.setDescription(" 林宥嘉 詞:向月娥 曲:陳小霞"); // music.setMusicurl("http://wma.5282.cc/2008-11//20140226/1.mp3"); // music.setHqmusicurl("http://wma.5282.cc/2008-11//20140226/1.mp3"); // music.setThumb_media_id(WeChatCertificate.VIDEOMEDIAID); // kfResponse.setMusic(music); //傳送圖文訊息 // kfResponse.setMsgtype("news"); // KFResponse.News news = kfResponse.new News(); // for(int i=0; i<3; i++) { // KFResponse.News.Article article = news.new Article(); // article.setTitle("title"+i); // article.setDescription("description"+i); // article.setPicurl("picurl"+i); // article.setUrl("url"+i); // news.getArticles().add(article); // } // kfResponse.setNews(news); //某個客服帳號來發訊息 // KFResponse.CustomService customservice = kfResponse.new CustomService(); // customservice.setKF_Account("[email protected]"); // kfResponse.setCustomservice(customservice); System.out.println(">>>"+customServiceService.sendMessageByKF(kfResponse)); } }

#

在公眾平臺網站上,為訂閱號提供了每天一條的群發許可權,為服務號提供每月(自然月)4條的群發許可權。而對於某些具備開發能力的公眾號運營者,可以通過高階群發介面,實現更靈活的群發能力。

/*
 * 根據標籤進行群發【訂閱號與服務號認證後均可用】
 * https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140549&token=&lang=zh_CN
 */
public class MassSendResponse {

    private String  msgtype;    //群發的訊息型別,
    private Filter  filter;     //用於設定圖文訊息的接收者
    private String  previewUser;//用於設定圖文訊息的接收者
    private List<String> touser;//填寫圖文訊息的接收者,一串OpenID列表,OpenID最少2個,最多10000個
    private MPNews  mpnews;     //圖文訊息(注意圖文訊息的media_id需要通過上述方法來得到):
    private MPVideo mpvideo;    //此處視訊的media_id需通過POST請求WeChatInterface.uploadVideo的返回結果獲得
    private Text    text;       //文字
    private Voice   voice;      //語音(注意此處media_id需通過基礎支援中的上傳下載多媒體檔案來得到):
    private Image   image;      //圖片(注意此處media_id需通過基礎支援中的上傳下載多媒體檔案來得到):
    private WXCard  wxcard;     //卡券訊息(注意圖文訊息的media_id需要通過上述方法來得到)

    public String getMsgtype() {
        return msgtype;
    }
    public void setMsgtype(String msgtype) {
        this.msgtype = msgtype;
    }

    public Filter getFilter() {
        return filter;
    }
    public void setFilter(Filter filter) {
        this.filter = filter;
    }

    public MPNews getMpnews() {
        return mpnews;
    }
    public void setMpnews(MPNews mpnews) {
        this.mpnews = mpnews;
    }

    public MPVideo getMpvideo() {
        return mpvideo;
    }
    public void setMpvideo(MPVideo mpvideo) {
        this.mpvideo = mpvideo;
    }

    public Text getText() {
        return text;
    }
    public void setText(Text text) {
        this.text = text;
    }

    public Voice getVoice() {
        return voice;
    }
    public void setVoice(Voice voice) {
        this.voice = voice;
    }

    public Image getImage() {
        return image;
    }
    public void setImage(Image image) {
        this.image = image;
    }

    public WXCard getWxcard() {
        return wxcard;
    }
    public void setWxcard(WXCard wxcard) {
        this.wxcard = wxcard;
    }

    public List<String> getTouser() {
        if(touser==null) touser= new ArrayList<String>();
        return touser;
    }
    public void setTouser(List<String> touser) {
        this.touser = touser;
    }

    public String getPreviewUser() {
        return previewUser;
    }
    public void setPreviewUser(String previewUser) {
        this.previewUser = previewUser;
    }

    public class Filter{
        private boolean is_to_all;  //用於設定是否向全部使用者傳送,值為true或false,選擇true該訊息群發給所有使用者,選擇false可根據tag_id傳送給指定群組的使用者
        private String group_id;    //群發到的分組的group_id.若is_to_all值為true,可不填寫group_id 

        public boolean getIs_to_all() {
            return is_to_all;
        }
        public void setIs_to_all(boolean is_to_all) {
            this.is_to_all = is_to_all;
        }

        public String getGroup_id() {
            return group_id;
        }
        public void setGroup_id(String group_id) {
            this.group_id = group_id;
        }
    }

    public class MPNews{
        private String media_id;    //用於群發的訊息的media_id

        public String getMedia_id() {
            return media_id;
        }
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
    }

    public class MPVideo{
        private String media_id;

        public String getMedia_id() {
            return media_id;
        }
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
    }

    public class Text{
        private String content;

        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
    }

    public class Voice{
        private String media_id;

        public String getMedia_id() {
            return media_id;
        }
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
    }

    public class Image{
        private String media_id;

        public String getMedia_id() {
            return media_id;
        }
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
    }

    public class WXCard{
        private String media_id;

        public String getMedia_id() {
            return media_id;
        }
        public void setMedia_id(String media_id) {
            this.media_id = media_id;
        }
    }
}
@RunWith(SpringJUnit4ClassRunner.class)        // 表示繼承了 SpringJUnit4ClassRunner 類
@ContextConfiguration("classpath:spring-mybatis.xml")
public class TestWXMassSendMsg {

    @Resource   
    private MassSendMsgService massSendMsgService;

//  @Test
    public void getUrlByUploadImg() {
        //curl: (60) SSL certificate problem: unable to get local issuer certificate   請檢視 Remark第五條
  System.out.println(">>>"+massSendMsgService.getUrlByUploadImg("D:/test/test.jpg"));
    }

//  @Test
    public void getVideoMediaIdMassMsg() {
        Material massMsg = new Material();
        massMsg.setMedia_id(WeChatCertificate.VIDEO_MEDIAID);
        massMsg.setTitle("title");
        massMsg.setDescription("description");
  System.out.println(">>>"+massSendMsgService.getVideoMediaIdMassMsg(massMsg));
    }

//  @Test
    public void uploadNews() {
        Material mass = new Material();
        for(int i=0; i<1; i++) {
            Material.Article article = mass.new Article();
            article.setThumb_media_id(WeChatCertificate.THUMB_MEDIA_ID);
            article.setAuthor("Author"+i);
            article.setTitle("Title"+i);
            article.setContent_source_url("www.baidu.com");
            article.setContent("Content"+i);
            article.setDigest("Digest"+i);
            article.setShow_cover_pic(article.SHOW);
            mass.getArticles().add(article);
        }
        System.out.println(">>>"+massSendMsgService.uploadNews(mass));
    }

//  @Test
    public void massMsgByPacket() {
        MassSendResponse mass = new MassSendResponse();
        mass.setMsgtype("mpnews");
        MassSendResponse.Filter filter = mass.new Filter();
        filter.setIs_to_all(false);
        filter.setGroup_id("101");
        mass.setFilter(filter);
        MassSendResponse.MPNews mpnews = mass.new MPNews();
        mpnews.setMedia_id(WeChatCertificate.NEWS_ID);
        mass.setMpnews(mpnews);
      System.out.println(">>>"+massSendMsgService.massMsgByPacket(mass));
    }

//  @Test
    public void massMsgByOpenID() {
        MassSendResponse mass = new MassSendResponse();
        mass.setMsgtype("mpnews");
        mass.getTouser().add(WeChatCertificate.OPENID1);
        mass.getTouser().add(WeChatCertificate.OPENID2);
        MassSendResponse.MPNews mpnews = mass.new MPNews();
        mpnews.setMedia_id(WeChatCertificate.NEWS_ID);
        mass.setMpnews(mpnews);
      System.out.println(">>>"+massSendMsgService.massMsgByOpenID(mass));
    }

//  @Test
    public void deleteMassMsg() {
        Material mass = new Material();
        mass.setMsg_id(WeChatCertificate.MSG_ID);
      System.out.println(">>>"+massSendMsgService.deleteMassMsg(mass));
    }

//  @Test
    public void previewMassMsg() {
        MassSendResponse mass = new MassSendResponse(); 
        mass.setMsgtype("mpnews");
        mass.setPreviewUser(WeChatCertificate.OPENID1);
        MassSendResponse.MPNews mpnews = mass.new MPNews();
        mpnews.setMedia_id(WeChatCertificate.NEWS_ID);
        mass.setMpnews(mpnews);
      System.out.println(">>>"+massSendMsgService.previewMassMsg(mass));
    }

//  @Test
    public void getMassMsgStatus() {
        Material mass = new Material();
        mass.setMsg_id(WeChatCertificate.MSG_ID);
      System.out.println(">>>"+massSendMsgService.getMassMsgStatus(mass));
    }
}

模板訊息介面

模板訊息僅用於公眾號向用戶傳送重要的服務通知,只能用於符合其要求的服務場景中,如信用卡刷卡通知,商品購買成功通知等。不支援廣告等營銷類訊息以及其它所有可能對使用者造成騷擾的訊息。

/*
 * 模板訊息
 * http://mp.weixin.qq.com/wiki/17/304c1885ea66dbedf7dc170d84999a9d.html
 */
public class MessageModel {

    public String industry_id1;         //公眾號模板訊息所屬行業編號
    public String industry_id2;         //公眾號模板訊息所屬行業編號

    public String template_id_short;    //模板庫中模板的編號,有“TM**”和“OPENTMTM**”等形式

    public String template_id;          //公眾帳號下模板訊息ID

    public String touser;               //是 接收者openid
    public String url;                  //否 模板跳轉連結
    public String data;                 //是 模板資料

    public String getTouser() {
        return touser;
    }
    public void setTouser(String touser) {
        this.touser = touser;
    }

    public String getUrl() {
        return url;
    }
    public void setUrl(String url) {
        this.url = url;
    }

    public String getData() {
        return data;
    }
    public void setData(String data) {
        this.data = data;
    }

    public String getTemplate_id() {
        return template_id;
    }
    public void setTemplate_id(String template_id) {
        this.template_id = template_id;
    }

    public String getTemplate_id_short() {
        return template_id_short;
    }
    public void setTemplate_id_short(String template_id_short) {
        this.template_id_short = template_id_short;
    }
    public String getIndustry_id1() {
        return industry_id1;
    }
    public void setIndustry_id1(String industry_id1) {
        this.industry_id1 = industry_id1;
    }

    public String getIndustry_id2() {
        return industry_id2;
    }
    public void setIndustry_id2(String industry_id2) {
        this.industry_id2 = industry_id2;
    }
}
@RunWith(SpringJUnit4ClassRunner.class)        // 表示繼承了 SpringJUnit4ClassRunner 類
@ContextConfiguration("classpath:spring-mybatis.xml")
public class TestWXMsgModel {

    @Resource   
    private MsgModelService msgModelService;

//  @Test
    public void setIndustry() {
        MessageModel industry = new MessageModel();
        industry.setIndustry_id1("2");
        industry.setIndustry_id2("31");
        System.out.println(">>>"+msgModelService.setIndustry(industry));
    }

//  @Test
    public void getIndustry() {
        System.out.println(">>>"+msgModelService.getIndustry());
    }

//  @Test
    public void getIdByaddTemplate() {
        MessageModel model = new MessageModel();
        model.setTemplate_id_short("TM00015");
      System.out.println(">>>"+msgModelService.getIdByaddTemplate(model));
    }

//  @Test
    public void deleteTemplate() {
        MessageModel model = new MessageModel();
        model.setTemplate_id(WeChatCertificate.MODELID);
        System.out.println(">>>"+msgModelService.deleteTemplate(model));
    }

//  @Test
    public void getAllTemplate() {
        System.out.println(">>>"+msgModelService.getAllTemplate());
    }

//  @Test
    public void sendTemplate() {
        MessageModel model = new MessageModel();
        model.setTouser(WeChatCertificate.OPENID2);
        model.setTemplate_id(WeChatCertificate.MODELID);
        model.setUrl("http://www.baidu.com");
        System.out.println(">>>"+msgModelService.sendTemplate(model));
    }
}

相關推薦

開發 傳送訊息/群發/模板

本例中有些引用的類與方法不做過多介紹,之後會提供完整原始碼下載,請自行檢視。 客服介面 當用戶主動發訊息給公眾號的時候(包括髮送資訊、點選自定義選單、訂閱事件、掃描二維碼事件、支付成功事件、使用者維權),微信將會把訊息資料推送給開發者,開發者在

開發傳送訊息介面

傳送訊息,是指使用者公眾號向用戶傳送相應形式的訊息。根據微信開發文件,由以下四種形式:被動回覆,群發介面,客服訊息介面以及模板訊息介面。本文將基於Java語言以及個人微信測試號,說明被動回覆、客服訊息介

EasyWeChat開發平臺第三方接入Laravel5+,EasyWeChat3.0

一、準備微信開發平臺賬號(需要認證¥300) 二、看程式碼 <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use EasyWeChat\Foundation\Applicati

Python && 企業號傳送訊息指令碼

#!/usr/local/python3/bin/python3 #-*- coding:utf-8 -*- import requests import json import sys import os #企業號及應用相關資訊 corp_id='wwe0x

PHP實現開發中提現功能企業付款到使用者零錢

一.實現該功能目的     這幾天在小程式裡要實現使用者從系統中提現到零錢的功能,查了一下文件可以使用 企業付款到使用者零錢 來實現;  官方文件:https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapt

java之開發回覆圖片訊息

微信開發文件見:詳見 具體實現見:詳見 public class WxController { public static final Logger LOGGER= LoggerFactory.getLogger(WxController.

java 開發 常用工具類xml傳輸和解析 json轉換物件

與微信通訊常用工具(xml傳輸和解析) package com.lownsun.wechatOauth.utl; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; impo

公眾平臺 傳送訊息-群發介面

前言:最近做微信開發,弄了一個群發介面,本著不記錄肯定會忘記的本事,在這裡整理一下。多學習多進步。本文比較囉嗦,可以檢視參考文章,我就是從那位大神那裡學習到的。本片文章基於Spring開發,Spring提供的用於訪問Rest服務的客戶端。 這篇文章解決的問題:

開發一伺服器地址URL、令牌Token配置

前期準備工作準備工具:Tomcat伺服器,阿里雲伺服器阿里雲伺服器購買:學生可以使用雲翼計劃購買阿里雲伺服器,平均一個月12塊左右。正常阿里雲購買一個月阿里雲伺服器配置:登陸進入阿里雲伺服器的控制檯,進入"雲伺服器ECS",具體位置如下圖1所示。圖1進入雲伺服器ECS後入圖2

公眾號傳送訊息

import org.apache.http.HttpEntity; import org.apache.http.client.config.RequestConfig; import org.apache.http.client.entity.UrlEncodedFormEntity; import

公眾號傳送訊息給使用者 php

1.微信公眾號 這裡得話 一開始先去看了 微信公眾號的介面 發現網頁授權需要時認證的服務號,一開始想的是那去申請一個認證的服務號豈不是很費事,然後網上搜了搜,發現了還有微信公眾號個人測試號這個東西,所有的介面都可以使用(據說是這樣),然後就開始去學習使用微信公眾號個人測試號的使用; 個人測試號網址: http

小程序之初探常見語法 VS vue常見問題點擊不生效,數據綁定

數據驅動 一點 驅動 win -1 沒有 html cat been 最近在調研微信小程序開發,對於一個前端小白來說,在各種框架都還用不熟的情況下,再來開發小程序確實還是不容易。 小程序出來之初,聽過演講,看過一點點兒視頻,感覺和angular語法有點相似(PS:那是也是只

小程序 基礎操作邊做邊學

內容 多層 his 路徑 ack navig data 所有 順序 動態修改綁定數據方法,需要使用Page.prototype.setData().setData函數用於將數據異步從邏輯層發送到視圖層。同時改變對應的this.data的值。 小程序使用storage。本地緩

php5.6 退款回撥處理AES-256-ECB+PKCS7Padding

微信退款的回撥中有個欄位是req_info。該欄位微信官方給的解密方法是: 解密步驟如下:  (1)對加密串A做base64解碼,得到加密串B (2)對商戶key做md5,得到32位小寫key* ( key設定路徑:微信商戶平臺(pay.weixin.qq.com)-->

公眾號接入第三方平臺系統的操作步驟:

最近看到有人抱怨公眾號後臺的客服功能太爛,使用者來了只能通過網頁視窗交流,一個使用者一個視窗,有新訊息也不提醒,需要不斷的重新整理,嚴重影響了客服的工作效率。其實大可不必這麼麻煩,只需要在公眾號接入一套第三方的客服系統就完全可以搞定,而且它還不止這些功能哦!   那麼該如何接入第三方客服系統呢?

自定義分享連結設定標題+簡介+圖片

$(function() { //對url進行編碼 var localUrl = encodeURIComponent(location.href.split('#')[0]); //url傳到後臺格式 var Url = "URL=" +localUrl; //這幾

公眾平臺—使用者管理獲取使用者基本資訊

play 框架中的使用 網頁授權獲取使用者基本資訊 如果使用者在微信客戶端中訪問第三方網頁,公眾號可以通過微信網頁授權機制,來獲取使用者基本資訊,進而實現業務邏輯 關於網頁授權回撥域名的說明 1、在微信公眾號請求使用者網頁授權之前,開發者需要先到公眾

一鍵啟用會員卡跳轉外鏈

1.建立會員卡的同時配置(use_custom_code:false)// 1.填入了自動啟用auto_activate欄位,啟用連結activate_url和一鍵開卡介面設定都會失效;// 2.若同時傳入了activate_url,則一鍵開卡介面設定會失效;// 3.建議開

公眾號和公眾號系統有什麽區別,要怎麽綁定?

註意 申請 知識 網站 整合營銷 由於 作用 營銷 界面 微信作為當今社交軟件當之無愧的“一哥”,是企業接觸客戶不可缺少的一種方式,因此許多企業都想用使用微信公眾號客服來接待客戶,但是很多企業都不了解該如何接入和使用微信公眾號客服。今天小編將為您介紹一下企業接入微信在線客服

點餐系統感悟上1-6章

系統 去掉 Pageable 數據結構 ctc 實現類 異常 自定義 截圖 小結感悟 1.首先完成了一個階段,對所有的Api進行了分析與實現 2.項目開始時說的技術只實現了SpringBoot+jpa的數據庫實現和RESTful風格接口實現前後端的數據連通 3.項目實現的四