In front< UI automation easily solves the trouble of sending messages manually by wechat >In the article, I demonstrated how to send text messages using python. Some time ago, a group of friends asked:
Although I thought it was too simple to do before, I thought of this problem again this weekend. Considering that many netizens don't know how to realize it, I'll give a simple demonstration today.
uiautomation controls wechat to send pictures 🌁
In fact, the principle is very simple. Before, it was nothing more than copying and pasting the text to be sent to the clipboard. In fact, it is the same for the pictures. We just need to put the pictures to be sent into the clipboard. The logic is the same everywhere else.
So how to put the picture into the clipboard?
uiautomation has provided the SetClipboardBitmap method, and the implementation code is as follows:
from uiautomation.uiautomation import Bitmap auto.SetClipboardBitmap(Bitmap.FromFile(path))
The complete code of the picture message we sent before is:
import time import uiautomation as auto wechatWindow = auto.WindowControl( searchDepth=1, Name="WeChat", ClassName='WeChatMainWndForPC') wechatWindow.SetActive() search = wechatWindow.EditControl(Name='search') edit = wechatWindow.EditControl(Name='input') messages = wechatWindow.ListControl(Name='news') sendButton = wechatWindow.ButtonControl(Name='send out(S)') def send2name(name, txt, wait_time=0.1): search.Click() auto.SetClipboardText(name) edit.SendKeys('v') # Wait for wechat index search to keep up time.sleep(wait_time) search.SendKeys("") auto.SetClipboardText(txt) edit.SendKeys('v') sendButton.Click() name = "Xiao Ming" txt = "Hello, Xiaoming. Receiving this message indicates that your program has been successful---From automated test program" send2name(name, txt)
Let's transform to support picture sending:
import time import uiautomation as auto from uiautomation.uiautomation import Bitmap wechatWindow = auto.WindowControl( searchDepth=1, Name="WeChat", ClassName='WeChatMainWndForPC') wechatWindow.SetActive() search = wechatWindow.EditControl(Name='search') edit = wechatWindow.EditControl(Name='input') messages = wechatWindow.ListControl(Name='news') sendButton = wechatWindow.ButtonControl(Name='send out(S)') def selectSessionFromName(name, wait_time=0.1): search.Click() auto.SetClipboardText(name) edit.SendKeys('v') # Wait for wechat index search to keep up time.sleep(wait_time) search.SendKeys("") def send_msg(content, msg_type=1): if msg_type: auto.SetClipboardText(content) else: auto.SetClipboardBitmap(Bitmap.FromFile(content)) edit.SendKeys('v') sendButton.Click()
Test switching and sending text and picture messages:
name = "Xiao Ming" selectSessionFromName(name) content = "Hello, Xiaoming. Receiving this message indicates that your program has been successful---From automated test program" send_msg(content) content = r"C:\Users\ASUS\Pictures\avatar.png" send_msg(content, msg_type=0)
In this way, we have finished sending the picture message.
Forwarding of picture messages 🎉
In the above, the function of forwarding the current last text message has been realized, but if the last message is a picture, the previous method will report an error and exit. What the hell is this? Let's look at the similarities and differences between picture and text message nodes:
In order to make the code more general, we must analyze more general rules. We can find the rules by sending the following figure:
Each message is composed of three sub nodes of a panel. The contents of the first and last nodes are mutually exclusive. The first node has a button to represent a message not sent by itself, the tail node has a button to represent a message sent by itself, and the middle node is a panel that includes the message content.
Then you can change the previous code from message.EditControl().RightClick() to message.GetFirstChildControl().GetChildren()[1].RightClick()
In addition, the previous code can exclude the case that the last message is taken, but in fact, the last message may be withdrawn and needs to be excluded. When looking forward for nodes, you may encounter time and need to eliminate it.
For forwarding messages, we basically only need to forward the messages we sent. The following code will find the last message we sent in the current window. Of course, we need to ensure that the last message we sent is within the visual range. As long as it is a message sent to the file transfer assistant, this problem will not exist.
The message sending code before transformation is as follows:
def relay2users(users, max_n=9, wait_time=0.1): message = messages.GetLastChildControl() while True: nodes = message.GetFirstChildControl().GetChildren() if nodes and nodes[2].ControlTypeName == "ButtonControl": break message = message.GetPreviousSiblingControl() nodes[1].RightClick() menu = wechatWindow.MenuControl() menu_items = menu.GetLastChildControl().GetFirstChildControl().GetChildren() for menu_item in menu_items: if menu_item.ControlTypeName != "MenuItemControl": continue if menu_item.Name == "forward": menu_item.Click() break send2ps = wechatWindow.WindowControl(Name="Forward to", ClassName='SelectContactWnd') receiver = send2ps.EditControl(Name="search") sendB2 = send2ps.ButtonControl(Name='send out') items = send2ps.ListControl() for i in range(0, len(users), max_n): users_split = users[i:i+max_n] for user in users_split: receiver.Click() auto.SetClipboardText(user) receiver.SendKeys('v') # Wait for rendering changes to UI components time.sleep(wait_time) items.ButtonControl().Click() receiver.GetParentControl().GetNextSiblingControl().Click() sendB2.Click()
Here is a test to forward this picture message to the specified session name:
users = ["File transfer assistant", "Xiao Ming", "Playmarker", "Data analysis and commercial design", "Daocai Gdc", "Yun Duojun MrCloudData"] relay2users(users)
You can see that this realizes the forwarding of image messages.