个人微信api接口调用-给微信好友发消息、接收微信好友发来的聊天消息
个人微信api接口调用-给微信好友发消息、接收微信好友发来的聊天消息
给微信好友发消息
/**
* 给微信好友发消息
* @author wechatno:tangjinjinwx
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
TalkToFriendTaskMessage.Builder bd = TalkToFriendTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
TalkToFriendTaskMessage req = bd.build();
// 消息记录数据库
asyncTaskService.savePcMessage(req);
// 将消息转发送给手机客户端
asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TalkToFriendTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}
微信好友发来聊天消息通知
/**
* 微信好友发来聊天消息通知
* @author wechatno:tangjinjinwx
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo) {
try {
FriendTalkNoticeMessage req = vo.getContent().unpack(FriendTalkNoticeMessage.class);
log.debug(JsonFormat.printer().print(req));
//拦截消息
asyncTaskService.msgAopTask(ctx,req,vo.getAccessToken(), vo.getId());
//消息转发到pc端
asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendTalkNotice, req);
// 告诉客户端消息已收到
MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
}
}
以上代码依赖微信开发SDK,微信api接口方案咨询微信happybabby110
拓展阅读:http://www.wlkankan.cn/cate41/301.html