定製化本身的itchat

上篇文章很詳實,能夠實現一個低級的微信自動回覆機器人,據說還有用圖靈的,那就變成高級機器人了。git

初級目標: 自動回覆好友的信息。微信

 
 
#-*- coding:utf-8 -*-
#微信的庫
import itchat
#導入想要處理的content內容
from itchat.content import *

import re
import time
#這個裝飾器是個類裝飾器吧,貌似功能很強大,括號裏的內容定義了你這個函數想處理的信息類型,msg即是你收到的微信信息,看樣子是個字典。
@itchat.msg_register([TEXT, PICTURE, MAP, CARD, NOTE, SHARING, RECORDING, ATTACHMENT, VIDEO])
def text_reply(msg):
    #調試用的,看看不一樣的信息都長啥樣
    print msg
    #對於不一樣類型的信息,咱們要記錄不一樣的內容來回復,
    #普通文本
    if msg['Type'] == 'Text':
        reply_content = msg['Text']
    
    #圖片,記錄圖片的名字,FileName這個鍵值能夠表示圖片,音頻視頻的名字
    elif msg['Type'] == 'Picture':
        reply_content = r"Picture: " + msg['FileName']
    #若是接收到的是個名片的話,記下推薦信息和暱稱
    elif msg['Type'] == 'Card':
        reply_content = r" " + msg['RecommendInfo']['NickName'] + r" 's card"
    #若是收到的是一個共享的地址的話,用正則分別嘗試去匹配經緯度和位置名稱
    elif msg['Type'] == 'Map':
        x, y, location = re.search("<location x=\"(.*?)\" y=\"(.*?)\".*label=\"(.*?)\".*", msg['OriContent']).group(1,
                                                                                                                    2,
                                                                                                                    3)
        if location is None:
            reply_content = r"Location: longititude->" + x.__str__() + u" jingdu->" + y.__str__()
        else:
            reply_content = r"location: " + location
    #後面這些還沒用過,直接處理了,之後有錯再說
    elif msg['Type'] == 'Note':
        reply_content = r"Note"
    elif msg['Type'] == 'Sharing':
        reply_content = r"Sharing"
    elif msg['Type'] == 'Recording':
        reply_content = r"Voice"
    elif msg['Type'] == 'Attachment':
        reply_content = r"File: " + msg['FileName']
    elif msg['Type'] == 'Video':
        reply_content = r"Video: " + msg['FileName']
    else:
        reply_content = r"Message"
    #獲取信息來源
    friend = itchat.search_friends(userName=msg['FromUserName'])
    #在itchat助手裏進行記錄
    itchat.send(r"Friend:%s -- %s    "
                r"Time:%s    "
                r" Message:%s" % (friend['NickName'], friend['RemarkName'], time.ctime(), reply_content),
                toUserName='filehelper')
    #回覆給信息來源,表示朕已經收到你的消息了,你能夠退下了
    itchat.send(r"I received your news (%s) %s.Reply later.--From itchat(Python)" % (time.ctime(), reply_content),
                toUserName=msg['FromUserName'])
#懶得自定義登陸函數了,用自帶的函數
itchat.auto_login(enableCmdQR=-2,hotReload=True)
itchat.send(r'Hello my friend!',toUserName='filehelper')
#運行起來,等待接受信息
itchat.run()
 
 

 

傻瓜式的照搬例子就能夠了,代碼幾乎同樣。高級功能有待後續實現
相關文章
相關標籤/搜索