示例#1
0
        public static void SendMsg(QQUser user)
        {
            Console.WriteLine("sendMsg " + user);

            // 组装QQ消息发送回去
            var sendMsg = new QQMsg
            {
                To   = user,
                Type = QQMsgType.BUDDY_MSG
            };

            sendMsg.AddContentItem(new TextItem("hello from iqq")); // 添加文本内容
            sendMsg.AddContentItem(new FaceItem(0));                // QQ id为0的表情
            sendMsg.AddContentItem(new FontItem());                 // 使用默认字体
            mClient.SendMsg(sendMsg, null);                         // 调用接口发送消息
        }
示例#2
0
        private QQNotifyListener GetListener()
        {
            return(async(client, notifyEvent) =>
            {
                try
                {
                    switch (notifyEvent.Type)
                    {
                    case QQNotifyEventType.LoginSuccess:
                        {
                            client.Logger.LogInformation("登录成功");
                            break;
                        }

                    case QQNotifyEventType.GroupMsg:
                        {
                            var revMsg = (QQMsg)notifyEvent.Target;
                            if (revMsg.From.QQ.IsDefault())
                            {
                                await client.GetUserQQ(revMsg.From).WhenFinalEvent().ConfigureAwait(false);
                            }
                            client.Logger.LogInformation($"群[{revMsg.Group.Name}]-好友[{revMsg.From.Nickname}]:{revMsg.GetText()}");
                            break;
                        }

                    case QQNotifyEventType.ChatMsg:
                        {
                            var revMsg = (QQMsg)notifyEvent.Target;
                            if (revMsg.From.QQ.IsDefault())
                            {
                                await client.GetUserQQ(revMsg.From).WhenFinalEvent().ConfigureAwait(false);
                            }
                            client.Logger.LogInformation($"好友[{revMsg.From.Nickname}]:{revMsg.GetText()}");
                            if (chkUseRobot.Checked)
                            {
                                Thread.Sleep(3000);
                                var msgReply = new QQMsg()
                                {
                                    Type = QQMsgType.BUDDY_MSG,
                                    To = revMsg.From,
                                    From = client.Account,
                                    Date = DateTime.Now,
                                };
                                var replyEvent = await client.GetRobotReply(revMsg, RobotType.Turing).WhenFinalEvent();

                                if (replyEvent.Type == QQActionEventType.EvtOK)
                                {
                                    var str = (string)replyEvent.Target;
                                    var text = new TextItem($"{str} --来自机器人");
                                    msgReply.AddContentItem(text);
                                    msgReply.AddContentItem(new FontItem()); // 使用默认字体,不加这句对方收不到信息
                                    var result = await client.SendMsg(msgReply).WhenFinalEvent().ConfigureAwait(false);

                                    if (result.Type == QQActionEventType.EvtOK)
                                    {
                                        client.Logger.LogInformation($"自动回复给[{revMsg.From.Nickname}]:{text.ToText()}");
                                    }
                                    else
                                    {
                                        client.Logger.LogWarning($"自动回复给[{revMsg.From.Nickname}]发送失败");
                                    }
                                }
                                else
                                {
                                    client.Logger.LogWarning("获取机器人回复失败");
                                }
                            }
                            break;
                        }

                    case QQNotifyEventType.QrcodeInvalid:
                        {
                            client.Logger.LogWarning("二维码已失效");
                            CancelLogin();
                            break;
                        }

                    case QQNotifyEventType.QrcodeReady:
                        {
                            client.Logger.LogWarning("二维码获取成功,请用手机qq扫码登录");
                            var verify = (Image)notifyEvent.Target;
                            pbQRCode.InvokeIfRequired(() =>
                            {
                                pbQRCode.Image = verify;
                            });
                            break;
                        }

                    case QQNotifyEventType.KickOffline:
                        {
                            client.Logger.LogInformation("被踢下线");
                            break;
                        }

                    case QQNotifyEventType.ShakeWindow:
                        {
                            var buddy = (QQBuddy)notifyEvent.Target;
                            if (buddy.QQ.IsDefault())
                            {
                                await client.GetUserQQ(buddy).WhenFinalEvent().ConfigureAwait(false);
                            }
                            client.Logger.LogInformation($"[{buddy.ShowName}]发来抖动屏幕");
                            break;
                        }

                    case QQNotifyEventType.BuddyInput:
                    case QQNotifyEventType.BuddyStatusChange:
                    case QQNotifyEventType.QrcodeSuccess:
                    case QQNotifyEventType.NetError:
                    case QQNotifyEventType.UnknownError:
                        break;

                    default:
                        client.Logger.LogInformation(notifyEvent.Type + ", " + notifyEvent.Target);
                        break;
                    }
                }
                catch (Exception ex)
                {
                    client.Logger.LogError(ex.ToString());
                }
            });
        }