@
时,图灵机器人自动回复pip3 intall itchat
#!/usr/bin/env python3 import time import itchat import requests import json from itchat.content import * blessing = ['祝愿', '福', '快乐', '新年', '过年', '吉祥', '大吉', 'new year'] # 收到的祝福关键词 send_me_friends = {} # 不发送的好友微信名 set_time = '2019-02-01 00:00:00' # 设置群发时间 you_blessing = '祝愿您在新的一年里,全部的但愿都能如愿,全部的梦想都能实现,全部的等候都能出现,全部的付出都能兑现。' # 你本身定义的新年祝福 blacklist_dict = {} send_status = 0 @itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING]) def text_reply(msg): global send_status if not send_status: if get_sys_time() >= set_stamp(): send_status = 1 send_msg(msg) # 当消息不是由本身发出的时候 if not msg['FromUserName'] == myUserName: # 发送一条提示给文件助手 itchat.send_msg(u"[%s]收到好友@%s 的信息:%s\n" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])), msg['User']['NickName'], msg['Text']), 'filehelper') for i in blessing: if i in msg['Text']: # 先给本身发的就加入列表,以后计时发送的时候就跳过这些用户 send_me_friends[msg['User']['NickName']] = msg['User']['RemarkName'] return u'[主人比较懒,早早的去撸代码(shuijiao)去了]机器人助理带主人对您说:{}'.format(you_blessing) elif check_msg(msg): return else: continue return u'[Medivh的机器人助理]说:{}'.format(tuling(msg['Text'])) @itchat.msg_register(TEXT, isGroupChat=True) def group_reply(msg): # 只有群里@本身的时候回复,其余忽视,包括@all if msg['IsAt']: itchat.send_msg(u"[%s]收到好友@%s 的信息:%s\n" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])), msg['User']['NickName'], msg['Text']), 'filehelper') return u'[Medivh的机器人助理]说:{}'.format(tuling(msg['Text'])) def send_msg(msg): # 若是都到你设置的时间了,仍是没有人给你祝福,我看仍是洗洗睡吧 friends = itchat.get_friends(update=True) for name in friends[1:]: if check_msg(msg): return if name.NickName not in send_me_friends: print('正在给{}发消息~'.format(name.NickName)) itchat.send_msg(u"[%s]收到好友@%s 的信息:%s\n" % (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(msg['CreateTime'])), 'name.NickName', # msg['Text']), 'filehelper') return u'{}'.format(you_blessing) else: pass return def tuling(info): appkey = "e5ccc9c7c8834ec3b08940e290ff1559" url = "http://www.tuling123.com/openapi/api?key=%s&info=%s" % (appkey, info) req = requests.get(url) content = req.text data = json.loads(content) answer = data['text'] return answer def get_sys_time(): sys_time = time.time() return sys_time def set_stamp(): # 将其转换为时间数组 time_array = time.strptime(set_time, '%Y-%m-%d %H:%M:%S') # 转换为时间戳 time_stamp = int(time.mktime(time_array)) return time_stamp def check_msg(msg): if msg['Text'] == '消息已发出,但被对方拒收了。': blacklist_dict[msg['User']['NickName']] = msg['User']['RemarkName'] return True else: return False if __name__ == '__main__': itchat.auto_login(hotReload=True) # 获取本身的UserName myUserName = itchat.get_friends(update=True)[0]["UserName"] itchat.run() print('今年主动给你发送半年消息的有{}我的,名单以下:\n {}'.format(len(send_me_friends) , send_me_friends)) print('今年被拉黑了{}次,名单以下:\n {}'.format(len(blacklist_dict), blacklist_dict))