码迷,mamicode.com
首页 > Web开发 > 详细

C# WebQQ协议群发机器人(二)

时间:2014-08-30 10:00:29      阅读:328      评论:0      收藏:0      [点我收藏+]

标签:机器人   webqq   c#   自动发消息   

本文出处http://blog.csdn.net/zhujunxxxxx/ 如需转载请注明出处!

接着上一篇http://blog.csdn.net/zhujunxxxxx/article/details/38931287我讲的内容

上面实现的都是与群操作相关的

接下来是获取好友信息的方法

/// <summary>
        /// 获取好友列表信息
        /// </summary>
        public void GetFriendList()
        {
            Dictionary<string, Object> r = new Dictionary<string, Object>();
            r.Add("h", "hello");
            r.Add("hash", this.hash);
            r.Add("vfwebqq", this.vfwebqq);
            Dictionary<string, Object> data = new Dictionary<string, Object>();
            data.Add("r", JsonConvert.SerializeObject(r));
            string postdata = Tool.MakePostData(data);

            HttpItem item = new HttpItem()
            {
                URL = "http://s.web2.qq.com/api/get_user_friends2",
                Method = "POST",
                IsToLower = false,
                Timeout = 100000,
                Postdata = postdata,
                Host = HOST[1],
                Referer = REFERER[1],
                UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",
                ResultType = ResultType.String
            };
            HttpResult result = http.GetHtml(item);
            JObject ret = (JObject)JsonConvert.DeserializeObject(result.Html);
            JObject retjson = (JObject)ret["result"];
            //有备注的好友
            foreach (JToken jk in retjson["marknames"])
            {
                this.friendlist.Add(jk["uin"].ToString(), jk["markname"].ToString());
            }
            //无备注名的好友
            foreach (JToken jk in retjson["info"])
            {
                if (!this.friendlist.ContainsKey(jk["uin"].ToString()))
                    this.friendlist.Add(jk["uin"].ToString(), jk["nick"].ToString());
            }
        }


这个方法可以获取到好友的信息,分为两部分一部分是有备注名的好友信息,一部分是无备注名的好友信息

把这部分信息保存下来以后有用

获取一个具体好友的信息(自己的信息也是通过这个获取)

 public void GetFriendInfo(string tuin)
        {
            string url = "http://s.web2.qq.com/api/get_friend_info2?tuin={$tuin}&verifysession=&code=&vfwebqq={$vfwebqq}&t=1402534798024";
            url = url.Replace("{$vfwebqq}", vfwebqq);
            url = url.Replace("{$tuin}", tuin);

            HttpItem item = new HttpItem()
            {
                URL = url,
                Encoding = System.Text.Encoding.GetEncoding("utf-8"),
                Method = "get",
                IsToLower = false,
                Timeout = 100000,
                ReadWriteTimeout = 30000,
                Host = HOST[1],
                Referer = REFERER[1],
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",
                ResultType = ResultType.String,
            };
            HttpResult result = http.GetHtml(item);
        }


接下来是向某一个好友发送信息

public void SendPersonMsg(string tuin,string msg)
        {
            string style = "\"{content}\"";
            string temp = "";
            temp += style.Replace("{content}", msg) + ",";
            temp = temp.Substring(0, temp.Length - 1);
            Random rd = new Random();
            int msg_id = (rd.Next(100000) + 100000);
            string content = "[{$msg},\"\",[\"font\",{\"name\":\"宋体\",\"size\":\"10\",\"style\":[0,0,0],\"color\":\"000000\"}]]";
            content = content.Replace("{$msg}", temp);


            Dictionary<string, Object> r = new Dictionary<string, Object>();
            r.Add("to", tuin);
            r.Add("face", 561);
            r.Add("content", content);
            r.Add("msg_id",msg_id);
            r.Add("clientid", this.clientid);
            r.Add("psessionid", this.psessionid);
            Dictionary<string, Object> data = new Dictionary<string, Object>();
            data.Add("r", JsonConvert.SerializeObject(r));
            data.Add("clientid", this.clientid);
            data.Add("psessionid", this.psessionid);
            string postdata = Tool.MakePostData(data);

            HttpItem item = new HttpItem()
            {
                URL = "http://d.web2.qq.com/channel/send_buddy_msg2",
                Accept = "*/*",
                Method = "POST",
                IsToLower = false,
                KeepAlive = true,
                Timeout = 100000,
                Postdata = postdata,
                Host = HOST[0],
                Referer = REFERER[0],
                UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2041.4 Safari/537.36",
                ContentType = "application/x-www-form-urlencoded",
                ResultType = ResultType.String
            };

            HttpResult result = http.GetHtml(item);
            JObject ret = (JObject)JsonConvert.DeserializeObject(result.Html);
        }

好了,这篇和上一篇文章就基本上实现了WebQQ的基本功能,

为了保持QQ一直在线我们比如一定时间向服务器发送心跳请求,心跳请求的返回值就是我们收到的消息,这个是很重要的所以想对群消息进行处理的话都需要在这里处理它

 public void HeartBreak()
        {
            /*
            Dictionary<string, Object> r = new Dictionary<string, Object>();
            r.Add("clientid", this.clientid);
            r.Add("psessionid", this.psessionid);
            r.Add("key", 0);
            r.Add("ids", "[]");
            Dictionary<string, Object> data = new Dictionary<string, Object>();
            data.Add("r", JsonConvert.SerializeObject(r));
            data.Add("clientid", this.clientid);
            data.Add("psessionid", this.psessionid);
            string postdata2 = Tool.MakePostData(data);
            */
            string postdata = "r=%7B%22clientid%22%3A%22{$clientid}%22%2C%22psessionid%22%3A%22{$psessionid}%22%2C%22key%22%3A0%2C%22ids%22%3A%5B%5D%7D&clientid={$clientid}&psessionid={$psessionid}";
            postdata=postdata.Replace("{$clientid}",this.clientid);
            postdata = postdata.Replace("{$psessionid}", this.psessionid);
            HttpItem item = new HttpItem()
            {
                URL = "http://d.web2.qq.com/channel/poll2",
                Method = "POST",
                IsToLower = false,
                Timeout = 100000,
                Postdata = postdata,
                Host = HOST[0],
                Referer = REFERER[0],
                UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36",//用户的浏览器类型,版本,操作系统     可选项有默认值   
                ContentType = "application/x-www-form-urlencoded",
                ResultType = ResultType.String
            };
            HttpResult result = http.GetHtml(item);
            JObject ret = (JObject)JsonConvert.DeserializeObject(result.Html);
            JObject retjson = (JObject)ret["result"][0];
            if (retjson["poll_type"].ToString() == "group_message")
            {
                JObject msg = (JObject)retjson["value"];
                ProcessMsg(msg);
            }
            Thread.Sleep(2000);

        }

在发送信息的上面还有一些可以拓展的,比如图片等,我这一部分还没有研究,如果有谁做过的话希望分享一下咯

 

 


 

C# WebQQ协议群发机器人(二)

标签:机器人   webqq   c#   自动发消息   

原文地址:http://blog.csdn.net/zhujunxxxxx/article/details/38941599

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!