Mobile Development 5 min read

Automating QQ Message Replies on Android Using Python and Appium

This tutorial explains how to set up Python and Appium on a PC, configure Android SDK and device debugging, and write a Python script that automatically reads and replies to QQ messages on an Android phone.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Automating QQ Message Replies on Android Using Python and Appium

The author was inspired by a video on Python app automation and decided to create a program that automatically replies to QQ messages.

Preparation : Install the Appium Python client with pip install appium-python-client , download and start the Appium server (http://appium.io), install JDK and set the JAVA_HOME environment variable, install the Android SDK and set ANDROID_HOME , add adb.exe to the system PATH , and install a screen‑mirroring tool (e.g., Mirroid at https://cn.mirroid.com).

Connect the phone : Use a USB cable, enable Developer Options on the Android device (e.g., on an OPPO phone by tapping the build number repeatedly), then enable USB debugging.

Test the connection : Open a command window and run adb devices -l . If the device appears in the list, the connection is successful. If an error occurs, ensure the adb.exe version used by the mirroring software matches the SDK version.

Code implementation :

<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 = [
    "刘邦,字季,沛郡丰邑(今江苏省丰县)人。中国历史上杰出的政治家、战略家和军事指挥家,汉朝开国皇帝。",
    "还没",
    "湖南省,简称‘湘’,是中华人民共和国省级行政区,省会长沙。"
]

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>

Running the script will cause the phone to install necessary components automatically; simply accept the prompts.

PythonAndroidAutomationMobile TestingQQappium
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.