微信客服系统开发SDK使用教程-给好友发消息任务
case "TalkToFriendTask": {// 给好友发消息任务
log.debug("websocket:msgtype=TalkToFriendTask。。。。。");
talkToFriendTaskWebsocketHandler.handleMsg(ctx, vo,contentJsonStr);
break;
}
package com.jubotech.framework.netty.handler.websocket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.protobuf.util.JsonFormat;
import com.jubotech.business.web.domain.WeChatAccountInfo;
import com.jubotech.business.web.domain.WeChatMessageInfo;
import com.jubotech.business.web.service.WeChatAccountService;
import com.jubotech.business.web.service.WeChatMessageService;
import com.jubotech.framework.netty.common.Constant;
import com.jubotech.framework.netty.utils.ByteStringToString;
import com.jubotech.framework.netty.utils.MessageUtil;
import com.jubotech.framework.netty.utils.NettyConnectionUtil;
import Jubo.JuLiao.IM.Wx.Proto.TalkToFriendTask.TalkToFriendTaskMessage;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumErrorCode;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.EnumMsgType;
import Jubo.JuLiao.IM.Wx.Proto.TransportMessageOuterClass.TransportMessage;
import io.netty.channel.ChannelHandlerContext;
@Service
public class TalkToFriendTaskWebsocketHandler{
@Autowired
private WeChatMessageService weChatMessageService;
@Autowired
private WeChatAccountService weChatAccountService;
private final Logger log = LoggerFactory.getLogger(getClass());
/**
* 服务端、客服客户端发给设备的指令类消息
* @author wechatno:tangjinjinwx
* @param ctx
* @param vo
*/
public void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
try {
log.info(contentJsonStr);
TalkToFriendTaskMessage.Builder bd = TalkToFriendTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
TalkToFriendTaskMessage req = bd.build();
//消息记录数据库
saveMessage(weChatMessageService,weChatAccountService,req);
System.out.println(JsonFormat.printer().print(req));
//将消息转发送给手机客户端
ChannelHandlerContext cx= NettyConnectionUtil.getClientChannelHandlerContextByUserId(req.getWeChatId());
if(null != cx){
//.setMsgId(MsgIdBuilder.getId())// 发送给手机端的时候需要赋值,用于TalkToFriendTaskResultNotice中
MessageUtil.sendMsg(cx, EnumMsgType.TalkToFriendTask, NettyConnectionUtil.getNettyId(ctx), null, req);
// 告诉发送方消息已收到
MessageUtil.sendJsonMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
}else{
// 对方不在线
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.TargetNotOnline, Constant.ERROR_MSG_NOTONLINE);
}
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}
private static void saveMessage(WeChatMessageService weChatMessageService,WeChatAccountService weChatAccountService,TalkToFriendTaskMessage req ){
WeChatAccountInfo wechat = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId());
if(null != wechat){
try {
WeChatMessageInfo info = new WeChatMessageInfo();
String content = ByteStringToString.bytesToString(req.getContent(), "utf-8");
info.setCid(wechat.getCid());
info.setWechatId(req.getWeChatId());
info.setFriendId(req.getFriendId());
info.setContentType(req.getContentTypeValue());
info.setContent(content);//base64了 需要解码存储
weChatMessageService.insert(info);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
项目地址 http://www.wlkankan.cn/cate41/301.html
接口地址 http://www.yunlauncher.com/Blog/articles/119.html