python 微信機器人
阿新 • • 發佈:2018-11-30
# coding:utf-8 from wxpy import * # 指定圖片檔案的路徑 image_path = "./time.png" # 初始化微信機器人 bot = Bot() #給機器的好友列表 robotFriendList = [] #群聊列表 robotGooupsList = [] #需要傳送的訊息 infos=[ { 'type': 'image', #訊息型別 'info': './time.png' #訊息 }, { 'type': 'text', 'info': '1' }, ] #群聊成員 groupList = [] def getmy_friends(): # 獲取所有好友 global robotFriendList,robotGooupsList friend_search = bot.friends() for i in friend_search: # 儲存好友進list robotFriendList.append(i) def getmy_groups(): # 獲取所有的群列表 gooupsm = bot.groups(update=False, contact_only=False).search() print(gooupsm) for i in gooupsm: # 儲存群進list robotGooupsList.append(i) print(robotGooupsList) #給朋友傳送訊息 事件1 def Friend_send_code(): #給好友傳送訊息 for m in infos: send_msg(m['type'], m['info']) #給朋友傳送訊息 事件2 #type:訊息類別 #val:需要傳送的訊息 def send_msg(type,val): if type == 'image': for i in robotFriendList: i.send_image(val) elif type == 'text': for i in robotFriendList: i.send_image(val) #獲取群聊中的成員 def getGroupsList(all=True, groupName=1): global groupList if all: #獲取所有群聊中的成員 groupList = [] # 初始化 #獲取所有群中的成員 for i in robotGooupsList: found = i.search() for m in found: groupList.append(m) else:#獲取單個群聊中的成員 groupList = [] # 初始化 found = groupName.search() for m in found: #儲存進list groupList.append(m) print(groupList) getmy_friends()