当微信群的人数达到100人之后,将不能再通过分享群二维码来邀请用户入群了,必须用户添加好友后,手动去邀请别人才能入群,如果有大量的用户加群,只能一个一个手动加好友后再拉入微信群中,过程十分麻烦。下面介绍一种方法来通过Python实现微信半自动化自动添加好友邀请用户入群
1、准备一台服务器
序列号 | CPU | RAM | HDD | 带宽 | 售价(美元) | 免费试用 |
---|---|---|---|---|---|---|
香港服务器1 | E5-2620 | 32G | 1T HDD | 50M/无限流量 | $196.00 | 立即申请 |
香港服务器2 | E5-2650 | 32G | 1T HDD | 50M/无限流量 | $256.00 | 立即申请 |
香港服务器3 | E5-2680 | 32G | 1T HDD | 50M/无限流量 | $316.00 | 立即申请 |
香港服务器4 | E5-2690 | 32G | 1T HDD | 50M/无限流量 | $336.00 | 立即申请 |
香港服务器5 | E5-2697 | 32G | 1T HDD | 50M/无限流量 | $376.00 | 立即申请 |
香港服务器6 | E5-2620*2 | 32G | 1T HDD | 50M/无限流量 | $376.00 | 立即申请 |
香港服务器7 | E5-2650*2 | 32G | 1T HDD | 50M/无限流量 | $436.00 | 立即申请 |
香港服务器8 | E5-2680*2 | 32G | 1T HDD | 50M/无限流量 | $476.00 | 立即申请 |
香港服务器9 | E5-2690*2 | 32G | 1T HDD | 50M/无限流量 | $556.00 | 立即申请 |
香港服务器10 | E5-2697*2 | 32G | 1T HDD | 50M/无限流量 | $596.00 | 立即申请 |
香港服务器11 | E5-2680v4*2 | 32G | 1T HDD | 50M/无限流量 | $696.00 | 立即申请 |
香港服务器12 | E5-2698v4*2 | 32G | 1T HDD | 50M/无限流量 | $796.00 | 立即申请 |
2、 安装以下相关依赖(有的不需要)
#coding=utf8
import requests
from requests import exceptions
from urllib.request import urlopen
from bs4 import BeautifulSoup
from urllib.parse import urlencode
from threading import Timer
import re
from wxpy import *
import schedule
import time
import http
import json
import datetime
import random
import os
import ctypes
3、一个能够在网页版微信登录的账号(重要)
4、该微信创建群聊,并保存通讯录中(重要)
3、代码部分
3.1、注册机器人
bot = Bot(cache_path=True,console_qr = 2)
bot.enable_puid('wxpy_puid.pkl')
rebot = bot.groups().search('Goodog') // 需要加入的群
3.2、自动通过好友后发送的加群提示
allText = '回复关键词加群: \n\n1、关键词1\n2、关键词2\n3、关键词3\n4、关键词4\n\n '
3.3、自动通过好友请求
@bot.register(msg_types=FRIENDS)
def auto_audit_msg(msg):
new_friend = bot.accept_friend(msg.card)
new_friend.send('添加好友自动回复信息')
new_friend.send_raw_msg(
raw_type=42,
raw_content='
)
new_friend.send(allText)
3.4、关键字回复发送加群邀请链接
@bot.register(Friend, msg_types=TEXT)
def auto_add_msg(msg):
if 'py' in msg.text.lower():
rebot[0].add_members(msg.sender, use_invitation=True)
msg.sender.send('如果加群失败,请等待人工邀请加入!')
3.5、邀请用户入群后发送群公告
invite_compile = re.compile(r'邀请"(.*?)"加入了群聊\s*$') // 判断是否为新用户入群
3.5.1、新用户入群发送的公告
rebot_msg = '''@{atname}\u2005\u2005\u2005 欢迎加入微信群'''
3.5.2、为加入的群进行注册,以用来抓取群消息
@bot.register(rebot, NOTE) // NOTE 为系统通知
def invite_group(msg):
""" 群通知处理 """
text = msg.text # 通知的内容
member = msg.member.name # 消息发送者昵称
invite_names = invite_compile.findall(text) # 判断是否是加入了新用户
if invite_names: # 用于邀请
invite_name = invite_names[0] # 加入者的昵称
if rebot_msg:
note = rebot_msg.format(atname=invite_name)
msg.sender.send(note) # 向群里发送欢迎语句
return
4、运行机器人
while True:
schedule.run_pending()
time.sleep(1)