Automate WeChat’s “Poke” Feature with Python, Appium, and OpenCV in 30 Lines
This tutorial explains how to use Python, Appium, and OpenCV to automatically perform WeChat’s “punch” (拍一拍) action by locating a friend’s avatar via image recognition and simulating a double‑tap, all within roughly 30 lines of code.
Introduction
WeChat’s “punch” (拍一拍) feature can be automated using Python, Appium, and OpenCV. The following tutorial shows how to implement it in about 30 lines of code.
Highlights
Use Appium to control the phone, capture screenshots, locate a friend’s avatar via image recognition, and simulate a double‑tap on the avatar coordinates.
Steps
1. Control the phone with Appium
Install Appium as described elsewhere. The essential capabilities are:
desired_caps = {
"platformName": "Android",
"deviceName": "VOG-AL00",
"appPackage": "com.tencent.mm",
"appActivity": ".ui.LauncherUI",
"noReset": "true",
"fullReset": "false"
}
server = 'http://localhost:4723/wd/hub'
driver = webdriver.Remote(server, desired_caps)These settings launch WeChat on the connected device.
2. Locate the avatar
We use the Aircv library (a Python wrapper for OpenCV) and its find_template function to find the avatar image within a screenshot.
def matchImg(imgsrc, imgobj, confidencevalue=0.7):
imsrc = ac.imread(imgsrc)
imgobj = ac.imread(imgobj)
match_result = ac.find_template(imsrc, imgobj, confidencevalue)
return match_resultThe function returns a dictionary containing result (center coordinates), rectangle (corner points), and confidence.
3. Capture screenshot and search
driver.save_screenshot('src.png')
imgsrc='src.png'
imgobj='obj.png'
match_result=matchImg(imgsrc, imgobj)If match_result is None, the avatar was not found and the loop continues.
4. Simulate double‑tap
if match_result is not None:
result = [match_result['rectangle'][0], match_result['rectangle'][3]]
for i in range(2):
driver.tap(result, 100)
print('拍成功')
else:
print('没找到头像')This taps the avatar twice, achieving the “punch” effect.
Conclusion
The script demonstrates a simple way to automate WeChat’s “punch” feature. It can be extended to monitor group chats and automatically “punch” active users or iterate over all members.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
