根据用户的备注名来给用户发送特定的消息,在itchat获取的friends列表中,username每次登陆之后都会出现变化。
1 #-*- coding:utf-8 -*- 2 """ 3 __author__ = BlingBling 4 """ 5 6 import time 7 import itchat 8 import xlrd 9 10 11 # 登录 12 if itchat.check_login() == 200: 13 print("已经登录") 14 else: 15 itchat.login() 16 #itchat.run() 17 18 def get_friends(): 19 # 获取好友列表,备注名和用户名对应 20 friends = itchat.get_friends(update=True)[0:] 21 nfriends = {} 22 for friend in friends: 23 if friend["RemarkName"]: 24 nfriends[friend["RemarkName"]] = friend["UserName"] 25 else: 26 nfriends[friend["NickName"]] = friend["UserName"] 27 return nfriends 28 29 filename = "wechatcontact.xlsx" 30 def contact(filename): 31 """获取产品名和对应微信备注名""" 32 workbook = xlrd.open_workbook(filename) 33 table = workbook.sheets()[0] #选择第一个表 34 nrows = table.nrows #行数 35 print(nrows) 36 contact = {} 37 for row in range(nrows): 38 nickname = table.cell(row,5).value 39 conform = table.cell(row,8).value 40 if conform == "√": 41 if nickname not in contact: 42 contact[nickname.strip()] = table.cell(row,2).value 43 else: 44 contact[nickname.strip()] += "、" + table.cell(row, 2).value 45 return contact 46 47 48 49 # groups = itchat.get_chatrooms(update=True)[0:] #获取聊天群 50 message_ = "nickname您好,麻烦发送product_name8月份的银行流水,如无流水烦请告知银行存款余额,谢谢!" 51 52 53 # itchat.send_msg(message,username),根据用户名来发送微信号,用户名是一串加密数据 54 # 用户名是一串加密的数据,每次登陆之后都得重新获取,但是可以通过nickname等方式来匹配上 55 def send_message(friends,contact): 56 """发送消息""" 57 for key,value in contact.items(): 58 try: 59 username = friends[key] 60 message = "%s您好,麻烦发送%s的8月份银行流水,如无流水烦请告知银行存款余额,谢谢!"%(key,value) 61 itchat.send_msg(message, username) 62 print("发送消息:%s" % message, "to %s" %key) 63 except Exception as e : 64 print("消息发送失败,接收人:%s "%key,"错误为%s"%e) 65 67 friends = get_friends() 68 contact = contact(filename) 69 send_message(friends,contact) 70 71 72
原文地址:https://www.cnblogs.com/starkbling/p/9634477.html
时间: 2024-10-09 08:29:21