用python批量發送微信消息


根據用戶的備注名來給用戶發送特定的消息,在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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM