Ejemplo n.º 1
0
        /// <summary>
        /// 将消息中的东西替换为CQ码
        /// </summary>
        /// <param name="message">原始消息</param>
        /// <returns></returns>
        public static string Start(ReceiveMessage message)
        {
            string result = string.Empty;
            string msg    = message.CurrentPacket.Data.Content;

            switch (message.CurrentPacket.Data.MsgType)
            {
            case "TempSessionMsg":
                if (msg.Contains("图片"))
                {
                    var c = JsonConvert.DeserializeObject <PicMessage>(msg).FriendPic;
                    foreach (var item in c)
                    {
                        result += MakeCQImage(item);
                    }
                }
                else
                {
                    result = JObject.Parse(msg)["Content"].ToString();
                }

                break;

            case "AtMsg":
            {
                //at消息主要将消息中的at消息转变为CQ码
                //而@人名 中的人名格式可能会不同,不能直接用群名片替换,所以要从群成员列表寻找这个人
                //按备注->群名片->昵称的顺序,替换可能出现的名称
                TextMessage textMessage = JsonConvert.DeserializeObject <TextMessage>(msg);
                result = textMessage.Content;
                //从缓存寻找这个群
                GroupMemberList ls = MemberSave.Find(x => x.GroupUin == message.CurrentPacket.Data.FromGroupId);
                if (ls == null)         //未在缓存找到,将这个群加入缓存
                {
                    ls = JsonConvert.DeserializeObject <GroupMemberList>(
                        WebAPI.GetGroupMemberList(message.CurrentPacket.Data.FromGroupId));
                    MemberSave.Add(ls);
                }

                foreach (var item in textMessage.UserID)
                {
                    GroupMemberList.Memberlist mem = ls.MemberList.Where(x => x.MemberUin == item).First();
                    foreach (var pro in mem.GetType().GetProperties())
                    {
                        //将空文本变成null,方便后续??运算符
                        try
                        {
                            if (string.IsNullOrEmpty(pro.GetValue(mem).ToString()))
                            {
                                pro.SetValue(mem, null);
                            }
                        }
                        catch (NullReferenceException e)
                        {
                            pro.SetValue(mem, null);         //如果是null则会跳至catch块
                        }
                    }

                    string originStr = "@" + (mem.AutoRemark ?? mem.GroupCard ?? mem.NickName);
                    result = result.Replace(originStr, CQApi.CQCode_At(item).ToSendString());
                }

                break;
            }

            case "TextMsg":
                result = msg;
                break;

            case "PicMsg":
            {
                //图片消息是将图片消息的信息配置进image文件夹下的以MD5为名称的cqimg文件内
                PicMessage picMessage =
                    JsonConvert.DeserializeObject <PicMessage>(message.CurrentPacket.Data.Content);
                if (!Directory.Exists("data\\image"))
                {
                    Directory.CreateDirectory("data\\image");
                }
                result = picMessage.Content;
                if (picMessage.GroupPic != null)         //是群图片消息
                {
                    foreach (var item in picMessage.GroupPic)
                    {
                        result += MakeCQImage(item);
                    }
                }
                else         //是好友图片消息
                {
                    foreach (var item in picMessage.FriendPic)
                    {
                        result += MakeCQImage(item);
                    }
                }
                break;
            }

            case "VoiceMsg":
            {
                if (!Directory.Exists("data\\record\\"))
                {
                    Directory.CreateDirectory("data\\record\\");
                }
                JObject json = JObject.Parse(msg);
                string  url  = json["Url"].ToString();
                string  MD5  = GenerateMD5(url);
                string  path = "data\\record\\" + MD5 + ".silk";
                if (!File.Exists(path))
                {
                    IniConfig ini = new IniConfig(path);
                    ini.Object.Add(new ISection("record"));
                    ini.Object["record"]["url"] = url;
                    ini.Save();
                }

                result = CQApi.CQCode_Record(MD5 + ".silk").ToString();
                break;
            }

            case "RedBagMsg":
            {
                string title = JObject.Parse(msg)["Content"]?.ToString();
                result = $"[CQ:hb,title={title}]";
                break;
            }

            case "XmlMsg":
            {
                result = JObject.Parse(msg)["Content"].ToString();
                var xml   = new XmlDocument();
                int index = result.IndexOf("</msg>");
                result = result.Substring(0, index + "</msg>".Length);
                xml.LoadXml(result);
                var root = xml.FirstChild.NextSibling;
                foreach (XmlAttribute item in root.Attributes)
                {
                    if (item.Name == "actionData" && item.Value.Contains("group:"))
                    {
                        result = $"[CQ:contact,id={item.Value.Replace("group:", "")},type=group]";
                        break;
                    }
                    else if (item.Name == "actionData" && item.Value.Contains("AppCmd://OpenContactInfo/?uin"))
                    {
                        result = $"[CQ:contact,id={item.Value.Replace("AppCmd://OpenContactInfo/?uin=", "")},type=qq]";
                        break;
                    }
                    else if (item.Name == "url" && item.Value.Contains("y.music.163.com"))
                    {
                        int musicid = Convert.ToInt32(Regex.Replace(item.Value, "https:\\/\\/y.music.163.com\\/m/song\\/([0-9]*)\\/\\?userid=([0-9]*)", "$1"));
                        int userid  = Convert.ToInt32(Regex.Replace(item.Value, "https:\\/\\/y.music.163.com\\/m/song\\/([0-9]*)\\/\\?userid=([0-9]*)", "$2"));
                        result = $"[CQ:music,type=163,id={musicid},userid={userid}]";
                        break;
                    }
                }
                if (!result.StartsWith("[CQ:"))
                {
                    result = $"[CQ:rich,content={result.Replace("\n","").Replace("\r","")}]";
                }
                break;
            }

            case "JsonMsg":
            {
                result = JObject.Parse(msg)["Content"].ToString();
                result = result.Substring(result.IndexOf("{\"app\":\""));
                var json = JObject.Parse(result);
                if (json["meta"]["music"] != null)
                {
                    var musicroot = json["meta"]["music"];
                    int musicid   = Convert.ToInt32(Regex.Replace(musicroot["jumpUrl"].ToString(), "https:\\/\\/y.music.163.com\\/m/song\\/([0-9]*)\\/\\?userid=([0-9]*)", "$1"));
                    int userid    = Convert.ToInt32(Regex.Replace(musicroot["jumpUrl"].ToString(), "https:\\/\\/y.music.163.com\\/m/song\\/([0-9]*)\\/\\?userid=([0-9]*)", "$2"));
                    result = $"[CQ:music,type=163,id={musicid},userid={userid},title={musicroot["title"]},desc={musicroot["desc"]}]";
                }
                else
                {
                    result = $"[CQ:rich,title={json["prompt"]}]";
                }
                break;
            }
            }

            result = Regex.Replace(result, "\\[表情(\\d*)\\]", "[CQ:face,id=$1]"); //处理QQ表情信息
            //处理emoji消息
            foreach (var a in result)
            {
                //UTF-8下,大部分的emoji都是以\ud83d开头
                if (a == '\ud83d' && result.IndexOf(a) != result.Length - 1)
                {
                    //取这个emoji
                    string        str   = a.ToString() + result[result.IndexOf(a) + 1].ToString();
                    UTF32Encoding enc   = new UTF32Encoding(true, false);
                    byte[]        bytes = enc.GetBytes(str); //转换字节数组
                    //使用BitConvert将字节数组转换为16进制,之后转换为10进制即可
                    result = result.Replace(str,
                                            CQApi.CQCode_Emoji(Convert.ToInt32(BitConverter.ToString(bytes).Replace("-", ""), 16))
                                            .ToString());
                    break;
                }
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 将消息中的东西替换为CQ码
        /// </summary>
        /// <param name="message">原始消息</param>
        /// <returns></returns>
        public static string Start(ReceiveMessage message)
        {
            string result = string.Empty;
            string msg    = message.CurrentPacket.Data.Content;

            switch (message.CurrentPacket.Data.MsgType)
            {
            case "AtMsg":
            {
                TextMessage textMessage = JsonConvert.DeserializeObject <TextMessage>(msg);
                result = textMessage.Content;
                GroupMemberList ls = JsonConvert.DeserializeObject <GroupMemberList>(WebAPI.GetGroupMemberList(message.CurrentPacket.Data.FromGroupId));
                foreach (var item in textMessage.UserID)
                {
                    GroupMemberList.Memberlist mem = ls.MemberList.Where(x => x.MemberUin == item).First();
                    foreach (var pro in mem.GetType().GetProperties())
                    {
                        if (string.IsNullOrEmpty(pro.GetValue(mem).ToString()))
                        {
                            pro.SetValue(mem, null);
                        }
                    }
                    string originStr = "@" + (mem.AutoRemark ?? mem.GroupCard ?? mem.NickName);
                    result = result.Replace(originStr, CQApi.CQCode_At(item).ToSendString());
                }
                break;
            }

            case "TextMsg":
                result = msg;
                break;

            case "PicMsg":
            {
                PicMessage picMessage = JsonConvert.DeserializeObject <PicMessage>(message.CurrentPacket.Data.Content);
                if (!Directory.Exists("data\\image"))
                {
                    Directory.CreateDirectory("data\\image");
                }
                result = picMessage.Content;
                foreach (var item in picMessage.GroupPic)
                {
                    string md5  = GenerateMD5(item.FileMd5).ToUpper();
                    string path = $"data\\image\\{md5}.cqimg";
                    if (!File.Exists(path))
                    {
                        IniConfig ini = new IniConfig(path);
                        ini.Object.Add(new ISection("image"));
                        ini.Object["image"]["md5"]  = item.FileMd5;
                        ini.Object["image"]["size"] = item.FileSize;
                        ini.Object["image"]["url"]  = item.Url;
                        ini.Save();
                    }
                    result += CQApi.CQCode_Image(md5);
                }
                break;
            }

            case "VoiceMsg":
            {
                if (!Directory.Exists("data\\record\\"))
                {
                    Directory.CreateDirectory("data\\record\\");
                }
                JObject json = JObject.Parse(msg);
                string  url  = json["Url"].ToString();
                string  MD5  = GenerateMD5(url);
                string  path = "data\\record\\" + MD5 + ".silk";
                if (!File.Exists(path))
                {
                    IniConfig ini = new IniConfig(path);
                    ini.Object.Add(new ISection("record"));
                    ini.Object["record"]["url"] = url;
                    ini.Save();
                }
                result = CQApi.CQCode_Record(MD5 + ".silk").ToString();
                break;
            }
            }
            result = Regex.Replace(result, "\\[表情(\\d*)\\]", "[CQ:face,id=$1]");
            foreach (var a in result)
            {
                if (a == '\ud83d' && result.IndexOf(a) != result.Length - 1)
                {
                    string str  = a.ToString() + result[result.IndexOf(a) + 1].ToString();
                    string text = "";
                    foreach (var item in Encoding.UTF32.GetBytes(str))
                    {
                        text += item.ToString("x");
                    }
                    result = result.Replace(str, CQApi.CQCode_Emoji(Convert.ToInt32(text, 16)).ToString());
                    break;
                }
            }
            return(result);
        }