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)

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

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值