public async Task <ResultModel> Run(SendMsgReq req, AppSetting appSetting, HttpContext context) { if (req == null) { return(ResultModel.GetNullErrorModel()); } var msg = req.ValidInfo(); if (msg != string.Empty) { return(ResultModel.GetParamErrorModel(msg)); } var conn = appSetting.GetMysqlConn(context); var param = new MsgInfo() { Content = req.Content, MsgNo = Guid.NewGuid().ToString(), Sender = req.Sender, Receiver = req.Receiver, TypeId = (int)req.MsgType }; var result = await DapperTools.CreateSelectiveItem(conn, param); if (result > 0) { Logger.Debug($"发送消息成功:{param.MsgNo}"); } return(ResultModel.GetSuccessModel(result)); }
/// <summary> /// 发送好友消息 /// </summary> /// <param name="qq">好友QQ</param> /// <param name="txt">文字内容</param> /// <param name="voice">语音消息【http开头的网络地址,或base64内容】</param> /// <param name="pic">图片消息【http开头的网络地址,或base64内容】</param> /// <returns></returns> public static MsgResp SendFriendMsg(long qq, string txt = "", string pic = "", string voice = "", bool changeCode = true) { if (string.IsNullOrEmpty(txt + voice + pic)) { return(new MsgResp() { Ret = 1, Msg = "消息为空" }); } SendMsgReq req = new SendMsgReq() { toUser = qq, sendMsgType = "TextMsg", sendToType = 1, content = (txt == null ? "" : txt) }; if (!string.IsNullOrEmpty(voice)) { req.content = ""; req.sendMsgType = "VoiceMsg"; req.voiceUrl = voice.StartsWith("http") ? voice : ""; req.voiceBase64Buf = voice.StartsWith("http") ? "" : voice; } else if (!string.IsNullOrEmpty(pic)) { req.sendMsgType = "PicMsg"; req.picUrl = pic.StartsWith("http") ? pic : ""; req.picBase64Buf = pic.StartsWith("http") ? "" : pic; } return(SendMsg(req, changeCode)); }
/// <summary> /// 发送群消息 /// </summary> /// <param name="groupId">群号</param> /// <param name="txt">文字内容</param> /// <param name="voice">语音消息【http开头的网络地址,或base64内容】</param> /// <param name="pic">图片消息【http开头的网络地址,或base64内容】</param> /// <param name="changeCode">是否转换OPQ吗</param> /// <returns></returns> public static MsgResp SendGroupMsg(long groupId, string txt = "", string pic = "", string voice = "", object json = null, bool changeCode = true) { if (string.IsNullOrEmpty(txt + voice + pic)) { return(new MsgResp() { Ret = 1, Msg = "消息为空" }); } SendMsgReq req = new SendMsgReq() { toUser = groupId, sendMsgType = "TextMsg", sendToType = 2, content = (txt == null ? "" : txt) }; if (!string.IsNullOrEmpty(voice)) { req.content = ""; req.sendMsgType = "VoiceMsg"; req.voiceUrl = voice.StartsWith("http") ? voice : ""; req.voiceBase64Buf = voice.StartsWith("http") ? "" : voice; } else if (!string.IsNullOrEmpty(pic)) { req.sendMsgType = "PicMsg"; req.picUrl = pic.StartsWith("http") ? pic : ""; req.picBase64Buf = pic.StartsWith("http") ? "" : pic; } else if (null != json) { req.sendMsgType = "JsonMsg"; req.content = JsonConvert.SerializeObject(json); } return(SendMsg(req, changeCode)); }
/// <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); }
/// <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); }
private void OnSend() { Debug.Log("on_send"); if (_inputField.text.Length == 0) { return; } var req = new SendMsgReq { content = _inputField.text }; this.talkContent = _inputField.text; NetworkMgr.Instance.SendProtobufCmd(2, (int)ChatCmd.eSendMsgReq, req); }
/// <summary> /// 发送好友消息 /// </summary> /// <param name="qq">好友QQ</param> /// <param name="txt">文字内容</param> /// <param name="voice">语音消息【http开头的网络地址,或base64内容】</param> /// <param name="pic">图片消息【http开头的网络地址,或base64内容】</param> /// <returns></returns> public static MsgResp SendFriendMsg(long qq, string txt = "", object json = null, bool changeCode = true) { if (string.IsNullOrEmpty(txt) && null == json) { return(new MsgResp() { Ret = 1, Msg = "消息为空" }); } SendMsgReq req = new SendMsgReq() { ToUserUid = qq, SendMsgType = "TextMsg", SendToType = 1, Content = (txt == null ? "" : txt) }; if (null != json) { req.SendMsgType = "JsonMsg"; req.Content = JsonConvert.SerializeObject(json); } return(SendMsg(req, changeCode)); }