1、将文件传输助手置顶,模拟鼠标点击文件传输助手;
2、一直刷新会话列表,有新的消息就需要回复内容;
3、当刷新到新的消息时,模拟鼠标点击到对应的会话人,此时判断是群聊还是人,如果是群聊则不回复。
4、获取消息后转发给chatGPT,同时等待chatGPT回复内容。
6、模拟鼠标点击文件传输助手,等待其它消息。
三、功能代码
3.1、获取聊天消息并发送
voidGetChatInfo
{
if(!IsInit)
return;
}
if(wxWindow==null)
//wxWindow.FindAllDescendants(x=>x.ByControlType(FlaUI.Core.Definitions.ControlType.Button)).AsParallel.FirstOrDefault(s=>s!=null&&s.Name=="聊天").Click(false);
wxWindow.FindFirstDescendant(cf=>cf.ByName("聊天")).Click(false);
Task.Run(=>
AutomationElementassFirst=null;
objectobj=newobject;
while(true)
if(ChatListCancellationToken.IsCancellationRequested)
break;
try
DateTimedateTime3=DateTime.Now;
varsearchTextBox=wxWindow.FindFirstDescendant(cf=>cf.ByName("会话")).AsListBoxItem;
if(searchTextBox!=null)
varlist=searchTextBox.FindAllChildren;
if(assFirst==null)
assFirst=list.AsParallel.FirstOrDefault(t=>t!=null&&"文件传输助手".Equals(t.Name));//并行查询---需要将文件传输助手置顶
assFirst.Click;
continue;
Parallel.ForEach(list,item=>
if(item!=null&&!string.IsNullOrEmpty(item.Name)&&!"折叠置顶聊天".Equals(item.Name)
&&!"腾讯新闻".Equals(item.Name)&&!"群聊".Contains(item.Name))
DateTimet1=DateTime.Now;
varallText=item.FindAllByXPath(".//Text");//定位元素的局部搜索:.//Text;全局搜索://*/Text
DateTimet2=DateTime.Now;
Trace.WriteLine($"allText用时:{(t2-t1).TotalMilliseconds}ms");
//回复消息列表还未回复的
if(allText!=null&&allText.Length>=4)
if(int.TryParse(allText[3].Name,outvarcount)&&count>0)
lock(obj)
varname=allText[0].Name;
vartime=allText[1].Name;
varcontent=allText[2].Name;
if(wxWindow.Patterns.Window.PatternOrDefault!=null)
wxWindow.Patterns.Window.Pattern.SetWindowVisualState(FlaUI.Core.Definitions.WindowVisualState.Normal);
item.Click;
DateTimet7=DateTime.Now;
varitemFirst=wxWindow.FindAllDescendants(x=>x.ByControlType(FlaUI.Core.Definitions.ControlType.Text)).AsParallel
.FirstOrDefault(t=>t!=null&&t.Parent.ControlType==ControlType.Pane&&!t.IsOffscreen&&t.Name.Trim.IsSpecificNumbers);
DateTimet8=DateTime.Now;
Trace.WriteLine($"itemFirst:{(t8-t7).TotalMilliseconds}ms");
//判断是否为群聊
if(itemFirst==null)
AutoGetMesg(content);
});
DateTimedateTime4=DateTime.Now;
Trace.WriteLine($"任务888耗时:{(dateTime4-dateTime3).TotalMilliseconds}ms");
else
Thread.Sleep(10);
//ScrollEvent(-700);
catch(Exceptionex)
finally
//awaitTask.Delay(1);
},ChatListCancellationToken);
3.2、自动回复IChat_chat;
privatevoidAutoGetMesg(stringtxt)
if(_chat==null)
_chat=newChatAchieve;
_chat.RequestContent=GetMessage;
Trace.WriteLine($"发送消息:{txt}");
_chat.RequestGPT(txt);
privateFlaUI.Core.AutomationElements.TextBox_mesText;
publicFlaUI.Core.AutomationElements.TextBoxMesText
get
if(_mesText==null)
_mesText=wxWindow.FindFirstDescendant(x=>x.ByControlType(FlaUI.Core.Definitions.ControlType.Text)).AsTextBox;
return_mesText;
privateAutomationElement_btnSend;
publicAutomationElementbtnSend
if(_btnSend==null)
_btnSend=wxWindow.FindFirstDescendant(cf=>cf.ByName("sendBtn"));
//_btnSend=wxWindow.FindAllDescendants(x=>x.ByControlType(FlaUI.Core.Definitions.ControlType.Button)).FirstOrDefault(s=>s.Name=="发送(S)");
return_btnSend;
constint_offSize=300;
privatevoidGetMessage(stringmes)
SendMes(mes);
Trace.WriteLine($"回复:{mes}");
privatevoidSendMes(stringmes)
inttempLen=0;
stringtxt=string.Empty;
if(!string.IsNullOrWhiteSpace(mes))
string[]lines=mes.Split(Environment.NewLine);
foreach(stringlineinlines)
tempLen+=line.Length;
txt+=line+Environment.NewLine;
if(tempLen>_offSize)
MesText.Text=txt;
btnSend.Click;
tempLen=0;
txt=string.Empty;
if(!string.IsNullOrWhiteSpace(txt))
Thread.Sleep(3);
Trace.WriteLine(ex.Message);
3.3、其它代码///
///启动
///
///
///
privatevoidbtnStart_Click(objectsender,EventArgse)
InitWechat;
privatevoidFrmMain_Load(objectsender,EventArgse)
privatevoidFrmMain_FormClosing(objectsender,FormClosingEventArgse)
this.Dispose;
GC.Collect;
privateCancellationTokenFriendCancellationToken{get;set;}
privateCancellationTokenSourceFriendTokenSource{get;set;}
privateCancellationTokenChatListCancellationToken{get;set;}
privateCancellationTokenSourceChatListTokenSource{get;set;}
privateCancellationTokenGetFriendCancellationToken{get;set;}
privateCancellationTokenSourceGetFriendTokenSource{get;set;}
///
privateintProcessId{get;set;}
privateWindowwxWindow{get;set;}
privateboolIsInit{get;set;}
///获取
voidGetWxHandle
varprocess=Process.GetProcessesByName("Wechat").FirstOrDefault;
if(process!=null)
ProcessId=process.Id;
voidInitWechat
IsInit=true;
GetWxHandle;
GetFriendTokenSource=newCancellationTokenSource;
GetFriendCancellationToken=GetFriendTokenSource.Token;
ChatListTokenSource=newCancellationTokenSource;
ChatListCancellationToken=ChatListTokenSource.Token;
FriendTokenSource=newCancellationTokenSource;
FriendCancellationToken=FriendTokenSource.Token;
varapplication=FlaUI.Core.Application.Attach(ProcessId);
varautomation=newUIA3Automation;
wxWindow=application.GetMainWindow(automation);
if(MessageBox.Show(ex.Message,"异常",MessageBoxButtons.OK,MessageBoxIcon.Error)==DialogResult.OK)
this.Close;
//加载好友
IsListenCronyList=true;
//加载聊天信息
GetChatInfo;
///获取好友列表
voidGetFriends
wxWindow.FindAllDescendants(x=>x.ByControlType(FlaUI.Core.Definitions.ControlType.Button)).AsParallel
.FirstOrDefault(item=>item!=null&&item.Name=="通讯录").Click(false);
stringlastName=string.Empty;
varlist=newList
varsync=SynchronizationContext.Current;
Task.Run(async=>
if(GetFriendCancellationToken.IsCancellationRequested)
varall=wxWindow.FindAllDescendants(x=>x.ByControlType(FlaUI.Core.Definitions.ControlType.ListItem));
varallItem=all.AsParallel.Where(s=>s!=null&&s.Parent!=null&&"联系人".Equals(s.Parent.Name)).ToList;
foreach(variteminallItem)
if(!string.IsNullOrWhiteSpace(item.Name)&&!listBox1.Items.Contains(item.Name.ToString))
sync.Post(s=>
listBox1.Items.Add(s);
},item.Name.ToString);
awaitTask.Delay(1);
},GetFriendCancellationToken);
///监听好友列表
privatevoidbtnListenCronyList_Click(objectsender,EventArgse)
IsListenCronyList=!IsListenCronyList;
privatebool_isListenCronyList=false;
publicboolIsListenCronyList
set
if(_isListenCronyList==value)
_isListenCronyList=value;
if(value)
txt="关闭监听好友列表";
GetFriends;
txt="开启监听好友列表";
GetFriendTokenSource.Cancel;
btnListenCronyList.ExecBeginInvoke(=>
btnListenCronyList.Text=txt;
get=>this._isListenCronyList;
3.4、扩展方法internalstaticclassSystemEx
///跨线程操作控件
///
///
publicstaticvoidExecBeginInvoke(thisControlcon,Actionaction)
if(action==null)return;
if(con.InvokeRequired)
con.BeginInvoke(newAction(action));
action;
publicstaticvoidExecInvoke(thisControlcon,Actionaction)