RPA's FlaUI-based WeChat sends a message to someone

RPA's FlaUI-based WeChat sends a message to someone

Implement WeChat group sending function

最后更新 5/25/2022 12:24 AM
蓝创精英团队
预计阅读 8 分钟
分类
.NET
标签
.NET C# RPA

本文由网友蓝创精英团队投稿,欢迎转载、分享

Original author: Lan Chuang elite team

Original link: kesshei.blog.csdn.net/article/details/124955177


objective

I always wanted to implement WeChat's group sending function, but I couldn't do it. One of the reasons was that I was afraid of breaking the law. I remember that a certain company cracked the WeChat interface and then was accused. A lot of money was fined.

At this time, I thought that if I was based on RPA technology, then it would not have any impact on WeChat. After all, I was just simulating my computer operations, and WeChat company could not recognize that my behavior was illegal.

Then, this behavior may be a technology based on a secure approach.

So, I gave it a try and also learned from the information on the Internet, which gave me some inspiration


1. What is FlaUI?

FlaUI is a new UI automation testing technology based on Microsoft UIAutomation technology launched since Windows Vista, or UIA for short. In the latest Windows SDK, other components that support UI automation technology such as UIA and MSAA are released together, called Windows Automation APIs.

UIA defines new interfaces and patterns for UI automation. They are TreeWalker/FindAll, which supports traversal and conditional queries on UI elements. UIA Properties that define the attributes of reading and writing UI elements, including Name, ID, Type, ClassName, Location, Visibility, etc. A UIA Pattern that defines the behavior of UI elements, such as Select, Expand, Resize, Check, Value, etc. The UIA Event interface is also introduced, which allows the test program to be notified after certain events occur, such as a new window opening event

Currently, FlaUI uses two technologies: UIA2 and UIA3. I mainly use UIA3 here

2. Use steps

1. Introducing NuGet package

Install-Package FlaUI.UIA3 -Version 3.2.0

2. Implement a simple way to send a message to a designated person

The code is as follows (example):

Process[] processes = Process.GetProcessesByName("WeChat");
if (processes.Count() != 1)
{
	Console.WriteLine("微信未启动或启动多个微信");
}
else
{
	//1.附加到微信进程
	using (var app = Application.Attach(processes.First().Id))
	{
		using (var automation = new UIA3Automation())
		{
			//2.获取主界面
			var mainWindow = app.GetMainWindow(automation);
			Console.WriteLine("获取主界面");
			//3.切换到通讯录
			var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, TrueCondition.Default);
			var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("通讯录"));
			addressBook.DrawHighlight(System.Drawing.Color.Red);
			var path = Debug.GetXPathToElement(addressBook);
			Console.WriteLine("点击通讯录");
			addressBook.Click();

			//4.搜索
			string target = "文件传输助手";
			var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox();
			searchTextBox.Click();
			Keyboard.Type(target);
			Keyboard.Type(VirtualKeyShort.RETURN);
			Console.WriteLine("搜索目标对象");

			//5.切换到对话框
			Thread.Sleep(500);

			var searchList = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索结果"));
			if (searchList != null)
			{
				var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name == target && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
				searchItem?.DrawHighlight(System.Drawing.Color.Red);
				searchItem?.AsListBoxItem().Click();
			}
			else
			{
				Console.WriteLine("没有搜索到内容");
			}
			Thread.Sleep(500);
			//6.输入文本
			string sendMsg = "这个是我微信的输入信息:" + DateTime.Now.ToString();
			var msgInput = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
			msgInput?.Click();
			System.Windows.Forms.Clipboard.SetText(sendMsg);
			Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
			var sendBtn = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
			sendBtn?.DrawHighlight(System.Drawing.Color.Red);
			sendBtn?.Click();
		}
	}
}

The code is easy to understand with comments.

It is to search for the designated person and then send the designated information to him. Done.

The graphic effect is as follows:


3. Implement a method to obtain a session list and send messages in batches

The code is as follows (example):

Process[] processes = Process.GetProcessesByName("WeChat");
if (processes.Count() != 1)
{
	Console.WriteLine("微信未启动或启动多个微信");
}
else
{
	//1.附加到微信进程
	using (var app = Application.Attach(processes.First().Id))
	{
		using (var automation = new UIA3Automation())
		{
			//2.获取主界面
			var mainWindow = app.GetMainWindow(automation);
			Console.WriteLine("获取主界面");
			//3.切换到聊天目录
			var elements = mainWindow.FindAll(TreeScope.Subtree, TrueCondition.Default);
			var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("聊天"));
			addressBook.DrawHighlight(System.Drawing.Color.Red);
			var path = Debug.GetXPathToElement(addressBook);
			addressBook.Click();
			Console.WriteLine("切换到聊天");
			Thread.Sleep(2000);
			//4.获取聊天列表
			//只发前六个
			var count = 0;
			var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
			while (searchTextBox != null)
			{
				var list = searchTextBox.FindAllChildren();
				foreach (var item in list)
				{
					count++;
					var name = item.Name;
					item.Click();
					var type = item.ControlType;
					item.DrawHighlight(System.Drawing.Color.Red);
					var MsgSend = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
					var MsgSendButton = mainWindow.FindFirstDescendant(cf => cf.ByName("sendBtn"));
					if (MsgSend != null && MsgSendButton != null)
					{
						MsgSend.Click();
						System.Windows.Forms.Clipboard.SetText($"群发消息,请忽略:{DateTime.Now}");
						Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
						MsgSendButton.Click();
						Console.WriteLine($"发送信息:{name}");
						Thread.Sleep(500);
					}
					if (count == 6)
					{
						break;
					}
				}
				if (count == 6)
				{
					break;
				}
				for (int i = 0; i < list.Length; i++)
				{
					searchTextBox.Focus();
					Keyboard.Press(VirtualKeyShort.DOWN);
					Thread.Sleep(100);
				}
				searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("会话")).AsListBoxItem();
				Thread.Sleep(2000);
			}
		}
	}
}

This code is important to send it to the first 6 people in a group. If there is no send button in the conversation, it will not be sent to avoid affecting more people.

The graphic effect is as follows:

Recorded several times... In the end, someone deleted me, which was embarrassing.

4. How does FlaUI obtain page information

Open this FlaUinspect tool

FlaUInspect

You can see the XPath address as follows

This FlaUinspect project is a WPF project. If you want to do in-depth research, you can check the source code and track and debug it.

The main thing here is that you can get the content you need in the following two ways

The first one is like the one below, obtained through the unique name of the same page.

var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("聊天"));

And the second one goes like this.

You can find the control you want by looking at the XPath address of Figure 2 above.

var infoData = automationElement.FindAllByXPath("/Pane/Pane[1]");

summary

In general, this technology is still very convenient, but for QQ this bottom is self-painting technology and the use of QT, JAVA level applications should not be achieved. Products such as WinFrom and WPF that can only target Microsoft's technology.

Generally speaking, it still reduces the difficulty of using it.

For example, once you have functions to send messages on WeChat, you can expand it yourself. For example, sending messages by designated people, sending in group groups, sending them regularly, sending them with tags. After implementation, the effect is also good for individuals.


Revised on May 30, 2022

About solutions that other netizens can't work

If you report this error.

可以通过,下载 https://github.com/FlaUI/FlaUI 源码,直接引用,不通过 nuget 解决这个问题。

For the time being, we have not delved into what caused it.

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 4/22/2026

Support for. NET by operating system versions (250707 update)

Use virtual machines and test machines to test the support of each version of the operating system for. NET. After installing the operating system, it is passed by measuring the corresponding running time of the installation and being able to run the Stardust Agent.

继续阅读