java调用个人微信api接口实现收发消息发朋友圈
個人微信api接口,java調用個人微信api接口實現收發消息發朋友圈
1、微信好友收發消息
? ? ? ? /**
?? ? * 給微信好友發消息
?? ? * @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));
?? ??? ??? ?
?? ??? ??? ?log.debug(LocalDateTime.now()+" 微信好友發來聊天消息 ?對應的線程名: "+Thread.currentThread().getName());
?? ??? ??? ? ?
? ? ? ? ? ? //攔截消息
?? ??? ??? ?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);
? ? ? ? ?? ?
? ? ? ? ?? ?WxAccountInfo account = weChatAccountService.findWeChatAccountInfoByWeChatId(req.getWeChatId());
?? ??? ??? ?//消息記錄數據庫
?? ??? ??? ?if (null != account){
?? ??? ??? ??? ?asyncTaskService.saveMessage(account, req);
?? ??? ? ? ?}
?? ??? ??? ??
?? ??? ?} catch (Exception e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), e.getMessage());
?? ??? ?}
?? ?}
2、觸發手機推送微信好友列表及返回
? ? ? ? /**
?? ? * 觸發手機推送微信好友列表
?? ? * @author wechatno:tangjinjinwx
?? ? * @blog http://www.wlkankan.cn
?? ? */
?? ?@Async
? ? public ?void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
? ? ? ? try {
? ? ? ? ?? ?log.debug(contentJsonStr);
? ? ? ? ?? ?TriggerFriendPushTaskMessage.Builder bd = TriggerFriendPushTaskMessage.newBuilder();
? ? ? ? ?? ?JsonFormat.parser().merge(contentJsonStr, bd);
? ? ?? ??? ?TriggerFriendPushTaskMessage req = bd.build();
? ? ? ? ?? ?//TriggerFriendPushTaskMessage req = vo.getContent().unpack(TriggerFriendPushTaskMessage.class);
? ? ? ? ?? ?
? ? ? ? ?? ?//將消息轉發送給手機客戶端
? ? ? ? ?? ?asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TriggerFriendPushTask, 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 {
?? ??? ??? ?FriendPushNoticeMessage req = vo.getContent().unpack(FriendPushNoticeMessage.class);
?? ??? ??? ?log.debug(JsonFormat.printer().print(req));
?? ??? ??? ?// 把消息轉發給pc端
?? ??? ??? ?asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendPushNotice, req);
?? ??? ??? ??
?? ??? ??? ?// 異步保存到數據庫
?? ??? ??? ?asyncTaskService.friendListSave(req);
?? ??? ??? ??
?? ??? ??? ?// 告訴客戶端消息已收到
?? ??? ??? ?MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
?? ??? ?} catch (Exception e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ??? ?MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam, vo.getId(), Constant.ERROR_MSG_DECODFAIL);
?? ??? ?}
?? ?}
3、觸發推送微信群聊列表及返回
?? ?/**
?? ? * 觸發推送微信群聊列表
?? ? * @author wechatno:tangjinjinwx
?? ? * @blog http://www.wlkankan.cn
?? ? */
?? ?@Async
?? ?public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
?? ??? ?try {
?? ??? ??? ?log.debug(contentJsonStr);
?? ??? ??? ?TriggerChatRoomPushTaskMessage.Builder bd = TriggerChatRoomPushTaskMessage.newBuilder();
?? ??? ??? ?JsonFormat.parser().merge(contentJsonStr, bd);
?? ??? ??? ?TriggerChatRoomPushTaskMessage req = bd.build();
?? ??? ??? ?// 將消息轉發送給手機客戶端
?? ??? ??? ?asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TriggerChatroomPushTask, 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 {
? ? ? ? ?? ?ChatRoomPushNoticeMessage req = vo.getContent().unpack(ChatRoomPushNoticeMessage.class);
? ? ? ? ?? ?log.debug(JsonFormat.printer().print(req));
?? ??? ??? ?
?? ??? ??? ?asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.ChatroomPushNotice, req);
?? ??? ??? ??
?? ??? ??? ?asyncTaskService.qunListSave(req);
?? ??? ??? ? ?
?? ??? ??? ?// 告訴客戶端消息已收到
?? ??? ??? ?MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
? ? ? ? }
? ? }
4、推送微信朋友圈、發朋友圈
? ? ? ? /**
?? ? * 觸發推送朋友圈列表
?? ? * @author wechatno:tangjinjinwx?
?? ? * startTime傳秒
?? ? * @blog http://www.wlkankan.cn
?? ? */
?? ?@Async
?? ?public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
?? ??? ?try {
?? ??? ??? ?log.debug(contentJsonStr);
?? ??? ??? ?TriggerCirclePushTaskMessage.Builder bd = TriggerCirclePushTaskMessage.newBuilder();
?? ??? ??? ?JsonFormat.parser().merge(contentJsonStr, bd);
?? ??? ??? ?TriggerCirclePushTaskMessage req = bd.build();
?? ??? ??? ?// TriggerCirclePushTaskMessage req =
?? ??? ??? ?// vo.getContent().unpack(TriggerCirclePushTaskMessage.class);
?? ??? ??? ?// 將消息轉發送給手機客戶端
?? ??? ??? ?asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.TriggerCirclePushTask, 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 {
? ? ? ? ?? ?CirclePushNoticeMessage req = vo.getContent().unpack(CirclePushNoticeMessage.class);
? ? ? ? ?? ?log.debug(JsonFormat.printer().print(req));
? ? ? ? ?? ?//把消息轉發給pc端
? ? ?? ??? ?asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.CirclePushNotice, req);
? ? ?? ??? ?
? ? ?? ??? ?//保存朋友圈信息
? ? ?? ??? ?asyncTaskService.asyncSaveCircleMsg(req, circleService, weChatContactService);
? ? ?? ??? ?
? ? ? ? ?? ?//告訴客戶端消息已收到
? ? ?? ??? ?MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
? ? ? ? }
? ? }
? ? /**
?? ? * 發微信朋友圈
?? ? * @author wechatno:tangjinjinwx
?? ? * @blog http://www.wlkankan.cn
?? ? */
? ? @Async
? ? public ?void handleMsg(ChannelHandlerContext ctx,TransportMessage vo, String contentJsonStr) {
? ? ? ? try {
? ? ? ? ?? ?log.debug(contentJsonStr);
? ? ? ? ?? ?PostSNSNewsTaskMessage.Builder bd = PostSNSNewsTaskMessage.newBuilder();
? ? ? ? ?? ?JsonFormat.parser().merge(contentJsonStr, bd);
? ? ?? ??? ?PostSNSNewsTaskMessage req = bd.build();
? ? ? ? ?? ?//PostSNSNewsTaskMessage req = vo.getContent().unpack(PostSNSNewsTaskMessage.class);
? ? ? ? ?? ? ?
? ? ? ? ?? ?asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.PostSNSNewsTask, vo, req);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
? ? ? ? }
? ? }
5、加好友及通過好友請求
? ? ? ? /**?
?? ? * 微信自動添加好友
?? ? * @author wechatno:tangjinjinwx
? ? ? ? ?* @blog http://www.wlkankan.cn
?? ? */
?? ?@Async
? ? public ?void handleMsg(ChannelHandlerContext ctx ,TransportMessage vo, String contentJsonStr) {
? ? ? ? try {
? ? ? ? ?? ?log.debug(contentJsonStr);
? ? ? ? ?? ?FriendAddTaskSetting ?req = ?JSON.parseObject(contentJsonStr,FriendAddTaskSetting.class);
? ? ? ? ?? ?if(null != req){
? ? ? ? ?? ??? ?String resp ="fail";
? ? ? ? ?? ??? ??
? ? ? ? ?? ??? ?resp = friendAddTaskService.savePcTask(req);
? ? ? ? ?? ??? ??
? ? ? ? ?? ??? ?//3、告訴PC客戶端消息已收到
? ? ? ? ?? ??? ?MessageUtil.sendCustomJsonMsg(ctx, "AutoFriendAddTaskResp", resp);
? ? ? ? ?? ??? ?
? ? ? ? ?? ?}?
? ? ? ? } 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 {
? ? ? ? ?? ?FriendAddNoticeMessage req = vo.getContent().unpack(FriendAddNoticeMessage.class);
? ? ? ? ?? ??
? ? ?? ??? ?//把消息轉發給pc端
? ? ?? ??? ?asyncTaskService.msgSend2pc(req.getWeChatId(), EnumMsgType.FriendAddNotice, req);
? ? ?? ??? ?
? ? ?? ??? ?//保存新增好友
? ? ?? ??? ?asyncTaskService.saveFriendAddContactinfo(req);
? ? ?? ??? ?
? ? ?? ??? ?//告訴客戶端消息已收到
? ? ?? ??? ?MessageUtil.sendMsg(ctx, EnumMsgType.MsgReceivedAck, vo.getAccessToken(), vo.getId(), null);
? ? ?? ??? ? ?
?? ??? ??? ??
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? MessageUtil.sendErrMsg(ctx, EnumErrorCode.InvalidParam,vo.getId(), Constant.ERROR_MSG_DECODFAIL);
? ? ? ? }
? ? }
總結
以上是生活随笔為你收集整理的java调用个人微信api接口实现收发消息发朋友圈的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux下用nvidia显卡实现硬解码
- 下一篇: vsCode搜索中文, 正则匹配