ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

聚合微信聊天crm调用代码

2021-06-01 12:30:07  阅读:139  来源: 互联网

标签:contentJsonStr 调用 微信 req ctx vo asyncTaskService build crm


聚合微信聊天crm调用代码

/**
* 群发好友消息任务
*
* @author wechatno:tangjinjinwx
* @blog http://www.wlkankan.cn
*/
@Async
public void handleMsg(ChannelHandlerContext ctx, TransportMessage vo, String contentJsonStr) {
try {
log.debug(contentJsonStr);
WeChatGroupSendTaskMessage.Builder bd = WeChatGroupSendTaskMessage.newBuilder();
JsonFormat.parser().merge(contentJsonStr, bd);
WeChatGroupSendTaskMessage req = bd.build();
 
asyncTaskService.msgSend2Phone(ctx, req.getWeChatId(), EnumMsgType.WeChatGroupSendTask, vo, req);
} catch (Exception e) {
e.printStackTrace();
MessageUtil.sendJsonErrMsg(ctx, EnumErrorCode.InvalidParam, Constant.ERROR_MSG_DECODFAIL);
}
}
 

/**
* 群发群消息任务
* 接口参考http://www.wlkankan.cn/cate40/247.html
* @author wechatno:tangjinjinwx
*/
 
private static void talkMsgToFriend(TaskTimeInfo info, TimeTaskDetailsDao timeTaskDetailsDao,List<String> friends) {
for(String friend:friends){
ByteString byteString = ByteString.copyFromUtf8(info.getContent());
TalkToFriendTaskMessage.Builder build=TalkToFriendTaskMessage.newBuilder();
build.setWeChatId(info.getWechatId());
build.setFriendId(friend);
build.setContent(byteString);
build.setMsgId(MsgIdBuilder.getId());
if(info.getRestype()==1){//文本类型
build.setContentType(EnumContentType.Text);
}else if(info.getRestype()==2){//图片类型
build.setContentType(EnumContentType.Picture);
}else if(info.getRestype()==3){//语音类型
build.setContentType(EnumContentType.Voice);
}else if(info.getRestype()==4){//视频类型
build.setContentType(EnumContentType.Video);
}else if(info.getRestype()==6){//链接类型
build.setContentType(EnumContentType.Link);
}else if(info.getRestype()==13){//小程序
build.setContentType(EnumContentType.WeApp);
}
TalkToFriendTaskMessage msg = build.build();
if (null != msg) {
try {
String json = JsonFormat.printer().print(msg);
if(!StringUtils.isEmpty(json)){
TimeTaskDetails detail = new TimeTaskDetails();
detail.setTid(info.getId());
detail.setExecute_time(info.getExecute_time());
detail.setJson_content(json);
timeTaskDetailsDao.insert(detail);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

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);
        }
    }
 

TalkToFriendTaskMessage、TriggerChatRoomPushTaskMessage、TriggerCirclePushTaskMessage、PostSNSNewsTaskMessage、FriendAddTaskSetting、WeChatGroupSendTaskMessage等是封装类,个人博客有详细介绍。

标签:contentJsonStr,调用,微信,req,ctx,vo,asyncTaskService,build,crm
来源: https://blog.csdn.net/ruanquanf/article/details/117440592

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有