private void ListeningHandle(IEnumerable <WXMsg> msgs) { foreach (var item in msgs) { string reMsg; var msg = item.Msg; PrintLin($"[{item.FromNickName}] - [{item.ToNickName}] - {item.Time}"); PrintLin($"Msg: {msg}"); if (item.From.Equals(_wxSerivice.Me.UserName)) { // from me to other people var toUser = Rule.Rules.Keys.FirstOrDefault(o => o.UserName == item.To); if (toUser == null) { PrintLin(); continue; } var toRule = Rule.Rules[toUser]; if (toRule.Name == "Default") { PrintLin(); continue; } reMsg = toRule.FromMeInvoke(toUser.UserName, msg, item.Type, (int)toUser.UserType); } else { PlayMsgMusic(); // find the specify user rule, if aleady store in Rule.Rules var user = Rule.Rules.Keys.FirstOrDefault(o => o.UserName == item.From); if (user == null) { PrintLin(); continue; } ; var rule = Rule.Rules[user]; // if rule equals "Default", just ignore it if (rule.Name == "Default") { PrintLin(); continue; } reMsg = rule.Invoke(user.UserName, msg, item.Type, (int)user.UserType); } PrintLin($"ReMsg: {reMsg}"); PrintLin(); // if reMsg is null, skip it if (reMsg != null) { _wxSerivice.SendMsg(reMsg, _wxSerivice.Me.UserName, item.From, 1); } } }
/// <summary> /// 主动发送消息 /// </summary> public void AutoSendMessage(string to, string content) { try { WXService wxs = new WXService(); wxs.SendMsg(content, _me.UserName, to, 1); } catch (Exception ex) { log.Error(ex); } }
/// <summary> /// 向该用户发送消息 /// </summary> /// <param name="msg"></param> /// <param name="showOnly"></param> public void SendMsg(WXMsg msg, bool showOnly) { if (!showOnly) { WXService wxs = new WXService(); wxs.SendMsg(msg.Msg, msg.From, msg.To, msg.Type); } _sentMsg.Add(msg.Time, msg); if (MsgSent != null) { MsgSent(msg); } }
/// <summary> /// 向该用户发送消息 /// </summary> /// <param name="msg"></param> public void SendMsg(WXMsg msg, bool showOnly) { try { if (!showOnly) { WXService wxs = new WXService(); wxs.SendMsg(msg.Msg, msg.From, msg.To, msg.Type, msg.Uin, msg.Sid); } _sentMsg.Add(msg.Time, msg); MsgSent.Invoke(msg); } catch { return; } }
/// <summary> /// 接收消息 /// </summary> /// <param name="msg"></param> void wt_OnRevice(WXMsg msg) { try { WxContact c = new WxContact(msg.Uin); string log = "type:" + msg.Type.ToString() + "来源:" + msg.From + "[ " + c.GetNickName(msg.From) + "],发至:" + msg.To + " [" + c.GetNickName(msg.To) + "]" + msg.Msg; WriteLog(log); var m = ReplyFactory.Create(msg).MakeContent(msg); var sendContext = m.context; if (!string.IsNullOrEmpty(sendContext)) { WXService s = new WXService(); s.Sid = msg.Sid; s.Uin = msg.Uin; s.SendMsg(sendContext, msg.To, msg.From, m.type, msg.Uin, msg.Sid); } //string afTitle = "[ " + c.GetNickName(msg.From) + "]回复[" + c.GetNickName(msg.To) + "]" + "一条消息"; //string afContent = msg.Msg; //YUNkefu.AlertForm.ShowWay showWay = YUNkefu.AlertForm.ShowWay.UpDown; //int afShowInTime, afShowTime, afShowOutTime; //int afWidth, afHeigth; //int.TryParse("100", out afShowInTime); //int.TryParse("500", out afShowTime); //int.TryParse("100", out afShowOutTime); //int.TryParse("250", out afWidth); //int.TryParse("120", out afHeigth); //af = new YUNkefu.AlertForm(); //af.Show(afContent, afTitle, showWay, afWidth, afHeigth, afShowInTime, afShowTime, afShowOutTime); WriteLog("【demo】" + log); } catch (Exception ex) { WriteLog("【错误】" + ex.ToString()); Tools.WriteLog(ex.ToString()); } }
private void handleMsg(JObject sync_result) { if (sync_result["AddMsgCount"] != null && sync_result["AddMsgCount"].ToString() != "0") { WXService wxs = new WXService { Uin = uin, Sid = LoginCore.GetPassTicket(uin).WxSid, DeviceID = "e" + LoginCore.GenerateCheckCode(15), BaseUrl = "https://" + LoginCore.GetPassTicket(uin).WxHost, PushUrl = "https://webpush." + LoginCore.GetPassTicket(uin).WxHost, UploadUrl = "https://file." + LoginCore.GetPassTicket(uin).WxHost }; foreach (JObject m in sync_result["AddMsgList"]) { string from = m["FromUserName"].ToString(); string to = m["ToUserName"].ToString(); string content = m["Content"].ToString(); string MsgId = m["MsgId"].ToString(); string type = m["MsgType"].ToString();//语音视频标识 if (type == "1") { Dispatcher.BeginInvoke(((Action) delegate() { //wxs.SendMsg(content, from, to, 1, uin, sid); //MessageBox.Show(content); //chatText.AppendText("[" + type + "]" + from + "->" + to + " : " + content + "\n"); //chatText.AppendText("[" + type + "]:" + content + "\n"); if (forward == true) { if (from.Contains("@@")) { if (from == sCB.SelectedValue.ToString()) { string[] sArray = Regex.Split(content, ":<br/>", RegexOptions.IgnoreCase); var aa = sCB.SelectedValue.ToString(); if (sArray[0] == smCB.SelectedValue.ToString()) { wxs.SendMsg(sArray[1], forwardUser, dCB.SelectedValue.ToString(), int.Parse(type)); supLB.Content = DateTime.Now.ToLongTimeString().ToString() + ":" + sArray[1]; } /* * if (sArray[0] == smCB.SelectedValue.ToString()) * foreach (var g in dGroup) * { * wxs.SendMsg(sArray[1], forwardUser, g, int.Parse(type)); * //Thread.Sleep(100); * } */ } } //chatText.AppendText("[" + msg.Type + "]" + wxc.GetNickName(from) + "->" + wxc.GetNickName(to) + " : " + content + "\n"); //chatText.PageDown(); //chatText.AppendText("\nmsg: "+sync_result["AddMsgList"].ToString()); //debugTextBox.AppendText(m.ToString()); } })); } else if (type == "3")//图片 { string sFilePath = Environment.CurrentDirectory + "\\IMG"; Dispatcher.BeginInvoke(((Action) delegate() { if (forward == true) { if (from.Contains("@@")) { if (from == sCB.SelectedValue.ToString()) { string[] sArray = Regex.Split(content, ":<br/>", RegexOptions.IgnoreCase); if (sArray[0] == smCB.SelectedValue.ToString()) { wxs.SendMsgImg("", sArray[1], forwardUser, dCB.SelectedValue.ToString(), int.Parse(type)); supLB.Content = DateTime.Now.ToLongTimeString().ToString() + ":转发了图片"; } /* * foreach (var g in dGroup) * { * //wxs.SendMsgImg(ClientMediaId, forwardUser, g, int.Parse(type), uin, sid); * wxs.SendMsgImg("", sArray[1], forwardUser, g, int.Parse(type)); * //Thread.Sleep(300); * } */ } } //chatText.AppendText("[" + msg.Type + "]" + wxc.GetNickName(from) + "->" + wxc.GetNickName(to) + " : " + content + "\n"); //chatText.PageDown(); //chatText.AppendText("\nmsg: "+sync_result["AddMsgList"].ToString()); //debugTextBox.AppendText(m.ToString()); } })); } else if (type == "47")//动态表情 { Dispatcher.BeginInvoke(((Action) delegate() { if (forward == true) { if (from.Contains("@@")) { //chatText.AppendText("[" + type + "]" + from + "->" + to + " : " + content + "\n"); var aa = sCB.SelectedValue.ToString(); if (from == sCB.SelectedValue.ToString()) { string[] sArray = Regex.Split(content, ":<br/>", RegexOptions.IgnoreCase); if (sArray[0] == smCB.SelectedValue.ToString()) { Regex reg = new Regex(@"md5=.(.*).\slen"); Match match = reg.Match(sArray[1]); var aaa = match.Groups[1].Value; var bb = forwardUser; wxs.SendEmoticon(match.Groups[1].Value, forwardUser, dCB.SelectedValue.ToString(), int.Parse(type)); supLB.Content = DateTime.Now.ToLongTimeString().ToString() + ":转发了表情"; } /* * foreach (var g in dGroup) * { * Regex reg = new Regex(@"md5=.(.*).\slen"); * Match match = reg.Match(sArray[1]); * var aaa = match.Groups[1].Value; * var bb = forwardUser; * wxs.SendEmoticon(match.Groups[1].Value, forwardUser, g, int.Parse(type)); * //Thread.Sleep(100); * } */ } } } })); } else { //this.Dispatcher.BeginInvoke((Action)(delegate () //{ //supLB.Content = DateTime.Now.ToLongTimeString().ToString() + ":消息同步中"; //})); } } } /* * else * { * var _syncKey = new Dictionary<string, string>(); * foreach (JObject synckey in sync_result["SyncKey"]["List"]) //同步键值 * { * _syncKey.Add(synckey["Key"].ToString(), synckey["Val"].ToString()); * } * //保存最新key * LoginCore.SyncKey(uin, _syncKey); * } */ }