运行环境
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 chatroom['NickName'] == group_name:
target_group = chatroom
break
if target_group:
# 获取群聊成员列表
members = itchat.update_chatroom(target_group['UserName'], detailedMember=True)
# 遍历群聊成员发送消息
for member in members['MemberList']:
user_name = member['UserName']
if user_name and user_name not in sent_friends:
# 获取好友的备注或昵称
friend_name = member['RemarkName'] or member['NickName']
# 截取倒数两个字符
last_two_chars = friend_name[-2:]
# 替换消息模板中的 "xxx" 为截取到的字符
customized_message = message_template.replace("xxx", last_two_chars)
itchat.send(customized_message, toUserName=user_name)
sent_friends.add(user_name) # 将已发送消息的好友添加到集合
print(f"Message sent to {friend_name} ({user_name}) in group {group_name}")
else:
print(f"Skipping member {member['NickName']} without UserName or already sent")
else:`在这里插入代码片`
print(f"Group {group_name} not found.")
itchat.logout() # 注销登录
if __name__ == "__main__":
# 要发送的新消息模板
message_template = "初一啦!小明同学祝xxx新的一年,龙年快乐!事业飞升!日进斗金!"
# 发送新消息给特定群组的成员
group_name_to_send = "Group name" # 替换为目标群组的名称
send_message_to_group_members(group_name_to_send, message_template)
- 消息回复(当有人回复消息的时候,也可以托管回复消息)
import itchat
import time
def check_unread_messages():
itchat.auto_login(hotReload=True)
while True:
unread_messages = itchat.get_unread()
for message in unread_messages:
sender_user_name = message['FromUserName']
sender_nick_name = itchat.search_friends(userName=sender_user_name).get('NickName', 'Unknown')
print(f"Received unread message from {sender_nick_name} ({sender_user_name}): {message['Text']}")
# 在这里添加批量回复的逻辑
reply_message = "你好,已收到你的消息!"
itchat.send(reply_message, toUserName=sender_user_name)
print(f"Sent reply to {sender_nick_name} ({sender_user_name})")
time.sleep(10) # 每隔10秒检测一次
if __name__ == "__main__":
check_unread_messages()
备注:这个示例中,check_unread_messages 函数定期检测未读消息,获取发送未读消息的好友信息,并发送一条回复消息。请注意,这只是一个简单的示例,实际情况可能需要更复杂的逻辑,例如处理不同类型的消息、记录已回复的好友等。
满足不同需求的版本
需求比较大的几种发送方式
- 以标签分组 ,不同通讯录标签,有不同批量发送;
send_message_to_tag_members(tag_name, message)
import itchat
# 用于存储已经发送消息的好友 UserName
sent_friends = set()
def send_message_to_tag_members(tag_name, message):
global sent_friends # 使用全局变量
itchat.auto_login(hotReload=True) # 使用热重载,尝试加载本地缓存的登录状态
# 获取所有好友列表
friends = itchat.get_friends(update=True)
# 遍历好友列表,查找包含指定标签的好友并发送消息
for friend in friends:
if tag_name in friend['RemarkName']:
user_name = friend['UserName']
if user_name and user_name not in sent_friends:
itchat.send(message, toUserName=user_name)
sent_friends.add(user_name) # 将已发送消息的好友添加到集合
print(f"Message sent to {friend['NickName']} ({user_name}) with tag {tag_name}")
else:
print(f"Skipping friend {friend['NickName']} without UserName or already sent")
itchat.logout() # 注销登录
if __name__ == "__main__":
# 要发送的新消息
new_message_to_send = "初一啦!祝您新的一年,龙年快乐!万事大吉!心想事成!身体健康!阖家欢乐!"
# 发送新消息给具有特定标签的好友
tag_name_to_send = "Your Tag Name" # 替换为目标标签的名称
send_message_to_tag_members(tag_name_to_send, new_message_to_send)
- 以群 分组, 按群组,如家庭群,单位群都需要发送不同的祝福语;
# 获取群聊列表
chatrooms = itchat.get_chatrooms(update=True)
# 查找目标群聊
target_group = None
for chatroom in chatrooms:
if chatroom['NickName'] == group_name:
target_group = chatroom
break
if target_group:
# 获取群聊成员列表
members = itchat.update_chatroom(target_group['UserName'], detailedMember=True)
# 遍历群聊成员发送消息
for member in members['MemberList']:
库(需要的依赖)
pip install itchat
如果出现bug,尝试执行
pip install itchat-uos
资料: 前置确认.
运行截图: 效果展示:
私发给家人的:
常见bug
mismatched tag: line 64, column 4
体验
好友们都被蒙混过关了 好感度+1
新的一年,也祝CSDN的好兄弟们心想事成 日进斗金
如果好用给博主一个点赞 关注,有疑问请留言评论,程序媛只要看到就会回复的