Sell wechat machines on the Internet for hundreds of months, and Python can handle dozens of lines of code

Posted by rajbal on Wed, 27 Nov 2019 07:52:40 +0100

Preface

The text and pictures of the article are from the Internet, only for learning and communication, and do not have any commercial use. The copyright belongs to the original author. If you have any questions, please contact us in time for handling.

Author: Story Film

PS: if you need Python learning materials, you can click the link below to get them by yourself

http://note.youdao.com/noteshare?id=3054cce4add8a909e784ad934f956cef

Note: there must be a wechat that can log into the webpage

You can log in the wechat web page to check

1. Demand scenario

Many small partners who manage a large number of wechat communities have such scenarios. When the number of wechat groups reaches 100, they will not be able to join the group through the shared group QR code. They must add their own friends, and then invite them manually to join the group. If there are a large number of users to join the group, they can only pull one by one manually. It's beautiful to think about it and cry manually. .

2. Preparations

1. A computer that does not shut down OR a server

2. Install the following dependencies (some are not required)

 1 #coding=utf8
 2 import requests
 3 from requests import exceptions
 4 from urllib.request import urlopen
 5 from bs4 import BeautifulSoup
 6 from urllib.parse import urlencode
 7 from threading import Timer
 8 import re
 9 from wxpy import *
10 import  schedule
11 import  time
12 import http
13 import  json 
14 import datetime
15 import random
16 import os
17 import ctypes

 

3. An account that can log in to wechat on the web (important)

4. This wechat creates group chat and saves it in the address book (important)

3. Code part

3.1 registration robot

1 bot = Bot(cache_path=True,console_qr = 2)
2 bot.enable_puid('wxpy_puid.pkl')
3 rebot = bot.groups().search('Goodog') // Need to join

 

Group

3.2. Group adding prompt sent automatically after friends

1 allText = 'Reply keywords plus group: \n\n1,Wool (join the wool group)\n2,py(Join the robot experience group)\n3,Mutual powder (add public numbers to each other)\n4,More\n\n -PS: If the reply key is invalid, please wait for manual pull in.'

 

3.3. Request through friends automatically

1 @bot.register(msg_types=FRIENDS)
2 def auto_audit_msg(msg):
3     new_friend = bot.accept_friend(msg.card)
4     new_friend.send('I am Goodog Little assistant, if you want to have a robot like mine, please add the public number.')
5     new_friend.send_raw_msg(
6         raw_type=42,
7         raw_content='<msg username="infopush" nickname="Xiao Yao"/>'
8     ) 
9     new_friend.send(allText)

 

3.4. Keyword reply sending plus group invitation link

1 @bot.register(Friend, msg_types=TEXT)
2 def auto_add_msg(msg):
3     if 'py' in msg.text.lower():
4         rebot[0].add_members(msg.sender, use_invitation=True)
5         msg.sender.send('If joined py Group failed, please wait for manual invitation to join!!')

 

3.5 send group announcement after inviting users to the group

1 invite_compile = re.compile(r'Invitation"(.*?)"Joined the group chat\s*$') // Determine whether to join the group for new users

 

3.5.1 announcements sent by new users entering the group

1 rebot MSG = '@ {atname} \ u2005 \ u2005 welcome to join the group \ n
 2 ⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡⚡!!! \n
 3 ㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐㈐. \n
 4 🙋 how to ask questions: \ n
 5 1. Don't ask meaningless questions 
6 2. It is better to post screenshots before asking questions. A kind of 
7 3. Clear description and sufficient information. A kind of
 8 4. If you want the source code, please pay attention to [Edit] and reply to [Python] to get "'

 

3.5.2 register for the joined group to capture group messages

 1 @bot.register(rebot, NOTE)  // NOTE Notify for system
 2 def invite_group(msg):
 3     """ Group notification processing """
 4     text = msg.text # Contents of the notice
 5     member = msg.member.name # Message sender nickname
 6     invite_names = invite_compile.findall(text)  # Determine whether a new user has been added
 7     if invite_names:  # For invitation
 8         invite_name = invite_names[0]  # Nickname of the participant
 9         if rebot_msg:
10             note = rebot_msg.format(atname=invite_name)
11             msg.sender.send(note)  # Send welcome statements to the group
12         return

 

4. Running robot

1 while True:
2     schedule.run_pending()
3     time.sleep(1)

Topics: Python JSON