1. 程式人生 > >itchat實現命令列聊天+記錄聊天圖片/表情

itchat實現命令列聊天+記錄聊天圖片/表情

itchat可以很簡單的實現自動回覆,通過增加執行緒,修改程式碼實現完全終端微信聊天。

#coding=utf8

import requests

import itchat

import time

import csv

import datetime

import pandas as pd

import numpy as np

import threading

KEY = '8edce3ce905a4c1dbb965e6b35c3834d'

threads=[]

def get_response(msg): #通過改下面的程式碼,也可以自動回覆

apiUrl = 'http://www.tuling123.com/openapi/api'

data = {

'key' : KEY,

'info' : msg,

'userid' : 'wechat-robot',

}

try:

r = requests.post(apiUrl, data=data).json()

return r.get('text')

except:

return

@itchat.msg_register(itchat.content.TEXT)

def mes_reply(): #定義回覆函式,回覆是,先輸入想要回復的人或群的前面的標識數字,然後輸入一個空格,再輸入回覆訊息即可回覆。

while(1):

try:

MES=[]

MES=raw_input()

i=int(MES[0]) #獲取標識

s=MES[2:]

itchat.send("%s"%(s.decode('utf-8')),mes_list[i])

except:

print "error"

mes_list=[] #存放FromUserName

t = threading.Thread(target=mes_reply) #開啟並行執行緒

t.setDaemon(True)

t.start()

@itchat.msg_register(itchat.content.TEXT)

def tuling_reply(msg):

if (msg['FromUserName'] not in

mes_list) :

mes_list.append(msg['FromUserName'])

print mes_list.index(msg['FromUserName']),msg['User']['NickName'].encode('utf-8'),msg['User']['RemarkName'].encode('utf-8'),msg['Content'].encode('utf-8')

@itchat.msg_register(itchat.content.TEXT,isGroupChat=True)

def text_reply(msg):

if (msg['User']['UserName'] not in mes_list) :

mes_list.append(msg['User']['UserName'])

csv_writer.writerow(row)

printmes_list.index(msg['User']['UserName']),msg['User']['NickName'].encode('utf-8'),msg['ActualNickName'].encode('utf-8'),msg['User']['RemarkName'].encode('utf-8'),msg['Content'].encode('utf-8')

@itchat.msg_register([itchat.content.PICTURE,itchat.content.RECORDING,itchat.content.ATTACHMENT,itchat.content.VIDEO],isGroupChat=True)

def download_files(msg): #儲存記錄接受的圖片、表情等資訊。

print msg['Text']

msg['Text']('地址'+msg['FileName'])

@itchat.msg_register([itchat.content.PICTURE,itchat.content.RECORDING,itchat.content.ATTACHMENT,itchat.content.VIDEO])

def download_file(msg):

print msg['Text']

msg['Text']('地址'+msg['FileName'])

itchat.auto_login(hotReload=True)

itchat.run()