客服消息
參考:官方文檔
創建客服消息事件
官方代碼:
第一種:
<contact-button
type="default-light"
size="20"
session-from="weapp"
>
</contact-button>
第二種:
給button按鈕添加open-type="contact"
<button
class="weui-btn"
type="primary"
open-type="contact"
>發送客服信息
</button>
然后打開微信web開發者工具的預覽,手機上訪問調試的小程序。
點擊剛才設置的發送客服消息(我只用過第二種,第一種沒試過),小程序會跳轉到會話界面,這個界面跟普通會話沒有什么區別,但是只能給小程序發送文本消息
和圖片消息
客服消息界面
以上這些就是客服消息的界面發送入口了
接收消息
準備工作
在微信小程序網頁上面配置開發者服務器的地址
這里我的已經設置好了
消息服務器配置
下面再就是對用戶發過來的消息進行代碼編寫了
需要注意的地方
- 消息推送配置點提交的時候,微信發送的是GET請求。
- 當用戶給小程序發送消息的時候,微信發送的POST請求。
- 這兩次的請求雖然不一樣,但是他們的
請求的地址
要是一樣的。
上代碼來說:
這里是在網頁上填寫完配置信息點提交的時候,微信請求開發者服務器,發送GET請求,Java層做的處理,校驗通過則網頁上回提示提交成功。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.core.util.wx.SignUtil;
import com.core.util.wx.WXMsgResponseUtil;
import com.udbac.entity.WXImageRequest;
import com.udbac.entity.WXTextRequest;
import com.udbac.util.WxApiClient;
import net.sf.json.JSONObject;
@Controller
@RequestMapping("/wxapi")
public class WxApiController {
/**
* GET請求:進行URL、Tocken 認證;
* 1.將token、timestamp、nonce三個參數進行字典序排序
* 2.將三個參數字符串拼接成一個字符串進行sha1加密
* 3.開發者獲得加密后的字符串可與signature對比,標識該請求來源于微信
*/
@RequestMapping(value = "/message/token", method = RequestMethod.GET)
public @ResponseBody String doGet(HttpServletRequest request) {
String token = "mytoken";// 獲取token,進行驗證;
String signature = request.getParameter("signature");// 微信加密簽名
String timestamp = request.getParameter("timestamp");// 時間戳
String nonce = request.getParameter("nonce");// 隨機數
String echostr = request.getParameter("echostr");// 隨機字符串
// 校驗成功返回 echostr,成功成為開發者;否則返回error,接入失敗
if (SignUtil.validSign(signature, token, timestamp, nonce)) {
return echostr;
}
return "error";
}
好了,到這一步完了,就可以讓用戶發送消息了,但是我們要處理用戶發送過來的消息是什么?
繼續上代碼:
/**
* POST請求:
* 接收客服消息;
* 此處的value值也就是往微信公眾平臺要配置的請求的地址
*/
@ResponseBody
@RequestMapping(value = "/message/token",method=RequestMethod.POST)
public JSONObject doMessagePost(HttpServletRequest request,HttpServletResponse response) {
try {
ServletInputStream stream = request.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = new String("");
while((line = reader.readLine()) != null){
buffer.append(line);
}
JSONObject jsonObject = JSONObject.fromObject(buffer.toString());
System.out.println(jsonObject);
if (jsonObject.getString("MsgType").equals("text")) { //收到的是文本消息
WXTextRequest wxRequest = new WXTextRequest();
wxRequest.setToUserName(jsonObject.getString("ToUserName"));
wxRequest.setFromUserName(jsonObject.getString("FromUserName"));
wxRequest.setCreateTime(jsonObject.getString("CreateTime"));
wxRequest.setMsgType(jsonObject.getString("MsgType"));
wxRequest.setContent(jsonObject.getString("Content"));
wxRequest.setMsgId(jsonObject.getString("MsgId"));
//也回復一個文本消息
return WXMsgResponseUtil.sendCustomerMessage(WxApiClient.touser);
}else{ //那就是圖片的消息了
WXImageRequest imageRequest = new WXImageRequest();
imageRequest.setToUserName(jsonObject.getString("ToUserName"));
imageRequest.setFromUserName(jsonObject.getString("FromUserName"));
imageRequest.setCreateTime(jsonObject.getString("CreateTime"));
imageRequest.setMsgType(jsonObject.getString("MsgType"));
imageRequest.setPicUrl(jsonObject.getString("PicUrl"));
imageRequest.setMediaId(jsonObject.getString("MediaId"));
imageRequest.setMsgId(jsonObject.getString("MsgId"));
//也回復一個圖片消息
return WXMsgResponseUtil.sendCustomerImageMessage(WxApiClient.touser,imageRequest.getMediaId());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
此處可以看到處理用戶消息的controller跟配置消息請求的controller請求地址是一樣的,就是訪問的請求方式不一樣,這是尤其注意的地方。
這里有一個消息回復處理工具類:
微信小程序就兩種消息處理,文本和圖片,所以也就相對簡單多了
import java.util.Date;
import com.udbac.util.HttpUtil;
import com.udbac.util.WxApiClient;
import net.sf.json.JSONObject;
/**
* 測試客服消息發送 Title:TemplateTest Description:
*
* @author root
* @date 2017年6月7日 上午11:30:49
*/
public class WXMsgResponseUtil {
/***
* 文檔地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/conversation.html
* 發送的文本消息
*/
public static JSONObject sendCustomerMessage(String touser){
JSONObject obj = new JSONObject();
obj.put("touser", touser);
obj.put("msgtype", "text");
JSONObject text = new JSONObject();
text.put("content", "現在時刻:\n" + new Date());
obj.put("text", text);
System.out.println("回復的文本:\n"+obj.toString());
JSONObject jsonObject = HttpUtil.httpsRequest(WxApiClient.getCustomerMessageUrl(), "POST", obj.toString());
System.out.println(jsonObject);
return jsonObject;
}
/***
* 文檔地址:https://mp.weixin.qq.com/debug/wxadoc/dev/api/custommsg/conversation.html
* 發送的圖片消息
*/
public static JSONObject sendCustomerImageMessage(String touser,String mediaId){
JSONObject obj = new JSONObject();
obj.put("touser", touser);
obj.put("msgtype", "image");
JSONObject media = new JSONObject();
media.put("media_id",mediaId);
obj.put("image", media);
System.out.println("回復的圖片:\n"+obj.toString());
JSONObject jsonObject = HttpUtil.httpsRequest(WxApiClient.getCustomerMessageUrl(), "POST", obj.toString());
System.out.println(jsonObject);
return jsonObject;
}
}
最后看實際效果吧!
客服消息測試發送
測試發送文本消息和圖片消息都沒有問題。