1. An overview of the

I just got two messages, one good and one bad.

First the good news, the good news is that someone in the wechat group to send red envelopes, happy ~

But on second thought, a few times before a red envelope did not grab, this time?? An involuntary sigh…

After a while, the inner mood gradually calmed down.

I thought to myself, “Do you just give up? Have instant noodles for dinner (instant noodles feel offended)? But manual snatching is definitely out of the question, after all, hands can do nothing! Then we can only try to achieve automatic red envelope snatching through programming!”

Now to clarify the idea, the basic situation of wechat group red envelope: each red envelope will have some time interval with the last one, the basic idea of automatic red envelope snatching is as follows:

  • Manually empty the red envelope records in the previous wechat group

  • Execute the automatic red envelope snatching procedure, enter the wechat group that sends the red envelope (you can temporarily put it on the top), detect whether there is a red envelope in the group, and click the red envelope when it is found

  • Detect whether the red envelope has been claimed (judge whether the red envelope has an open character). If the red envelope has not been claimed, click the open character to claim the red envelope, and then return to the group chat interface to delete the record of the red envelope that has been claimed; If the red envelope has been claimed, go back to the group chat interface to delete the record of the red envelope that has been claimed, and so on

2. Environmental

The main environment of this paper is as follows:

  • Win7
  • Millet 5 s
  • Python3.7
  • Appium1.5
  • WeChat 7.0.20

If you are not familiar with the environment setup, you can see: Python + Appium automatic operation wechat entry and I used Python to find out all the people who deleted my wechat and automatically deleted them.

3. The implementation

Now let’s start to manually type the code. Let’s take a look at the implementation.

First look at the configuration information, the code implementation is as follows:

desired_caps = {
    "platformName": "Android".# system
    "platformVersion": "8.0.0".# System version number
    "deviceName": "m5s".# device name
    "appPackage": "com.tencent.mm".# the package name
    "appActivity": ".ui.LauncherUI".# App starts the main Activity
    'unicodeKeyboard': True.Use your own input method
    'noReset': True Save session information to avoid re-login
}
Copy the code

After clicking the red envelope, we need to judge whether the red envelope is received, that is, whether there is an open character, as shown in the picture:

So we define a method to determine whether the element exists, the code implementation is as follows:

Check whether the element exists
def is_element_exist(driver, by, value) :
    try:
        driver.find_element(by=by, value=value)
    except Exception as e:
        return False
    else:
        return True
Copy the code

Because red envelopes, whether received by themselves or others, will be deleted after the red envelope record, so we define a method to delete the red envelope, the code implementation is as follows:

Delete the record of the red envelope after receiving it
def del_red_envelope(wait, driver) :
    # Long press the red envelope
    r8 = wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/r8")))
    TouchAction(driver).long_press(r8).perform()
    # Click long press after display delete
    wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/gam"))).click()
    # Click the delete option in the pop-up box
    wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/doz"))).click()
Copy the code

After long press to get the red envelope effect picture is as follows:

Click and long press to display the effect picture of the deleted item as follows:

We then look at the main program implementation into the red envelope group, the code is as follows:

while True:
    # Click if there is a red envelope
    wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/r8"))).click()
    print("Click on the red envelope")
    # Determine whether the red envelope was received
    is_open = is_element_exist(driver, "id"."com.tencent.mm:id/den");
    print("Is the red envelope received?", is_open)
    if is_open == True:
        # Red envelope not received, open red envelope
        wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/den"))).click()
        # Return to group chat
        wait.until(EC.element_to_be_clickable((By.ID, "com.tencent.mm:id/dm"))).click()
        Delete the record of red packets received
        del_red_envelope(wait, driver)
    else:
        # Return to group chat
        driver.keyevent(4)
        Delete the record of red packets received
        del_red_envelope(wait, driver)
Copy the code

Source code in the public number Python minor 2 background reply 201123 to obtain.