突然想到的,然后在网上找到了相关代码,可以改,但没必要,程序如下:
(今天除夕,就不多解释了,不难理解,在这里给大家拜个晚年,狗年吉祥)
import itchat
import requests
import time
import random
from itchat.content import *
# 用于记录回复过的好友
replied = []
# 获取新年祝福语
def GetRandomGreeting():
res = requests.get("http://www.xjihe.com/api/life/greetings?festival=新年&page=10", headers = {'apiKey':'sQS2ylErlfm9Ao2oNPqw6TqMYbJjbs4g'})
results = res.json()['result']
return results[random.randrange(len(results))]['words']
# 发送新年祝福语
def SendGreeting(msg):
global replied
friend = itchat.search_friends(userName=msg['FromUserName'])
if friend['RemarkName']:
itchat.send('自动回复:' + (friend['RemarkName']+','+GetRandomGreeting()), msg['FromUserName'])
else:
itchat.send(‘自动回复:' + (friend['NickName']+','+GetRandomGreeting()), msg['FromUserName'])
#replied.append(msg['FromUserName'])
# 文本消息
@itchat.msg_register([TEXT])
def text_reply(msg):
if ('年' in msg['Text'] or '除夕' in msg['Text'] or '快乐' in msg['Text'] )and msg['FromUserName'] not in replied:
SendGreeting(msg)
# 其他消息
@itchat.msg_register([PICTURE, RECORDING, VIDEO, SHARING])
def others_reply(msg):
if msg['FromUserName'] not in replied:
SendGreeting(msg)
if __name__ == '__main__':
itchat.auto_login()
itchat.run()
网友评论