定制化自己的itchat


上篇文章很詳實,可以實現一個低級的微信自動回復機器人,聽說還有用圖靈的,那就變成高級機器人了。

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

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

 

傻瓜式的照搬例子就可以了,代碼幾乎一樣。
高級功能有待后續實現


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM