Пример #1
0
        public override int Enabled()
        {
            //面板重新启用时候判断
            if (ExitTask)
            {
                ExitTask = false;
                MemberIdCache.Clear();
            }


            if (File.Exists("config.ini"))
            {
                string config = File.ReadAllText("config.ini");
                try
                {
                    JObject configJson = JObject.Parse(config);
                    foreach (JObject idol in configJson.SelectTokens("$.Idols.[*]"))
                    {
                        ListenConfig listenConfig = new ListenConfig();
                        listenConfig.QQGroup         = long.Parse(idol["QQGroup"].ToString());
                        listenConfig.KDRoomId        = long.Parse(idol["KDRoomId"].ToString());
                        listenConfig.IdolName        = idol["IdolName"].ToString();
                        listenConfig.GetRoomMsgDelay = int.Parse(idol["GetRoomMsgDelay"].ToString());
                        listenConfig.GetLiveDelay    = int.Parse(idol["GetLiveDelay"].ToString());
                        listenConfig.GetWeiboDelay   = int.Parse(idol["GetWeiboDelay"].ToString());
                        listenConfig.HitYouText      = idol["NeedYou"].ToString();

                        MemberIdCache.Add(listenConfig.IdolName, listenConfig.QQGroup);
                        CoolQApi.AddLog(CoolQLogLevel.Info, listenConfig.IdolName + "info load ok:kid " + listenConfig.KDRoomId);
                        new Task(() => {
                            while (!ExitTask)
                            {
                                GetRoomMsg(listenConfig);
                                listenConfig.MsgCount++;
                                CoolQApi.AddLog(CoolQLogLevel.Debug, "get room msg count:" + listenConfig.MsgCount + " msg lasttime:" + listenConfig.Lasttime + " member:" + listenConfig.IdolName + " kdroomid" + listenConfig.KDRoomId);
                                Thread.Sleep(listenConfig.GetRoomMsgDelay);
                            }
                        }).Start();
                    }


                    new Task(() =>
                    {
                        while (!ExitTask)
                        {
                            GetLive();
                            LiveCount++;
                            CoolQApi.AddLog(CoolQLogLevel.Debug, "get live count:" + LiveCount);
                            Thread.Sleep(int.Parse(configJson["GetLiveDelay"].ToString()));
                        }
                    }).Start();
                }
                catch (Exception ex)
                {
                    File.AppendAllText("error.log", DateTime.Now.ToString() + "\r\n" + ex.ToString() + "\r\n" + ex.StackTrace + "\r\n");
                    CoolQApi.AddLog(CoolQLogLevel.Error, "请检查config.ini是否配置正确,七项缺一不可");
                    return(base.Enabled());
                }
            }
            else
            {
                CoolQApi.AddLog(CoolQLogLevel.Debug, "config error");
            }
            return(base.Enabled());
        }
Пример #2
0
        public void GetRoomMsg(ListenConfig listenConfig)
        {
            try
            {
                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(new Uri("https://pjuju.48.cn/imsystem/api/im/v1/member/room/message/chat"));
                req.Method    = "POST";
                req.UserAgent = "okhttp/3.4.1";

                JObject rss = new JObject(
                    new JProperty("roomId", listenConfig.KDRoomId),
                    new JProperty("lastTime", 0),
                    new JProperty("limit", 10)
                    );

                string postJson = rss.ToString();
                byte[] bytes    = Encoding.UTF8.GetBytes(postJson);

                req.ContentType   = "application/json";
                req.ContentLength = bytes.Length;

                Stream reqstream = req.GetRequestStream();
                reqstream.Write(bytes, 0, bytes.Length);

                HttpWebResponse response      = (HttpWebResponse)req.GetResponse();
                Stream          streamReceive = response.GetResponseStream();
                Encoding        encoding      = Encoding.UTF8;

                StreamReader streamReader = new StreamReader(streamReceive, encoding);
                string       strResult    = streamReader.ReadToEnd();

                streamReceive.Dispose();
                streamReader.Dispose();


                JObject json = JObject.Parse(strResult);
                if ((int)json["status"] == 200)
                {
                    IEnumerable <JToken> datas = json.SelectTokens("$.content.data[*]");

                    //记录本次最大时间戳
                    long tmpTime = 0;

                    foreach (JToken msgs in datas)
                    {
                        //历史最后时间戳比对
                        if ((long)msgs["msgTime"] > listenConfig.Lasttime)
                        {
                            //本次消息时间
                            if ((long)msgs["msgTime"] > tmpTime)
                            {
                                tmpTime = (long)msgs["msgTime"];
                            }
                            JObject msg = JObject.Parse(msgs["extInfo"].ToString());
                            //首次运行,直接退出循环
                            if (listenConfig.First)
                            {
                                break;
                            }
                            if ((long)msgs["msgTime"] < listenConfig.Lasttime)
                            {
                                break;
                            }
                            switch (msg["messageObject"].ToString())
                            {
                            case "deleteMessage":
                                //CQ.SendGroupMessage(qqGroup,"你的小偶像删除了一条口袋房间的消息");
                                break;

                            case "text":
                                CoolQApi.SendGroupMsg(listenConfig.QQGroup, String.Format("口袋房间:\r\n{0}:{1}\r\n发送时间:{2}", msg["senderName"].ToString(), msg["text"].ToString(), msgs["msgTimeStr"].ToString()));
                                break;

                            case "image":
                                JObject img         = JObject.Parse(msgs["bodys"].ToString());
                                string  imgFilename = GetImage(img["url"].ToString());
                                if (imgFilename == "")
                                {
                                    return;
                                }
                                CoolQApi.SendGroupMsg(listenConfig.QQGroup, String.Format("口袋房间:\r\n{0}:\r\n{1}", msg["senderName"].ToString(), CoolQCode.Image(imgFilename)));
                                break;

                            case "faipaiText":
                                CoolQApi.SendGroupMsg(listenConfig.QQGroup, String.Format("口袋房间:\r\n翻牌辣!{3}:{4}\r\n{0} 回复:{1}\r\n被翻牌的大佬不来集资一发吗?" + listenConfig.HitYouText + " \r\n发送时间:{2}", msg["senderName"].ToString(), msg["messageText"].ToString(), msgs["msgTimeStr"].ToString(), msg["faipaiName"].ToString(), msg["faipaiContent"].ToString()));
                                break;

                            case "audio":
                                JObject audio         = JObject.Parse(msgs["bodys"].ToString());
                                string  audioFilename = GetAudio(audio["url"].ToString(), audio["ext"].ToString());
                                if (audioFilename == "")
                                {
                                    return;
                                }
                                CoolQApi.SendGroupMsg(listenConfig.QQGroup, String.Format("口袋房间:\r\n{0}:\r\n{1}", msg["senderName"].ToString(), CoolQCode.ShareRecord(audioFilename)));
                                break;

                            case "videoRecord":
                                JObject video = JObject.Parse(msgs["bodys"].ToString());
                                CoolQApi.SendGroupMsg(listenConfig.QQGroup, string.Format("{0}发送了一个视频,请点击下面链接查看\r\n地址:{1}", msg["senderName"].ToString(), video["url"].ToString()));
                                break;

                            default:
                                CoolQApi.SendGroupMsg(listenConfig.QQGroup, "你的小偶像有一条新消息,TeemoBot无法支持该类型消息,请打开口袋48查看~~并向开发反馈下~谢谢~");
                                break;
                            }
                        }
                    }
                    if (tmpTime != 0)
                    {
                        listenConfig.Lasttime = tmpTime;
                    }
                }
                if (listenConfig.First)
                {
                    listenConfig.First = false;
                }
            }
            catch (Exception ex)
            {
                File.AppendAllText("error.log", DateTime.Now.ToString() + "\r\n" + ex.ToString() + "\r\n" + ex.StackTrace + "\r\n");
            }
        }