#!/usr/bin/env python
# coding:utf8
import sys
reload(sys)
sys.setdefaultencoding( "utf8" )

import urllib
import urllib2

headers = {}
headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.157 Safari/537.36"

def upload_message(username, userid, message_type, content, url):
    data = urllib.urlencode({
        'username': username,
        'userid': userid,
        'message_type': message_type,
        'content': content,
        'url': url,
        'token': 'SJTUHONLAN'
        })
    request = urllib2.Request('http://zhanghonglun.cn/fullstack/message', headers)
    opener = urllib2.build_opener(urllib2.HTTPCookieProcessor()) 
    response = opener.open(request, data) 
    result = response.read()

import itchat
from itchat.content import *

groups = []

@itchat.msg_register([TEXT, MAP, CARD, NOTE, SHARING], isGroupChat=False)
def text_reply(msg):
    '''
    if msg['Type'] == TEXT:
        itchat.send('%s' % (msg['Content']), msg['FromUserName'])
    elif msg['Type'] == MAP:
        itchat.send('%s%s' % (msg['Content'][:msg['Content'].find('/cgi-bin/')], msg['Url']), msg['FromUserName'])
    elif msg['Type'] == SHARING:
        itchat.send('%s\n%s' % (msg['Text'], msg['Url']), msg['FromUserName'])
    else:
        itchat.send('%s: %s' % (msg['Type'], msg['Text']), msg['FromUserName'])
    '''
    itchat.send('这是我的小号,暂无调戏功能,有事请加我大号:Honlann', msg['FromUserName'])

@itchat.msg_register([PICTURE, RECORDING, ATTACHMENT, VIDEO], isGroupChat=False)
def download_files(msg):
    '''
    msg['Text'](msg['FileName'])
    return '@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName'])
    '''
    itchat.send('这是我的小号,暂无调戏功能,有事请加我大号:Honlann', msg['FromUserName'])

@itchat.msg_register(FRIENDS)
def add_friend(msg):
    itchat.add_friend(**msg['Text']) # 该操作会自动将新好友的消息录入,不需要重载通讯录
    itchat.send_msg(u'你好哇', msg['RecommendInfo']['UserName'])

@itchat.msg_register([TEXT, SHARING], isGroupChat=True)
def group_reply_text(msg):
    source = msg['FromUserName']
    username = msg['ActualNickName']
    userid = msg['ToUserName']

    if not source in groups:
        groups.append(source)

    if msg['Type'] == TEXT:
        content = msg['Content']
    elif msg['Type'] == SHARING:
        content = msg['Text']
    upload_message(username, userid, msg['Type'], content, msg['Url'])

    if msg['Type'] == TEXT:
        for item in groups:
            if not item == source:
                itchat.send('%s\n%s' % (username, msg['Content']), item)
    elif msg['Type'] == SHARING:
        for item in groups:
            if not item == source:
                itchat.send('%s\n%s\n%s' % (username, msg['Text'], msg['Url']), item)

@itchat.msg_register([PICTURE, ATTACHMENT, VIDEO], isGroupChat=True)
def group_reply_media(msg):
    source = msg['FromUserName']
    username = msg['ActualNickName']
    userid = msg['ToUserName']

    if not source in groups:
        groups.append(source)

    upload_message(username, userid, msg['Type'], '', '')

    msg['Text'](msg['FileName'])
    for item in groups:
        if not item == source:
            itchat.send('@%s@%s' % ({'Picture': 'img', 'Video': 'vid'}.get(msg['Type'], 'fil'), msg['FileName']), item)

itchat.auto_login(hotReload=True, enableCmdQR=2)
itchat.run()
文档更新时间: 2019-06-14 11:00   作者:月影鹏鹏