小程序客服消息开发怎么弄
开发微信小程序之后要有用户使用,那就意味着要有客服回复用户的各种消息,所以开发者还需要最后一步,就是小程序客服消息开发,看下操作流程:
当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。此接口主要用于客服等有人工消息处理环节的功能,方便开发者为用户提供更加优质的服务。
这次就针对客服消息分享下我的心得和体会
1 客服接口-发消息接口使用说明
接口调用请求说明
请求方式: POST
api.weixin.qq/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN
各消息类型所需的JSON数据包如下:
发送文本消息
{
"touser":"OPENID",
"msgtype":"text",
"text":
{
"content":"Hello World"
}
}
发送图片消息
{
"touser":"OPENID",
"msgtype":"image",
"image":
{
"media_id":"MEDIA_ID"
}
}
发送语音消息
{
"touser":"OPENID",
"msgtype":"voice",
"voice":
{
"media_id":"MEDIA_ID"
}
}
......,基本上来说常用的消息类型,客服接口都是支持的
2 封装客服消息接口
[java] view plain copy public static String makeTextCustomMessage(String openId,String content){
content.replace(""", """);
String jsonMsg="{"touser":"%s","msgtype":"text","text":{"content":"%s"}}";
return String.format(jsonMsg, openId,content);
}
/**
* 组装图片客服消息
*
* @param openId 消息发送对象
* @param mediaId 媒体文件id
* @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);
}
/**
* 组装语音客服消息
*
* @param openId 消息发送对象
* @param mediaId 媒体文件id
* @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);
}
/**
* 组装视频客服消息
*
* @param openId 消息发送对象
* @param mediaId 媒体文件id
* @param thumbMediaId 视频消息缩略图的媒体id
* @return
*/
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);
}
/**
* 组装音乐客服消息
*
* @param openId 消息发送对象
* @param music 音乐对象
* @return
*/
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());
// 参数名称替换 @20140125
jsonMsg = jsonMsg.replace("musicUrl", "musicurl");
jsonMsg = jsonMsg.replace("HQMusicUrl", "hqmusicurl");
jsonMsg = jsonMsg.replace("thumbMediaId", "thumb_media_id");
return jsonMsg;
}
/**
* 组装图文客服消息
*
* @param openId 消息发送对象
* @param articleList 图文消息列表
* @return
*/
public static String makeNewsCustomMessage(String openId, List articleList) {
String jsonMsg = "{"touser":"%s","msgtype":"news","news":{"articles":%s}}";
jsonMsg = String.format(jsonMsg, openId, JSONArray.fromObject(articleList).toString().replaceAll(""", """));
// 将jsonMsg中的picUrl替换为picurl
jsonMsg = jsonMsg.replace("picUrl", "picurl");
return jsonMsg;
}
public static boolean sendCustomMessage(String token,String jsonMsg){
boolean flag=false;
//String accessToken=getAccessToken("xxxx","xxxx").getToken();
String requestUrl="api.weixin.qq/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN";
requestUrl=requestUrl.replace("ACCESS_TOKEN", token);
JSONObject jsonResult=CommonUtil.sRequest(requestUrl, "POST", jsonMsg);
if(jsonResult!=null){
int errorCode=jsonResult.getInt("errcode");
String errorMessage=jsonResult.getString("errmsg");
if(errorCode==0){
flag=true;
}else{
System.out.println("客服消息发送失败:"+errorCode+","+errorMessage);
flag=false;
}
}
return flag;
}
3 调用客服消息发送接口给指定用户发送客服消息
[java] view plain copyList all=new ArrayList();
Article a2=new Article();
a2.setDescription("最崇拜的明星Justin Timberlake");
a2.setPicUrl("img3.douban/view/photo/photo/public/p1408738004.jpg");
a2.setTitle("贾斯汀·汀布莱克(Justin Timberlake),1981年1月31日出生于美国田纳西州孟菲斯市,美国男歌手、演员、音乐制作人、主持人,前男子演唱组合超级男孩成员。");
a2.setUrl("baike.haosou/doc/3382630-3560934.html?from=1358&sid=3560934&redirect=search");
all.add(a2);
String articleMsg=makeNewsCustomMessage("xxx", all);
boolean res=sendCustomMessage(token,articleMsg);
if(res){
System.out.println("客服消息发送成功");
}else{
System.out.println("客服消息发送失败");
}
小程序客服消息开发完成之后,就可以和用户简单交流了,这样一来小程序的使用就会更加便捷。怎么样,是不是很简单呢?想要获取更多相关资料请关注微信小程序素材网。
小程序客服功能使用说明及客服电话
微信小程序客服api是什么?要怎么使用?
微信小程序客服接入怎么操作
下一篇:微信公众号刷僵尸粉会不会被封号?