运行环境
Linux,python3.9 pip3 20.0.2
新的改变(不同与群发助手)
需求:群发消息太过于没有心意,虽然好心带给了祝福,但是让人觉得过于敷衍
因此小明同学将批量发送的每一条祝福,都加上了亲友们的昵称,这样每条都“像”是精心编辑的私发效果
有几种发送方式
- 以标签分组 ,不同通讯录标签,有不同批量发送;
- 以群 分组, 按群组,如家庭群,单位群都需要发送不同的祝福语;
代码
- 以遍历方式群发消息
import itchat
def send_message_to_all_friends(message):
itchat.auto_login(hotReload=True)
# 获取所有好友列表
friends = itchat.get_friends(update=True)
# 遍历好友列表发送消息
for friend in friends:
user_name = friend['UserName']
itchat.send(message, toUserName=user_name)
print(f"Message sent to {
friend['NickName']} ({
user_name})")
itchat.logout()
if __name__ == "__main__":
# 要发送的消息
message_to_send = "你好,这是一条群发消息!"
send_message_to_all_friends(message_to_send)
- 分组群发
import itchat
# 用于存储已经发送消息的好友 UserName
sent_friends = set()
def send_message_to_group_members(group_name, message_template):
global sent_friends # 使用全局变量
itchat.auto_login(hotReload=True) # 使用热重载,尝试加载本地缓存的登录状态
# 获取群聊列表
chatrooms = itchat.get_chatrooms(update=True)
# 查找目标群聊
target_group = None
for chatroom in chatrooms:
if