Example #1
0
        public void Json_Received(object sender, JsonReceivedEventArgs args)
        {
            string json = args.JsonString;

            dynamic obj = JsonConvert.DeserializeObject(json);

            // 判断post类别
            try
            {
                if (obj.post_type == "message")
                {
                    // 私聊
                    if (obj.message_type == "private")
                    {
                        PrivateMsg parsedObj = JsonConvert.DeserializeObject <PrivateMsg>(json);
                        Core.ReceiveMessage(parsedObj);
                    }

                    // 群聊
                    else if (obj.message_type == "group")
                    {
                        GroupMsg parsedObj = JsonConvert.DeserializeObject <GroupMsg>(json);
                        Core.ReceiveMessage(parsedObj);
                    }

                    // 讨论组
                    else if (obj.message_type == "discuss")
                    {
                        DiscussMsg parsedObj = JsonConvert.DeserializeObject <DiscussMsg>(json);
                        Core.ReceiveMessage(parsedObj);
                    }
                }
                else if (obj.post_type == "notice")
                {
                    // 群文件上传
                    if (obj.notice_type == "group_upload")
                    {
                        GroupFileUpload parsedObj = JsonConvert.DeserializeObject <GroupFileUpload>(json);
                        // TODO
                    }
                    // 群管理员变动
                    else if (obj.notice_type == "group_admin")
                    {
                        GroupAdminChange parsedObj = JsonConvert.DeserializeObject <GroupAdminChange>(json);
                        // TODO
                    }
                    // 群成员增加/减少
                    else if (obj.notice_type == "group_decrease" || obj.notice_type == "group_increase")
                    {
                        GroupMemberChange parsedObj = JsonConvert.DeserializeObject <GroupMemberChange>(json);
                        // TODO
                    }
                    // 好友添加
                    else if (obj.notice_type == "friend_add")
                    {
                        FriendAdd parsedObj = JsonConvert.DeserializeObject <FriendAdd>(json);
                        // TODO
                    }
                }
                else if (obj.post_type == "request")
                {
                    // 加好友请求
                    if (obj.request_type == "friend")
                    {
                        FriendRequest parsedObj = JsonConvert.DeserializeObject <FriendRequest>(json);
                        // TODO
                    }
                    // 加群请求/邀请
                    else if (obj.request_type == "group")
                    {
                        GroupInvite parsedObj = JsonConvert.DeserializeObject <GroupInvite>(json);
                        // TODO
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
            }
        }
Example #2
0
        public bool RawObject_Received(object rawObject)
        {
            var handled = false;

            if (!(rawObject is string rawJson))
            {
                return(handled);
            }

            dynamic obj = JsonConvert.DeserializeObject(rawJson);

            try
            {
                if (obj.post_type == "message")
                {
                    if (obj.message_type == "private") // 私聊
                    {
                        CoolQPrivateMessageApi parsedObj = JsonConvert.DeserializeObject <CoolQPrivateMessageApi>(rawJson);
                        var arg = new MessageEventArgs(parsedObj);
                        PrivateMessageReceived?.Invoke(this, arg);
                        MessageReceived?.Invoke(this, arg);
                    }
                    else if (obj.message_type == "group") // 群聊
                    {
                        CoolQGroupMessageApi parsedObj = JsonConvert.DeserializeObject <CoolQGroupMessageApi>(rawJson);
                        var arg = new MessageEventArgs(parsedObj);
                        DiscussMessageReceived?.Invoke(this, arg);
                        MessageReceived?.Invoke(this, arg);
                    }
                    else if (obj.message_type == "discuss") // 讨论组
                    {
                        CoolQDiscussMessageApi parsedObj = JsonConvert.DeserializeObject <CoolQDiscussMessageApi>(rawJson);
                        var arg = new MessageEventArgs(parsedObj);
                        GroupMessageReceived?.Invoke(this, arg);
                        MessageReceived?.Invoke(this, arg);
                    }
                }
                else
                {
                    EventReceived?.Invoke(this, new EventEventArgs(rawJson));
                    if (obj.post_type == "notice")
                    {
                        if (obj.notice_type == "group_upload") // 群文件上传
                        {
                            GroupFileUpload parsedObj = JsonConvert.DeserializeObject <GroupFileUpload>(rawJson);
                            var             arg       = new NoticeEventArgs(parsedObj);
                            GroupFileUploaded?.Invoke(this, arg);
                            Noticed?.Invoke(this, arg);
                        }
                        else if (obj.notice_type == "group_admin") // 群管理员变动
                        {
                            GroupAdminChange parsedObj = JsonConvert.DeserializeObject <GroupAdminChange>(rawJson);
                            var arg = new NoticeEventArgs(parsedObj);
                            GroupAdminChanged?.Invoke(this, arg);
                            Noticed?.Invoke(this, arg);
                        }
                        else if (obj.notice_type == "group_decrease" || obj.notice_type == "group_increase") // 群成员增加/减少
                        {
                            GroupMemberChange parsedObj = JsonConvert.DeserializeObject <GroupMemberChange>(rawJson);
                            var arg = new NoticeEventArgs(parsedObj);
                            Noticed?.Invoke(this, arg);
                            GroupMemberChanged?.Invoke(this, arg);
                        }
                        else if (obj.notice_type == "friend_add") // 好友添加
                        {
                            FriendAdd parsedObj = JsonConvert.DeserializeObject <FriendAdd>(rawJson);
                            var       arg       = new NoticeEventArgs(parsedObj);
                            FriendAdded?.Invoke(this, arg);
                            Noticed?.Invoke(this, arg);
                        }
                    }
                    else if (obj.post_type == "request")
                    {
                        if (obj.request_type == "friend") // 加好友请求
                        {
                            FriendRequest parsedObj = JsonConvert.DeserializeObject <FriendRequest>(rawJson);
                            var           arg       = new RequestEventArgs(parsedObj);
                            FriendRequested?.Invoke(this, arg);
                            Requested?.Invoke(this, arg);

                            //// TODO,临时
                            //CqApi.SendPrivateMessage("2241521134",
                            //    string.Format("{0} ({1})邀请加我为好友",
                            //        CqApi.GetStrangerInfo(parsedObj.UserId.ToString()).Data?.Nickname, parsedObj.UserId));
                        }
                        else if (obj.request_type == "group") // 加群请求/邀请
                        {
                            GroupInvite parsedObj = JsonConvert.DeserializeObject <GroupInvite>(rawJson);
                            var         arg       = new RequestEventArgs(parsedObj);
                            GroupRequested?.Invoke(this, arg);
                            Requested?.Invoke(this, arg);

                            //// TODO,临时
                            //if (parsedObj.SubType == "invite")
                            //{
                            //    CqApi.SendPrivateMessage("2241521134",
                            //        string.Format("{0} ({1})邀请我加入群{2}",
                            //            CqApi.GetStrangerInfo(parsedObj.UserId.ToString()).Data?.Nickname, parsedObj.UserId,
                            //            parsedObj.GroupId));
                            //}
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorOccured?.Invoke(this, new ExceptionEventArgs(null, ex));
            }

            return(handled);
        }
Example #3
0
        public void Json_Received(object sender, JsonReceivedEventArgs args)
        {
            string json = args.JsonString;

            dynamic obj = JsonConvert.DeserializeObject(json);

            // 判断post类别
            try
            {
                if (obj.post_type == "message")
                {
                    // 私聊
                    if (obj.message_type == "private")
                    {
                        PrivateMsg parsedObj = JsonConvert.DeserializeObject <PrivateMsg>(json);
                        Core.ReceiveMessage(parsedObj);
                    }

                    // 群聊
                    else if (obj.message_type == "group")
                    {
                        GroupMsg parsedObj = JsonConvert.DeserializeObject <GroupMsg>(json);
                        Core.ReceiveMessage(parsedObj);
                    }

                    // 讨论组
                    else if (obj.message_type == "discuss")
                    {
                        DiscussMsg parsedObj = JsonConvert.DeserializeObject <DiscussMsg>(json);
                        Core.ReceiveMessage(parsedObj);
                    }
                }
                else if (obj.post_type == "notice")
                {
                    // 群文件上传
                    if (obj.notice_type == "group_upload")
                    {
                        GroupFileUpload parsedObj = JsonConvert.DeserializeObject <GroupFileUpload>(json);
                        // TODO
                    }
                    // 群管理员变动
                    else if (obj.notice_type == "group_admin")
                    {
                        GroupAdminChange parsedObj = JsonConvert.DeserializeObject <GroupAdminChange>(json);
                        // TODO
                    }
                    // 群成员增加/减少
                    else if (obj.notice_type == "group_decrease" || obj.notice_type == "group_increase")
                    {
                        GroupMemberChange parsedObj = JsonConvert.DeserializeObject <GroupMemberChange>(json);
                        // TODO
                    }
                    // 好友添加
                    else if (obj.notice_type == "friend_add")
                    {
                        FriendAdd parsedObj = JsonConvert.DeserializeObject <FriendAdd>(json);
                        // TODO
                    }
                }
                else if (obj.post_type == "request")
                {
                    // 加好友请求
                    if (obj.request_type == "friend")
                    {
                        FriendRequest parsedObj = JsonConvert.DeserializeObject <FriendRequest>(json);
                        // TODO,临时
                        CqApi.SendPrivateMessage("2241521134",
                                                 string.Format("{0} ({1})邀请加我为好友",
                                                               CqApi.GetStrangerInfo(parsedObj.UserId.ToString()).Data?.Nickname, parsedObj.UserId));
                    }
                    // 加群请求/邀请
                    else if (obj.request_type == "group")
                    {
                        GroupInvite parsedObj = JsonConvert.DeserializeObject <GroupInvite>(json);
                        // TODO,临时
                        if (parsedObj.SubType == "invite")
                        {
                            CqApi.SendPrivateMessage("2241521134",
                                                     string.Format("{0} ({1})邀请我加入群{2}",
                                                                   CqApi.GetStrangerInfo(parsedObj.UserId.ToString()).Data?.Nickname, parsedObj.UserId,
                                                                   parsedObj.GroupId));
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
            }
        }