Table of Contents: 0 Introduction 1 Environment 2 Requirement Analysis 3 Preparation 4 Review of the red envelope snatching process 5 Code comb 6 Postscript

0 the introduction

Mention grab red envelope, you have to mention the Xposed framework, it is a magic device grab red envelope, but the use of the Xposed framework has a prerequisite: mobile phone root, for apple mobile phone then need to jailbreak. Nowadays, it is not easy to root or jailbreak mobile phones, and it will bring some risks to the security of mobile phones. Snatching red packets itself is just an entertainment activity, so the loss is not worth the gain.

In order to automatically grab red packets, can Python help us implement it?

The answer is yes. In this article, we will explore how to implement automatic red envelope snatching in Python.

1 the environment

Operating system: Windows

Python version: 3.7.2

Mobile phone system: Android

2 Demand Analysis

Here we need to achieve automatic snatching red packets. First of all, we need to open wechat, enter the designated group chat, identify wechat red envelopes, and perform the action of grabbing red envelopes. That’s the key step, simple and clear.

Because the computer version of wechat does not have the function of snatching red packets, we can only use wechat, so we need to connect the mobile phone to the computer, and control the mobile phone through the computer to automatically snatching, then we need to ensure that the “ADB” command can be executed normally.

To identify the messages in wechat, whether they are red packets or ordinary information, we judge by the element identification of chat messages. Here we use the “Airtest IDE” tool to achieve.

3 Preparations

3.1 Enabling USB debugging of the mobile phone

To use “ADB” properly, you need to turn on USB debugging, and Settings usually appear in the developer options section of the phone’s system Settings.

After turning on USB debugging on your phone, we next verify that ADB works properly with Airttest IDE.

3.2 Download and install Airtest IDE

First, explain the concepts related to Airttest:

Airtest is a cross-platform, image recognition based UI automation framework for games and apps available on Windows, Android and iOS.

Poco is an automated testing framework based on UI control recognition. It currently supports Unity3D/ Cocos2DX -*/Android native APP /iOS native app/ wechat small program, and can also be used by accessing POco-SDK in other engines.

AirtestIDE is a cross-platform UI automation test editor that enables you to write scripts quickly and easily using Airtest and Poco plug-ins.

Airtest IDE can be downloaded at airtest.netease.com/

The Airtest IDE interface is as follows:

To automate an Android app using the AirtestIDE, the first step is to connect to an Android device.

  1. Please connect the phone with USB cable, and click OK in the allow USB debugging popup window on the phone.
  2. Click the Refresh ADB button in the Connection panel and the device list will refresh.
  3. Click Connect of the corresponding device in the list to complete the connection.
  4. If the device does not spawn, click the Refresh ADB button.

If you encounter problems with the connection, please refer to the official tutorial of Airtest: Android Real Connection

Review the process of snatching red envelopes

Open wechat, on the left Tab page, wechat message list, we need to select the designated group chat from here to grab the red envelope.

In the AirtestIDE, you can pause and freeze the current UI tree to view a UI control precisely. Let’s analyze the element identification in the page first.

Next, enter the group chat where we are going to grab the red envelope, identify the red envelope and perform the action of grabbing the red envelope.

Let’s look at the logo of the red envelope:

Click on the red envelope and record the “open” element identifier in the popup big red envelope page

Here we need to judge whether the red envelope is valid, such as the red envelope that has been received or has been received by themselves is invalid, we can skip these in the implementation.

Now that we’ve got the identifiers for each element we use, it’s time to organize our code.

5 Code Combing

First, you need to open wechat and use Airtest to launch the app. All you need is one line of code, as follows

# Open wechat on your phone
poco(text='WeChat').click()
Copy the code

Gets the names of all the group chats on the current page

# Element identification of group chat messages
Chat_msg = poco(name='com.tencent.mm:id/d1v').offspring('com.tencent.mm:id/b6e')
Get the names of all group chats in the current page
Chat_names = []
Chat_names = list(map(lambda x: x.get_text(), Chat_msg))
Copy the code

Select and enter the specified group chat

# Specify the group chat name for grabbing red packets
chat = input('Please specify group chat name :')
if chat in Chat_names:
    index = Chat_names.index(chat)
    # Click to enter the specified group chatChat_msg[index].click() In the wechat chat page, get all message elements in the current page. msg_list = poco("android.widget.ListView").children()
Copy the code

Traverse the message and look for the red envelope

for msg in msg_list:
    # Wechat red envelope logo
    LuckyMoney = msg.offspring('com.tencent.mm:id/aql')
    # Expired red envelope (such as received, received) logo
    Invalid = msg.offspring('com.tencent.mm:id/aqk')

    # Decide if the red envelope is valid and grab it!
    if LuckyMoney:
        pass
Copy the code

Define the action of grabbing a red envelope in LuckyMoney

if Invalid.exists() and (Invalid.get_text()=='Collected' or Invalid.get_text()=='Taken out'):
    print(F 'red envelope is invalid, skip... ')
    continue
else:
    print(F 'Found a new red envelope, grab it! ')
    poco("com.tencent.mm:id/d1v")
    msg.click()

    click_open = poco("com.tencent.mm:id/d02")
    if click_open.exists():
        click_open.click()
    keyevent('BACK')
Copy the code

6 afterword.

Can we execute this script if we don’t want to install the Airtest IDE on a computer?

Sure, just install the third party library “Pocoui” and ditch it!

pip install pocoui

When creating a project using the AirtestIDE, select Android for the device type and generate an initialized code in the coding area.

from airtest.core.api import *
auto_setup(__file__)

from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)
Copy the code

We also need to include this initialization code in our own Python code.

Again, make sure the phone is connected to the computer and that the “ADB” command works properly when executing the script.

If you are interested in Airtest, you can check out the official document of Airtest: AirtestIDE

Get the complete code for this article