Automating QQ Message Replies with Python and Appium
This article demonstrates how to set up a Python environment with Appium, install necessary tools, connect an Android device, and implement a script that automatically reads incoming QQ messages and replies with predefined responses, providing step‑by‑step instructions and sample code for mobile automation.
Introduction
Recently I watched a video about using Python to implement app automation and decided to create a program that automatically replies to QQ messages.
1. Preparation
1.1 Install client module
Open a command prompt and run:
pip install appium-python-client1.2 Install Appium Server
Download from:
http://appium.io1.3 Install JDK
After installation, add the JAVA_HOME environment variable pointing to the JDK directory.
1.4 Install Android SDK
Add an ANDROID_HOME environment variable pointing to the extracted SDK directory and optionally add adb.exe to the PATH .
1.5 Screen‑casting software
I use Mirroid screen‑casting assistant, download from:
https://cn.mirroid.com2. Connect the phone
Use a USB cable, enable developer options on the phone (example OPPO: tap the version number repeatedly in "About Phone" until the developer mode appears, then enable USB debugging).
After enabling USB debugging, the screen‑casting software should display the phone screen.
3. Test connection
In the command window run adb devices -l . If the device appears, the connection is successful.
If an error occurs, it is usually caused by mismatched adb.exe versions between the screen‑casting software and the SDK; replace one adb.exe with the other.
4. Code implementation
Reference code:
<code>from appium import webdriver
import time
desired_caps={
'platformName':'Android',
'platformVersion':'8.1',
'deviceName':'xxx',
'appPackage':'com.tencent.qqlite', # QQ Lite
'appActivity':'com.tencent.mobileqq.activity.SplashActivity',
'noReset':True,
'newCommandTimeout':6000,
'automationName':'UiAutomator2'
}
driver=webdriver.Remote('http://localhost:4723/wd/hub',desired_caps)
driver.implicitly_wait(10)
driver2=driver.find_element_by_id('recent_chat_list')
list2=driver2.find_elements_by_class_name('android.widget.LinearLayout')
print('当前QQ消息为%d个' % (len(list2)))
time.sleep(2)
list2[0].click()
def send_Message(text2:str):
driver4=driver.find_element_by_id('inputBar')
driver4.find_element_by_id('input').send_keys(text2)
driver4.find_element_by_id('fun_btn').click()
time.sleep(2)
print("发送消息:%s" % (text2))
list4=[
"刘邦,字季,沛郡丰邑(今江苏省丰县)人。中国历史上杰出的政治家、战略家和军事指挥家,汉朝开国皇帝,汉民族和汉文化的伟大奠基者和开拓者,对汉族的发展以及中国的统一有突出贡献。",
"还没",
"湖南省,简称‘湘’,是中华人民共和国省级行政区,省会长沙,界于北纬24°38′~30°08′,东经108°47′~114°15′之间,东临江西,西接重庆、贵州,南毗广东、广西,北连湖北,总面积21.18万平方千米。"
]
while True:
try:
driver3=driver.find_element_by_id('listView1')
list3=driver3.find_elements_by_class_name('android.widget.RelativeLayout')
text=list3[-1].find_element_by_id('chat_item_content_layout').text
print('收到消息:%s' % (text))
time.sleep(5)
if(text=='你好,请帮我查阅一下刘邦的简介'):
send_Message(list4[0])
elif(text=="你吃中饭了没"):
send_Message(list4[1])
elif(text=="介绍一下湖南呗!"):
send_Message(list4[2])
except Exception as e:
pass
</code>During execution the phone will request permission to install some components; simply accept.
Recommended Reading
How to Use Python Multiprocessing to Improve Program Performance?
How to Use Python Regular Expressions for Text Matching?
Write an Intelligent Lottery Program with Python (Full Code)
Python Database Secrets: 7 Little‑Known Treasure Tools
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
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.