How does Python realize the synchronous forwarding live broadcast of 24 wechat groups (a total of 10000 people)?

Pig brother 66 2020-11-13 07:32:46
python realize synchronous forwarding live

Many people are saying that the wechat web page ( The interface has been blocked , So all wechat can't log into the webpage , This is wrong .

2019 year 7 Wechat made a dynamic security policy adjustment to the wechat of the web page , As a result, a large number of wechat can not log into the webpage version , But some old wechat accounts can still log into the webpage version , Which micro signals can be logged in and which can't be logged in , I don't know about this at present !

We have written four articles on wechat robot project before :

  1. Python Create wechat robot : Explain in detail how to use wxpy Library to create wechat robot .
  2. How to use a robot to monitor the boss's wechat ?: Listen to group members' chat information for forwarding .
  3. python To prevent withdrawal of wechat messages : Explain the principle of wechat withdrawal message and realize the forwarding of withdrawal message .
  4. Hyperdetail Pycharm Deployment project video tutorial : Explain in detail how to deploy wechat robot project to Linux Server .

Today we are going to learn the fifth part of wechat robot series : Multi group forwarding for simultaneous graphic live broadcast

One 、 Background introduction

Brother pig was building a year ago Python When learning group, I said , We need to invite big business people to learn how to broadcast live .

In fact, the article has been written for a long time , But we haven't found a good forwarding software , So there was a delay of a few months .

Recently, brother pig feels that he can't delay any more , So consider developing a multi group live project , Use the original wechat robot project we have made .

The requirements are simple : Can do 24 A large group of wechat ( Ten thousand people ) Synchronous forwarding is OK

I believe that many companies will have the same needs !
 Insert picture description here

Two 、 Functional design

How to do multi group live broadcast ? The simple thing is to use the robot to do the second level forwarding :

  1. Create a new live speaker group
  2. Pull three robots and the speaker into the group
  3. All the messages sent by the speaker to the group are automatically forwarded to the group by the robot
  4. The robot replies in the main group after forwarding
  5. Keep sending messages

For your convenience , Brother pig drew a simple design :
 Insert picture description here
According to the design above , Our functional design is 4 The main elements : Speaker group 、 Speaker 、 Forwarding group 、 Robot Manager .

3、 ... and 、 Function realization

1. Load live 4 Elements

According to the above design , We learned that the first thing we need is a Live speaker group , And then live the group Speaker , Then you need to load what you need to forward Forwarding group .

We can write all these information in a configuration file .
 Insert picture description here
And then when the robot starts , Call the method to load the configuration file , And the method of loading configuration file calls the method of loading live broadcast information !
 Insert picture description here
The first is to load the robot Administrator , Then call the method loading group information in the middle of the method !
 Insert picture description here
Here is load_live Method's detailed code , There are three main things to do : Load the keynote group 、 Check if there are any speakers in the group 、 Load live group .

def load_live(bot):
""" Load the group needed for live broadcast """
# Set the switch 
bot.is_live_mode = config.is_live_mode
if not bot.is_live_mode:
return '\n Live mode is not turned on , Can be found in file is_live_mode Set to True Turn on !'
live_status_detail = ''
# 1、 Load the keynote group 
live_group = bot.groups().search(config.live_group)
if len(live_group) < 1:
bot.live_group = None
bot.is_live_mode = False
return f'\n Speaker group : Group name with... Not found 「{config.live_group}」 The keynote group of !\n Failed to start group live broadcast !\n\n You can send any message in the group , Then the administrator uses the command : Open group live mode , Load live information from New !'
elif len(live_group) > 1:
bot.live_group = live_group[0]
live_status_detail += f'\n Speaker group : Found more than one group name containing 「{config.live_group}」 The keynote group of , The first group is selected by default ({live_group[0]}) As a keynote group !'
bot.live_group = live_group[0]
# 2、 Load presenter 
live_group_members = bot.live_group.members
for member in live_group_members:
if == config.live_speaker:
live_status_detail += f'\n Speaker :「{config.live_speaker}」'
bot.live_speaker = config.live_speaker
live_status_detail += f'\n Speaker : No speaker was found in the group 「{config.live_speaker}」.\n Failed to start group live broadcast !'
bot.is_live_mode = False
return live_status_detail
# 3、 Load forwarding group 
forward_groups = search_groups(bot, config.forward_groups)
bot.forward_groups = forward_groups
live_status_detail += f'\n Forwarding group : Messages will be forwarded to these groups :{str(forward_groups)}, common {len(forward_groups)} individual .\n\n If there is any omission, you can send any message in these groups , Then the administrator uses the command : Open group live mode , Load live information from New !'
return live_status_detail

Here we have loaded the live broadcast 4 Elements : Robot Manager 、 Speaker group 、 Speaker 、 Forwarding group .

2. Implement forwarding messages

After all the required information has been loaded , We can start forwarding .

The general idea is :

  1. Define a method to receive group messages
  2. After receiving the group message, judge whether the message needs to be forwarded , The judgment condition is : Turn on live mode + The current news is from the keynote group + The person who sent the message was the speaker
  3. If the three conditions of appeal are met , Then forward the message to the group
  4. After forwarding, reply in the main speaker group : Once the forwarding is finished , Then the speaker can say the next sentence

Let's take a look at the code
 Insert picture description here
Let's see **wx_command.remote_forward(msg)** The specific code of !

def remote_forward(msg):
""" Forward a message """
forward_groups = []
for group in
msg.forward(group, suffix='')
return forward_groups

This method is very simple , Loop through the groups that need to be forwarded , Then send one by one , There is a random pause within one second after sending , This is to prevent messages from being sent too often to send out .

Here is the code for multi group forwarding , Now we can start testing .

Four 、 test

1. Testing process

After the code is written, we can start testing , The whole idea of the test is :

  1. newly build A group of speakers , Then pull the robot and the speaker in
  2. newly build 24 Test groups , Pull the robot in
  3. Start project , Let the robot enter live mode
  4. The speaker simulated the real live broadcast in the group , Pay attention to control the live broadcast speed
  5. The live broadcast is over , Turn off the robot

2. A robot forwards 24 Group

At the beginning, brother pig was thinking about : With a robot , Then forward it to 24 In groups , Configuration files are like this :
 Insert picture description here
But found that this will lead to the message can not be sent out , Wechat prompt messages are sent too frequently .
 Insert picture description here
No news can be sent out , Here, brother pig almost gave up , I don't think we can forward more than one group .

3. Three wechat forwarding 24 Group

Brother pig will go to the Internet to see if there is any free forwarding software , Then I looked at wetools( Based on a windows Wechat operation tools ), They found that a paid version of a wechat can also be forwarded at most 9 Group .

So brother pig wondered if a wechat can only be forwarded at most 9 A group ? Then I can use three wechat for forwarding ?
 Insert picture description here
The above configuration file means that I need to start the program three times , Change three wechat , Then every time I change forward_groups To configure .

Many students don't know :PyCharm How to run a project multiple times at the same time ?
 Insert picture description here
 Insert picture description here
 Insert picture description here
Forward with three wechat 24 Wechat groups , Looks like a good idea , Can we succeed in practice ?
 Insert picture description here
This live test is almost on 30 minute , Completely correct operation without message loss and robot disconnection .

Sent during : written words 、 picture 、 file 、 voice .

5、 ... and 、 The actual live broadcast

24 Live broadcast in groups of wechat , To be honest, few people can accomplish , I consulted a lot of colleagues who had done group live broadcast before , They're basically artificial copies .

however , The day before the live broadcast, the test went smoothly , Give pig a lot of confidence !

The actual live broadcast process is probably :

  1. Modify the announcement a few hours in advance to release the live group news at night
  2. No new people are allowed to join the group , Prevent the advertisement from coming in
  3. Punch in half an hour in advance , After the end of the live broadcast, randomly kick out several members of the group who didn't play cards to occupy the pit
  4. Before live broadcast 5 Stop clock in minutes , Start preparing for the live broadcast
  5. On the evening of 20:00 Start the live broadcast on time
  6. Probably 20:40 End of live broadcast , Collect feedback

 Insert picture description here
Because it was live , Forget the screenshot 24 Unread message graph of groups , I can only find some screenshots of chat records for you to see .

6、 ... and 、 Problems encountered

In fact, the live code is very simple , Not much , But because there are so many groups , The user volume is large, so it is inevitable to encounter many problems , Now, brother pig will sum up the problems encountered :

  1. The problem of the maximum number of forwarding groups : At the beginning, brother pig thought of using only one robot to forward to 24 In groups , But the news can't be sent out , Being prompted too often , Finally, I see wetools Their maximum number of forwarding groups is 9 individual , So we consider using three wechat , Every forwarding 8 Group .
  2. Forwarding mode problem : It used to be a robot , The speaker sends it directly to the forwarding robot , Later it became three robots , How to send messages to three robots ? At the suggestion of friends, we use the speaker group forwarding mode .
  3. Can't load group problem : Sometimes you can't load the group you want after you start the project , At this time, you need to send any message in the group , Turn on the live mode again , That's all right. , Bear in mind ! Bear in mind !
  4. In the actual live broadcast, there is a problem that the robot is disconnected : During the test, there was no disconnection of the robot , But once in the actual live broadcast , Just log in to the robot again .
  5. Many wechat users can't log in to the webpage version of wechat : This is really a headache , It's hard to find a few people who can log into the web page .

Of course, the above is about some technical problems , About the operation problems in the live broadcast process , such as : After stopping punching, someone still punches to affect reading 、 The live broadcast is too fast 、 If the content is not simple enough, the problems will not be listed one by one , If you want to learn more about these details, you can add brother Zhu's wechat .

7、 ... and 、 summary

In recent years, , We guo Home slowly by “ Made in China ” Evolved into “ Created in China ”, This is a difficult and exciting thing .

It's the same for us who do technology , A lot of work Ctrl+C(V) operation , It's not exactly what we understand “ manufacture ” Well !

Apply technology to life , To solve some pain points and problems , Create something interesting , I think that's the real technological creation !

In solving their own problems, but also to solve the difficulties of others , Maybe at this time , Opportunity is coming to you !

Source code :

本文为[Pig brother 66]所创,转载请带上原文链接,感谢

  1. 利用Python爬虫获取招聘网站职位信息
  2. Using Python crawler to obtain job information of recruitment website
  3. Several highly rated Python libraries arrow, jsonpath, psutil and tenacity are recommended
  4. Python装饰器
  5. Python实现LDAP认证
  6. Python decorator
  7. Implementing LDAP authentication with Python
  8. Vscode configures Python development environment!
  9. In Python, how dare you say you can't log module? ️
  10. 我收藏的有关Python的电子书和资料
  11. python 中 lambda的一些tips
  12. python中字典的一些tips
  13. python 用生成器生成斐波那契数列
  14. python脚本转pyc踩了个坑。。。
  15. My collection of e-books and materials about Python
  16. Some tips of lambda in Python
  17. Some tips of dictionary in Python
  18. Using Python generator to generate Fibonacci sequence
  19. The conversion of Python script to PyC stepped on a pit...
  20. Python游戏开发,pygame模块,Python实现扫雷小游戏
  21. Python game development, pyGame module, python implementation of minesweeping games
  22. Python实用工具,email模块,Python实现邮件远程控制自己电脑
  23. Python utility, email module, python realizes mail remote control of its own computer
  24. 毫无头绪的自学Python,你可能连门槛都摸不到!【最佳学习路线】
  25. Python读取二进制文件代码方法解析
  26. Python字典的实现原理
  27. Without a clue, you may not even touch the threshold【 Best learning route]
  28. Parsing method of Python reading binary file code
  29. Implementation principle of Python dictionary
  30. You must know the function of pandas to parse JSON data - JSON_ normalize()
  31. Python实用案例,私人定制,Python自动化生成爱豆专属2021日历
  32. Python practical case, private customization, python automatic generation of Adu exclusive 2021 calendar
  33. 《Python实例》震惊了,用Python这么简单实现了聊天系统的脏话,广告检测
  34. "Python instance" was shocked and realized the dirty words and advertisement detection of the chat system in Python
  35. Convolutional neural network processing sequence for Python deep learning
  36. Python data structure and algorithm (1) -- enum type enum
  37. 超全大厂算法岗百问百答(推荐系统/机器学习/深度学习/C++/Spark/python)
  38. 【Python进阶】你真的明白NumPy中的ndarray吗?
  39. All questions and answers for algorithm posts of super large factories (recommended system / machine learning / deep learning / C + + / spark / Python)
  40. [advanced Python] do you really understand ndarray in numpy?
  41. 【Python进阶】Python进阶专栏栏主自述:不忘初心,砥砺前行
  42. [advanced Python] Python advanced column main readme: never forget the original intention and forge ahead
  43. python垃圾回收和缓存管理
  44. java调用Python程序
  45. java调用Python程序
  46. Python常用函数有哪些?Python基础入门课程
  47. Python garbage collection and cache management
  48. Java calling Python program
  49. Java calling Python program
  50. What functions are commonly used in Python? Introduction to Python Basics
  51. Python basic knowledge
  52. Anaconda5.2 安装 Python 库(MySQLdb)的方法
  53. Python实现对脑电数据情绪分析
  54. Anaconda 5.2 method of installing Python Library (mysqldb)
  55. Python implements emotion analysis of EEG data
  56. Master some advanced usage of Python in 30 seconds, which makes others envy it
  57. python爬取百度图片并对图片做一系列处理
  58. Python crawls Baidu pictures and does a series of processing on them
  59. python链接mysql数据库
  60. Python link MySQL database