예제 #1
0
        protected override void OnStartListeningToHub()
        {
            _hubConnection.On <ReceivedMessageSignalArgs>(_RECEIVE_NEW_GROUP_MESSAGE, (args) => {
                GroupMessageReceived.Invoke(this, args);
            });

            _hubConnection.On <ReceivedMessageSignalArgs>(_RECEIVE_NEW_TEAM_MESSAGE, (args) => {
                TeamMessageReceived.Invoke(this, args);
            });

            _hubConnection.On <ReceivedMessageSignalArgs>(_RECEIVE_NEW_FAMILY_MESSAGE, (args) => {
                FamilyMessageReceived.Invoke(this, args);
            });

            _hubConnection.On <object>(_UNREAD_GROUP_MESSAGES, (args) => {
                try {
                    ResolvedUnreadGroupMessagesSignalArgs resolvedUnreadGroupMessagesSignalArgs = JsonConvert.DeserializeObject <ResolvedUnreadGroupMessagesSignalArgs>(args.ToString());
                    RefreshedUnreadGroupingMessages.Invoke(this, resolvedUnreadGroupMessagesSignalArgs);
                }
                catch (Exception exc) {
                    Debugger.Break();

                    throw new InvalidOperationException("GroupMessagingService on SignalR UnreadedMessages", exc);
                }
            });
        }
예제 #2
0
        internal int OnGroupMessageReceived(int subType, int msgId, long fromGroup, long fromQQ, string fromAnonymous, string msg, int font)
        {
            //subType恒为1
            var eventArgs = new GroupMessageReceivedEventArgs(
                msgId, fromGroup, fromQQ, fromAnonymous, msg, font);

            try
            {
                GroupMessageReceived?.Invoke(this, eventArgs);
            }
            catch (MessageInterceptedException)
            {
                Debug.Assert(eventArgs.ReturnValue == 1);
            }
            return(eventArgs.ReturnValue);
        }
예제 #3
0
        // 拉取消息
        private void PollMessage()
        {
            // Logger.Debug(DateTime.Now + " 开始接收消息");

            var r = new JObject
            {
                { "ptwebqq", Ptwebqq },
                { "clientid", ClientId },
                { "psessionid", Psessionid },
                { "key", "" }
            };

            var response = Client.Post(ApiUrl.PollMessage, r);
            var array    = GetResponseJson(response)["result"] as JArray;

            for (var i = 0; array != null && i < array.Count; i++)
            {
                var message = (JObject)array[i];
                var type    = message["poll_type"].Value <string>();
                switch (type)
                {
                case "message":
                    var fmsg = message["value"].ToObject <FriendMessage>();
                    fmsg.Client = this;
                    FriendMessageReceived?.Invoke(this, fmsg);
                    break;

                case "group_message":
                    var gmsg = message["value"].ToObject <GroupMessage>();
                    gmsg.Client = this;
                    GroupMessageReceived?.Invoke(this, gmsg);
                    break;

                case "discu_message":
                    var dmsg = message["value"].ToObject <DiscussionMessage>();
                    dmsg.Client = this;
                    DiscussionMessageReceived?.Invoke(this, dmsg);
                    break;

                default:
                    Logger.Warn("意外的消息类型:" + type);
                    break;
                }
            }
        }
예제 #4
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);
        }