本篇文章实现了微信自动接收最新的实时聊天信息,并对当前实时的聊天信息做出对应的回复。
可以自行接入人工智能或者结合自己的业务来做出自动回复。
下面视频是软件实际效果
自动接收消息并回复
实现的逻辑是实时监控微信的聊天面板中UI对象来判断是否有最新的消息过来,如果没有则继续监听,如果有则点击UI对象切换到当前聊天面板,并找到聊天面板中的输入框和发送按钮元素来进行回复。
(1)监控聊天面板中的UI对象
public override void Start(){if (State == TaskState.Stop){State = TaskState.Runing;}else {return;}Thread th = new Thread(new ThreadStart(() =>{while (State == TaskState.Runing){var currentChatList = UI_WX_Window.Current.Find("/Pane/Pane[1]/Pane[2]/Pane/Pane/Pane/List");if (currentChatList == null || currentChatList.ControlType != FlaUI.Core.Definitions.ControlType.List){Thread.Sleep(1000);continue;}var source = currentChatList.FindAllChildren();var tempMessageSource = new List<WXRealMsgEntity>();source.ToList().ForEach(item =>{if (item.ControlType == FlaUI.Core.Definitions.ControlType.ListItem){var buttonName = item.FindFirstByXPath("/Pane/Button")?.Name;var time = item.FindFirstByXPath("/Pane/Pane/Pane[1]/Text")?.Name;var lastMessage = item.FindFirstByXPath("/Pane/Pane/Pane[2]/Text")?.Name;var count = item.FindFirstByXPath("/Pane/Text")?.Name;var messageitem = new WXRealMsgEntity { UserName = buttonName, Time = time, CurrentMessage = lastMessage, Count = count };tempMessageSource.Add(messageitem);if (historyMessageSource != null){//与历史消息比较//var cnt = historyMessageSource.Count(s => s.UserName == messageitem.UserName//&& messageitem.Time == s.Time//&& messageitem.CurrentMessage == s.CurrentMessage//&& messageitem.Count == s.Count);var cnt = historyMessageSource.Count(s => s.UserName == messageitem.UserName && messageitem.CurrentMessage == s.CurrentMessage);if (cnt == 0){//有新消息SetMessage(messageitem.UserName + "发送给新消息你:" + messageitem.CurrentMessage);if (ReplyUser.Count(ss => ss.Contains(messageitem.UserName))>0){//自动回复UI_WX_Window.Current.ClickElement(item);Thread.Sleep(200);SendWXMsg(messageitem.UserName,ReplyMessage);//需要将历史消息本条重置SetMessage( "自动回复消息给"+messageitem.UserName+":" + ReplyMessage);messageitem.CurrentMessage = ReplyMessage;}}}}});historyMessageSource = tempMessageSource;Thread.Sleep(1000);}}));th.Start();base.Start();}
(2)发送消息
public void SendWXMsg(string name, string message){var SendInput = GetSendInput();if (SendInput != null){SetText(new WXMessageEntity { Info = message, Type = WXMessageType.Text }, SendInput, false);var SendButton = GetSendButton();if (SendButton != null){UI_WX_Window.Current.ClickElement(SendButton);SystemLog.Info("向联系人【" + name + "】发送消息成功");}}else{SystemLog.Error("【" + name + "】不是有效联系人");}}
上一篇 (12)PC端微信自动化测试-C#采集微信聊天记录
如果读者对上述文章存在不清晰的地方可以找本人获取运行源码来学习,QQ 978124155