Build a Python Appium Bot to Auto‑Reply QQ Messages

This step‑by‑step guide shows how to set up Python, Appium, JDK, Android SDK and a screen‑mirroring tool, connect an Android phone, verify the ADB connection, and run a complete Appium script that automatically reads and replies to QQ chats.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Build a Python Appium Bot to Auto‑Reply QQ Messages

Introduction

Recently I watched a video about using Python for 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-client.

1.2 Install Appium Server

Download the server from http://appium.io.

1.3 Install JDK

After installing the JDK, add the JAVA_HOME environment variable pointing to the JDK installation directory.

1.4 Install Android SDK

Add an ANDROID_HOME environment variable that points to the extracted SDK folder and optionally add adb.exe to the system PATH.

1.5 Screen‑mirroring software

I used Mirroid Screen Assistant, which can be downloaded from https://cn.mirroid.com.

2. Connect the Phone

Use a USB cable to connect the phone to the computer and enable Developer Options on the device. For an OPPO phone, go to Settings → About Phone and tap the version number repeatedly until the developer mode is unlocked.

Then open the newly visible Developer Options and enable the USB debugging switch.

3. Test the Connection

In a command window run adb devices -l. If the device appears in the list, the connection is successful.

If an error occurs, it is often caused by mismatched adb.exe versions between the screen‑mirroring tool and the Android SDK; copying one version over the other usually resolves the issue.

4. Code Implementation

The following script demonstrates how to launch QQ, read incoming messages, and send automatic replies based on simple keyword matching.

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)

# Locate recent chat list
chat_list = driver.find_element_by_id('recent_chat_list')
items = chat_list.find_elements_by_class_name('android.widget.LinearLayout')
print('当前QQ消息为%d个' % len(items))

time.sleep(2)
items[0].click()

def send_Message(text2: str):
    input_bar = driver.find_element_by_id('inputBar')
    input_bar.find_element_by_id('input').send_keys(text2)
    input_bar.find_element_by_id('fun_btn').click()
    time.sleep(2)
    print('发送消息:%s' % text2)

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

while True:
    try:
        chat_view = driver.find_element_by_id('listView1')
        msgs = chat_view.find_elements_by_class_name('android.widget.RelativeLayout')
        last_msg = msgs[-1].find_element_by_id('chat_item_content_layout').text
        print('收到消息:%s' % last_msg)
        time.sleep(5)
        if last_msg == '你好,请帮我查阅一下刘邦的简介':
            send_Message(responses[0])
        elif last_msg == '你吃中饭了没':
            send_Message(responses[1])
        elif last_msg == '介绍一下湖南呗!':
            send_Message(responses[2])
    except Exception as e:
        pass

When the script runs, the phone may prompt to install auxiliary components; simply accept the installations.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Automationmobile testingQQAppium
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.