Exemplo n.º 1
0
def attachment_files(msg):
    fileDir = './files/' + msg['FileName']
    # 不是自己发送的消息,收到的信息
    if msg['FromUserName'] != wxConfigSingleton.WXConfigSingleton(
    ).my_user_name:
        # 保存文件
        if not os.path.exists(fileDir):
            wxTools.save_file_to_files(msg)
        # 开启自动回复
        if wxConfigSingleton.WXConfigSingleton().auto_reply == True:
            time.sleep(random.randint(1, 2))
            wxTools.sendFileMessage(type=msg['Type'],
                                    file_path=fileDir,
                                    to_user_name=msg['FromUserName'])
Exemplo n.º 2
0
def initConfig():
    # 文件存储临时目录
    wxTools.removeTempConfig()
    wxTools.initTempConfig()
    # 获取我的id
    wxConfigSingleton.WXConfigSingleton(
    ).my_user_name = wxUserInfo.getUserInfoOfMe().user_name
Exemplo n.º 3
0
def text_msg(msg):
    if msg['Content'] == u'加群':
        itchat.send_msg(str(
            itchat.search_friends(userName=msg['FromUserName'])['NickName']),
                        toUserName='******')
        itchat.send_msg('您的加群信息已收到\n稍后(我也不知道多久)将会拉您进群', msg['FromUserName'])
        itchat.send_image('timg.gif', msg['FromUserName'])  # 发送一个骚气表情包
    elif msg['Content'] == u'公众号':
        itchat.send_msg(
            '#!/usr/bin/python\n#coding:utf-8\n    def ISA():\n        print("慎言善思,学以致用")\nif __name__ == "__main__":\n    ISA()',
            msg['FromUserName'])
        itchat.send_msg('欢迎关注我们的公众号\n', msg['FromUserName'])
        itchat.send_image('QR_ISA.jpg', msg['FromUserName'])  # 发送公众号的二维码

    # 不是自己发送的消息,收到的信息
    if msg['FromUserName'] != wxConfigSingleton.WXConfigSingleton(
    ).my_user_name:
        # 更新聊天数据
        wxTools.updateMessage(msg_dict, msg)
        # 判断为群聊,为接收到的消息
        if '@@' in msg['FromUserName']:
            # 群中有人找小艾同学
            if wxTools.isCallMe(msg):
                # 机器人回复
                call_me_in_group(msg)
        else:  # 判断为个人消息, 为接收到的消息
            call_me_in_frind(msg)
    else:
        if msg['ToUserName'] == 'filehelper':
            send_filehelper(msg)
Exemplo n.º 4
0
def call_me_in_frind(msg):
    try:
        if '台湾' in msg['Text']:
            return '台湾是中国不可分割的一部分,支持祖国收复台湾,建立台湾省'
        # 开启自动回复
        if wxConfigSingleton.WXConfigSingleton().auto_reply == True:
            if wxConfigSingleton.WXConfigSingleton().robot_type == 'tl':
                # 图灵机器人
                auto_replayOFTL(msg)
            elif wxConfigSingleton.WXConfigSingleton().robot_type == 'xb':
                global face_bug
                # 从小冰获取回复消息
                face_bug = msg['FromUserName']
                auto_replayOFXB(msg)
    except Exception as error:
        print(error)
        return
Exemplo n.º 5
0
def video_msg(msg):
    # 不是自己发送的消息,收到的信息
    if msg['FromUserName'] != wxConfigSingleton.WXConfigSingleton(
    ).my_user_name:
        # 保存文件
        videoDir = './videos/' + msg['FileName']
        if not os.path.exists(videoDir):
            wxTools.save_video_to_videos(msg)
        # 更新聊天数据
        wxTools.updateMessage(msg_dict, msg)
        if wxConfigSingleton.WXConfigSingleton().auto_reply == True:
            try:
                time.sleep(random.randint(1, 2))
                wxTools.sendFileMessage(type=msg['Type'],
                                        file_path=videoDir,
                                        to_user_name=msg['FromUserName'])
            except Exception as error:
                print(error)
                return
Exemplo n.º 6
0
def image_msg(msg):
    # 不是自己发送的消息,收到的信息
    if msg['FromUserName'] != wxConfigSingleton.WXConfigSingleton(
    ).my_user_name:
        path = './images/'  # 图片路径
        imageDir = path + msg['FileName']
        # 保存文件
        if not os.path.exists(imageDir):
            wxTools.save_image_to_images(msg)
        # 更新聊天数据
        wxTools.updateMessage(msg_dict, msg)
        # 开启自动回复
        if wxConfigSingleton.WXConfigSingleton().auto_reply == True:
            try:
                # 以下方法会下载文件
                dirs = os.listdir(path)
                wxTools.sendFileMessage(type=msg['Type'],
                                        file_path=path + random.choice(dirs),
                                        to_user_name=msg['FromUserName'])
            except Exception as error:
                print(error)
                return
Exemplo n.º 7
0
def send_filehelper(msg):
    # 给文件助手发消息
    cmd_dic = {
        '/开启自动回复': '自动回复已开启',
        '/关闭自动回复': '自动回复已关闭',
        '/使用小冰': '小冰自动回复已开启',
        '/关闭小冰': '小冰自动回复已关闭',
        '/使用图灵': '图灵自动回复已开启',
        '/关闭图灵': '图灵自动回复已关闭',
        '/开启轮询': '轮询已开启',
        '/关闭轮询': '轮询已关闭',
        '/获取好友头像': '好友头像获取成功',
        '/获取好友头像拼图': '好友头像拼图获取成功',
        '/帮助':
        '/帮助  显示帮助信息\n\n/配置  下载服务端配置文件\n\n /开启回复  开启自动回复\n\n /关闭回复  关闭自动回复',
    }
    if msg['Text'] == '/开启自动回复':
        wxConfigSingleton.WXConfigSingleton().auto_reply = True
        # 默认使用图灵
        wxConfigSingleton.WXConfigSingleton().robot_type = 'tl'
    elif msg['Text'] == '/关闭自动回复':
        wxConfigSingleton.WXConfigSingleton().auto_reply = False
        wxConfigSingleton.WXConfigSingleton().robot_type = ''
    elif msg['Text'] == '/使用小冰':
        wxConfigSingleton.WXConfigSingleton().auto_reply = True
        wxConfigSingleton.WXConfigSingleton().robot_type = 'xb'
    elif msg['Text'] == '/关闭小冰':
        wxConfigSingleton.WXConfigSingleton().auto_reply = False
        wxConfigSingleton.WXConfigSingleton().robot_type = ''
    elif msg['Text'] == '/使用图灵':
        wxConfigSingleton.WXConfigSingleton().auto_reply = True
        wxConfigSingleton.WXConfigSingleton().robot_type = 'tl'
    elif msg['Text'] == '/关闭图灵':
        wxConfigSingleton.WXConfigSingleton().auto_reply = False
        wxConfigSingleton.WXConfigSingleton().robot_type = ''
    elif msg['Text'] == '/开启轮询':
        itchat.send(cmd_dic[msg['Text']], toUserName='******')
        # 循环
        sched.add_job(test, 'interval', seconds=5)
        # 指定日期执行
        #sched.add_job(tick, 'date', run_date='2016-02-14 15:23:05')
        '''
            job = scheduler.add_job(myfunc, 'interval', minutes=2)
            job.remove()
            # 如果有多个任务序列的话可以给每个任务设置ID号,可以根据ID号选择清除对象,且remove放到start前才有效
            sched.add_job(myfunc, 'interval', minutes=2, id='my_job_id')
            sched.remove_job('my_job_id')
            apsched.job.Job.pause()
            apsched.schedulers.base.BaseScheduler.pause_job()
        '''
        '''
            #表示2017年3月22日17时19分07秒执行该程序
            sched.add_job(my_job, 'cron', year=2017,month = 03,day = 22,hour = 17,minute = 19,second = 07)
 
            #表示任务在6,7,8,11,12月份的第三个星期五的00:00,01:00,02:00,03:00 执行该程序
            sched.add_job(my_job, 'cron', month='6-8,11-12', day='3rd fri', hour='0-3')
 
            #表示从星期一到星期五5:30(AM)直到2014-05-30 00:00:00
            sched.add_job(my_job(), 'cron', day_of_week='mon-fri', hour=5, minute=30,end_date='2014-05-30')
 
            #表示每5秒执行该程序一次,相当于interval 间隔调度中seconds = 5
            sched.add_job(my_job, 'cron',second = '*/5')
        '''

        try:
            sched.start()
        except:
            sched.shutdown()
    elif msg['Text'] == '/关闭轮询':
        sched.shutdown()
    # 好友头像
    elif msg['Text'] == '/获取好友头像':
        we_friends = itchat.get_friends(update=True)[:]
        num = 0
        for friend in we_friends:
            img = itchat.get_head_img(userName=friend["UserName"])
            file_image = open("./headerIcons/" + str(num) + ".jpg", 'wb')
            file_image.write(img)
            file_image.close()
            num += 1
    elif msg['Text'] == '/获取好友头像拼图':
        save_image = './files/' + "好友头像拼接图.jpg"
        if not os.path.exists(save_image):
            import math
            from PIL import Image
            header_icons = './headerIcons/'
            ls = os.listdir(header_icons)
            each_size = int(math.sqrt(float(640 * 640) /
                                      len(ls)))  # 算出每张图片的大小多少合适
            lines = int(640 / each_size)
            # PNG是四通道, JPG是三通道
            image = Image.new('RGB', (640, 640))  # 创建640*640px的大图
            x, y = 0, 0
            for i in range(0, len(ls)):
                try:
                    img = Image.open(header_icons + str(i) + ".jpg")
                except IOError as e:
                    print("Error: ", e)
                else:
                    img = img.resize((each_size, each_size), Image.ANTIALIAS)
                    image.paste(img, (x * each_size, y * each_size))  # 粘贴位置
                    x += 1
                    if x == lines:  # 换行
                        x = 0
                        y += 1
            image.save(save_image)
    itchat.send(cmd_dic[msg['Text']], toUserName='******')