美文网首页Python小程序
python3:春节自动回复祝福(微信)

python3:春节自动回复祝福(微信)

作者: 末一哟 | 来源:发表于2019-02-04 17:02 被阅读77次

    突然想到的,然后在网上找到了相关代码,可以改,但没必要,程序如下:
    (今天除夕,就不多解释了,不难理解,在这里给大家拜个晚年,狗年吉祥)

    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()
    

    我们没能力发现知识,我们只是知识的寄生虫

    相关文章

      网友评论

        本文标题:python3:春节自动回复祝福(微信)

        本文链接:https://www.haomeiwen.com/subject/wcnpsqtx.html