Ejemplo n.º 1
0
        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        private static MsgResp SendMsg(SendMsgReq req, bool changeCode)
        {
            if (string.IsNullOrEmpty(req.Content))
            {
                return(new MsgResp()
                {
                    Ret = 1
                });
            }
            Console.WriteLine($"[log]{(req.SendToType == 1 ? "好友" : (req.SendToType == 2 ? "群聊" : "私聊"))}给{req.ToUserUid}->{req.Content}");
            var codes = OPQCode.Parse(req.Content);

            if (changeCode)
            {
                if (string.IsNullOrEmpty(req.PicUrl))
                {
                    codes.Where(p => p.Function == OPQFunction.Pic).ToList().ForEach(code =>
                    {
                        req.SendMsgType = "PicMsg";
                        req.PicUrl      = code.Items.ContainsKey("url") ? code.Items["url"] : "";
                        req.PicPath     = code.Items.ContainsKey("path") ? code.Items["path"] : "";
                        req.FlashPic    = !code.Items.ContainsKey("flash") && ((code.Items["flash"] == (true + "") ? true : false));
                        req.Content     = req.Content.Replace(code.ToString(), "");
                    });
                }
                if (string.IsNullOrEmpty(req.VoiceUrl))
                {
                    codes.Where(p => p.Function == OPQFunction.Voice).ToList().ForEach(code =>
                    {
                        req.SendMsgType = "VoiceMsg";
                        req.VoiceUrl    = code.Items.ContainsKey("url") ? code.Items["url"] : "";
                        req.VoicePath   = code.Items.ContainsKey("path") ? code.Items["path"] : "";
                        req.Content     = req.Content.Replace(code.ToString(), "");
                    });
                }
                codes.Where(p => p.Function == OPQFunction.Rich).ToList().ForEach(code =>
                {
                    req.SendMsgType = "JsonMsg";
                    req.Content     = JsonConvert.SerializeObject(new RichCard(code.Items.GetValueOrDefault("title"), code.Items.GetValueOrDefault("desc"), code.Items.GetValueOrDefault("prompt"), code.Items.GetValueOrDefault("tag"), code.Items.GetValueOrDefault("url"), code.Items.GetValueOrDefault("preview")));
                });
            }
            Console.WriteLine($"[DEBUG]{JsonConvert.SerializeObject(req)}");
            var msg = HttpUtils.Post <MsgResp>(_ApiAddress + "&funcname=SendMsgV2", req);
            var i   = 0;

            while (msg.Ret == 241 && i < 10)
            {
                Console.WriteLine($"[WARN]API等待{JsonConvert.SerializeObject(req)}");
                Thread.Sleep(1100);
                msg = HttpUtils.Post <MsgResp>(_ApiAddress + "&funcname=SendMsgV2", req);
                i++;
            }
            if (msg.Ret == 241)
            {
                Console.WriteLine($"[WARN]API调用过于频繁,本条丢弃{JsonConvert.SerializeObject(req)}");
            }
            return(msg);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 发消息
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        private static MsgResp SendMsg(SendMsgReq req, bool changeCode)
        {
            if (string.IsNullOrEmpty(req.content))
            {
                return(new MsgResp()
                {
                    Ret = 1
                });
            }
            Console.WriteLine($"[log]{(req.sendToType == 1 ? "好友" : (req.sendToType == 2 ? "群聊" : "私聊"))}给{req.toUser}->{req.content}");
            List <OPQCode> codes = OPQCode.Parse(req.content);

            if (changeCode)
            {
                if (string.IsNullOrEmpty(req.picUrl))
                {
                    codes.Where(p => p.Function == OPQFunction.Pic).ToList().ForEach(code =>
                    {
                        req.sendMsgType  = "PicMsg";
                        req.picUrl       = code.Items["url"];
                        req.picBase64Buf = "";
                        req.content      = req.content.Replace(code.ToString(), "");
                    });
                }
                if (string.IsNullOrEmpty(req.voiceUrl))
                {
                    codes.Where(p => p.Function == OPQFunction.Voice).ToList().ForEach(code =>
                    {
                        req.sendMsgType  = "VoiceMsg";
                        req.voiceUrl     = code.Items["url"];
                        req.picBase64Buf = "";
                        req.content      = req.content.Replace(code.ToString(), "");
                    });
                }
                codes.Where(p => p.Function == OPQFunction.Rich).ToList().ForEach(code =>
                {
                    req.sendMsgType = "JsonMsg";
                    req.content     = JsonConvert.SerializeObject(new RichCard(code.Items.GetValueOrDefault("title"), code.Items.GetValueOrDefault("desc"), code.Items.GetValueOrDefault("prompt"), code.Items.GetValueOrDefault("tag"), code.Items.GetValueOrDefault("url"), code.Items.GetValueOrDefault("preview")));
                });
            }

            MsgResp msg = HttpUtils.Post <MsgResp>(_ApiAddress + "&funcname=SendMsg", req);
            int     i   = 0;

            while (msg.Ret == 241 && i < 10)
            {
                Console.WriteLine($"[WARN]API等待{JsonConvert.SerializeObject(req)}");
                System.Threading.Thread.Sleep(1100);
                msg = HttpUtils.Post <MsgResp>(_ApiAddress + "&funcname=SendMsg", req);
                i++;
            }
            if (msg.Ret == 241)
            {
                Console.WriteLine($"[WARN]API调用过于频繁,本条丢弃{JsonConvert.SerializeObject(req)}");
            }
            return(msg);
        }