예제 #1
0
def handle_friend(msg):
    """ 处理好友信息 """
    try:
        conf = config.get('auto_reply_info')
        if not conf.get('is_auto_reply'):
            return
        # 获取发送者的用户id
        uuid = FILEHELPER if msg[
            'ToUserName'] == FILEHELPER else msg.fromUserName
        is_all = conf.get('is_auto_reply_all')
        auto_uuids = conf.get(
            'auto_reply_black_uuids') if is_all else conf.get(
                'auto_reply_white_uuids')
        # 开启回复所有人,当用户是黑名单,不回复消息
        if is_all and uuid in auto_uuids:
            return

        # 关闭回复所有人,当用户不是白名单,不回复消息
        if not is_all and uuid not in auto_uuids:
            return

        receive_text = msg.text  # 好友发送来的消息内容
        # 好友叫啥,用于打印
        nick_name = FILEHELPER if uuid == FILEHELPER else msg.user.nickName
        print('\n{}发来信息:{}'.format(nick_name, receive_text))
        reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            time.sleep(random.randint(1, 2))  # 休眠一秒,保安全。想更快的,可以直接注释。
            reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text
            itchat.send(reply_text, toUserName=uuid)
            print('回复{}:{}'.format(nick_name, reply_text))
        else:
            print('自动回复失败\n')
    except Exception as exception:
        print(str(exception))
예제 #2
0
def text_reply(msg):
    """ 监听用户消息,用于自动回复 """
    try:
        # if not get_yaml().get('is_auto_relay'):
        #     return
        conf = config.get('auto_relay_info')
        if not conf.get('is_auto_relay'):
            return
        # print(json.dumps(msg, ensure_ascii=False))
        # 获取发送者的用户id
        uuid = FILEHELPER if msg[
            'ToUserName'] == FILEHELPER else msg.fromUserName
        # 如果用户id是自动回复列表的人员
        if uuid in conf.get('auto_reply_uuids'):
            receive_text = msg.text  # 好友发送来的消息内容
            # 好友叫啥,用于打印
            nickName = FILEHELPER if uuid == FILEHELPER else msg.user.nickName
            print('\n{}发来信息:{}'.format(nickName, receive_text))
            reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
            if reply_text:  # 如内容不为空,回复消息
                time.sleep(random.randint(1, 2))  # 休眠一秒,保安全。想更快的,可以直接注释。
                reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text
                itchat.send(reply_text, toUserName=uuid)
                print('回复{}:{}'.format(nickName, reply_text))
            else:
                print('自动回复失败\n')
    except Exception as e:
        print(str(e))
예제 #3
0
def text_reply(msg):
    """ 监听用户消息,用于自动回复 """
    try:
        if not get_yaml().get('is_auto_relay'):
            return
        # print(json.dumps(msg, ensure_ascii=False))
        # print(reply_userNames)
        # 获取发送者的用户id
        uuid = FILEHELPER if msg[
            'ToUserName'] == FILEHELPER else msg.fromUserName
        # 如果用户id是自动回复列表的人员
        if uuid in reply_userNames:
            receive_text = msg['Content']  # 好友发送来的消息内容
            # 好友叫啥
            nickName = FILEHELPER if uuid == FILEHELPER else msg.user.nickName
            print('\n{}发来信息:{}'.format(nickName, receive_text))
            reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
            time.sleep(random.randint(0, 2))  # 休眠一秒,保安全。想更快的,可以直接注释。
            if receive_text == "test":
                reply_text = "./one_today_image.jpg"  # 获取自动回复
                # reply_text = "D:\\sss.jpg"  # 获取自动回复
                itchat.send_image(reply_text, toUserName=uuid)
                print('回复图片{}:{}\n'.format(nickName, reply_text))
            elif reply_text:  # 如内容不为空,回复消息
                reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text
                itchat.send(reply_text, toUserName=uuid)

                print('回复{}:{}\n'.format(nickName, reply_text))
            else:
                print('自动回复失败\n'.format(receive_text))
    except Exception as e:
        print(str(e))
예제 #4
0
def text_reply(msg):
    """ 监听用户消息,用于自动回复 """
    try:
        if not get_yaml().get('is_auto_relay'):
            return
        # print(json.dumps(msg, ensure_ascii=False))
        # print(reply_userNames)
        # 获取发送者的用户id
        uuid = FILEHELPER if msg[
            'ToUserName'] == FILEHELPER else msg.fromUserName
        # 如果用户id是自动回复列表的人员
        if uuid in reply_userNames:
            receive_text = msg.text  # 好友发送来的消息内容
            # 好友叫啥
            nickName = FILEHELPER if uuid == FILEHELPER else msg.user.nickName
            print('\n{}发来信息:{}'.format(nickName, receive_text))
            reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
            time.sleep(random.randint(0, 2))  # 休眠一秒,保安全。想更快的,可以直接注释。
            if reply_text:  # 如内容不为空,回复消息
                #如果uuid不是FILEHELPER,回复不加前缀,否则,加前缀
                #reply_text = reply_text if not uuid == FILEHELPER else '小肥机器人替我回复:' + reply_text
                reply_text = '小肥机器人替我回复:' + reply_text
                itchat.send(reply_text, toUserName=uuid)
                print('回复{}:{}\n'.format(nickName, reply_text))
            else:
                print('自动回复失败\n'.format(receive_text))
    except Exception as e:
        print(str(e))
예제 #5
0
def auto_reply_text(nickName, receive_text, uuid):
    reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
    if reply_text:  # 如内容不为空,回复消息
        reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text
        itchat.send(reply_text, toUserName=uuid)
        # print('回复{}:{}\n'.format(nickName, reply_text))
    else:
        print('自动回复失败\n'.format(receive_text))
예제 #6
0
def handle_friend(msg):
    """ 处理好友信息 """
    try:

        # 自己通过手机微信发送给别人的消息(文件传输助手除外)不作处理。
        if msg['FromUserName'] == config.get(
                'wechat_uuid') and msg['ToUserName'] != FILEHELPER:
            return

        conf = config.get('auto_reply_info')
        if not conf.get('is_auto_reply'):
            return
        # 获取发送者的用户id
        uuid = FILEHELPER if msg['ToUserName'] == FILEHELPER else msg[
            'FromUserName']
        is_all = conf.get('is_auto_reply_all')
        auto_uuids = conf.get(
            'auto_reply_black_uuids') if is_all else conf.get(
                'auto_reply_white_uuids')
        # 开启回复所有人,当用户是黑名单,不回复消息
        if is_all and uuid in auto_uuids:
            return

        # 关闭回复所有人,当用户不是白名单,不回复消息
        if not is_all and uuid not in auto_uuids:
            return

        receive_text = msg.text  # 好友发送来的消息内容
        # 好友叫啥,用于打印
        nick_name = FILEHELPER if uuid == FILEHELPER else msg.user.nickName
        print('\n{}发来信息:{}'.format(nick_name, receive_text))
        reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            time.sleep(random.randint(1, 2))  # 休眠一秒,保安全。想更快的,可以直接注释。

            prefix = conf.get('auto_reply_prefix', '')  # 前缀
            if prefix:
                reply_text = '{}{}'.format(prefix, reply_text)

            suffix = conf.get('auto_reply_suffix', '')  # 后缀
            if suffix:
                reply_text = '{}{}'.format(reply_text, suffix)

            itchat.send(reply_text, toUserName=uuid)
            print('回复{}:{}'.format(nick_name, reply_text))
        else:
            print('自动回复失败\n')
    except Exception as exception:
        print(str(exception))
예제 #7
0
def handle_group_helper(msg):
    """
    处理群消息
    :param msg:
    :return:
    """

    conf = config.get('group_helper_conf')
    if not conf.get('is_open'):
        return
    text = msg['Text']

    # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息
    if conf.get('is_at') and not msg.isAt:
        return

    uuid = msg.fromUserName  # 群 uid
    ated_uuid = msg.actualUserName  # 艾特你的用户的uuid
    ated_name = msg.actualNickName  # 艾特你的人的群里的名称

    is_all = conf.get('is_all', False)
    user_uuids = conf.get('group_black_uuids') if is_all else conf.get(
        'group_white_uuids')

    # 开启回复所有群,而用户是黑名单,不处理消息
    if is_all and uuid in user_uuids:
        return

    # 未回复所有群,而用户不是白名单,不处理消息
    if not is_all and uuid not in user_uuids:
        return
    # 去掉 at 标记
    text = re.sub(at_compile, '', text)
    retext = handle_msg_helper(text, ated_uuid, ated_name)
    if retext:
        itchat.send(retext, toUserName=uuid)
        return

    # 其他结果都没有匹配到,走自动回复的路
    if conf.get('is_auto_reply'):
        reply_text = get_bot_info(text, ated_uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            reply_text = common_msg.format(ated_name=ated_name,
                                           text=reply_text)
            itchat.send(reply_text, uuid)
            print('回复{}:{}'.format(ated_name, reply_text))
        else:
            print('自动回复失败\n')
예제 #8
0
def group_text_reply(msg):
    """ 监听群消息,用于自动回复 """
    try:
        # 自动回复已关闭
        if not auto_reply:
            print('自动回复已关闭')
            return

        group_name = msg['User']['NickName']
        if group_name in reply_groupNames:
            # if not msg['IsAt']:
            #     return
            reply_text = get_bot_info(msg.Text, group_name)  # 获取自动回复
            if reply_text:  # 如内容不为空,回复消息
                itchat.send(reply_text, toUserName=msg['User']['UserName'])
                print('回复{}:{}\n'.format(group_name, reply_text))
            else:
                print('自动回复失败\n'.format(msg.Text))
        else:
            return

    except Exception as e:
        print(str(e))
예제 #9
0
def handle_group_helper(msg):
    """
    处理群消息
    :param msg:
    :return:
    """
    uuid = msg.fromUserName  # 群 uid
    ated_uuid = msg.actualUserName  # 艾特你的用户的uuid
    ated_name = msg.actualNickName  # 艾特你的人的群里的名称
    text = msg['Text']  # 发送到群里的消息。

    # 自己通过手机端微信发出的消息不作处理
    if ated_uuid == config.get('wechat_uuid'):
        return

    conf = config.get('group_helper_conf')
    if not conf.get('is_open'):
        return

    # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息
    if conf.get('is_at') and not msg.isAt:
        return

    is_all = conf.get('is_all', False)
    user_uuids = conf.get('group_black_uuids') if is_all else conf.get(
        'group_white_uuids')
    # 开启回复所有群,而用户是黑名单,不处理消息
    if is_all and uuid in user_uuids:
        return

    # 未回复所有群,而用户不是白名单,不处理消息
    if not is_all and uuid not in user_uuids:
        return
    # 去掉 at 标记
    text = re.sub(at_compile, '', text)

    # 如果是帮助设置
    helps = re.findall(help_complie, text, re.I)
    if helps:
        retext = help_group_content.format(ated_name=ated_name)
        itchat.send(retext, uuid)
        return

    # 是否是明天,用于日历,天气,星座查询
    is_tomorrow = re.findall(tomorrow_compile, text)
    if is_tomorrow:
        is_tomorrow = True
        htext = re.sub(tomorrow_compile, '', text)
    else:
        is_tomorrow = False
        htext = text

    htext = re.sub(punct_complie, '', htext)  # 去句末的标点

    # 已开启天气查询,并包括天气关键词
    if conf.get('is_weather'):
        if re.findall(weather_compile, htext, re.I):
            city = re.sub(weather_clean_compile, '', text,
                          flags=re.IGNORECASE).strip()

            if not city:  # 如果只是输入城市名
                # 从缓存数据库找最后一次查询的城市名
                city = find_user_city(ated_uuid)
            if not city:  # 缓存数据库没有保存,通过用户的资料查城市
                city = get_city_by_uuid(ated_uuid)
            if not city:
                retext = weather_null_msg.format(ated_name=ated_name)
                itchat.send(retext, uuid)
                return

            _date = datetime.now().strftime('%Y-%m-%d')
            weather_info = find_weather(_date, city)
            if weather_info:
                retext = common_msg.format(ated_name=ated_name,
                                           text=weather_info)
                itchat.send(retext, uuid)
                return

            weather_info = get_weather_info(city)
            if weather_info:
                # print(ated_name, city, retext)
                retext = common_msg.format(ated_name=ated_name,
                                           text=weather_info)
                itchat.send(retext, uuid)

                data = {
                    '_date': _date,
                    'city_name': city,
                    'weather_info': weather_info,
                    'userid': ated_uuid,
                    'last_time': datetime.now()
                }
                udpate_weather(data)
                # userid,city_name,last_time,group_name udpate_weather_city
                data2 = {
                    'userid': ated_uuid,
                    'city_name': city,
                    'last_time': datetime.now()
                }
                udpate_user_city(data2)
                return
            else:
                retext = weather_error_msg.format(ated_name=ated_name,
                                                  city=city)
                itchat.send(retext, uuid)
                return
            return

    # 已开启日历,并包含日历
    if conf.get('is_calendar'):
        if re.findall(calendar_complie, htext, flags=re.IGNORECASE):

            calendar_text = re.sub(calendar_complie, '', htext).strip()
            if calendar_text:  # 日历后面填上日期了
                dates = re.findall(calendar_date_compile, calendar_text)
                if not dates:
                    retext = calendar_error_msg.format(ated_name=ated_name)
                    itchat.send(retext, uuid)
                    return

                _date = '{}-{:0>2}-{:0>2}'.format(*dates[0])  # 用于保存数据库
                rt_date = '{}{:0>2}{:0>2}'.format(*dates[0])  # 用于查询日历
            else:  # 日历 后面没有日期,则默认使用今日。
                _date = datetime.now().strftime('%Y-%m-%d')
                rt_date = datetime.now().strftime('%Y%m%d')

            # 从数据库缓存中记取内容
            cale_info = find_perpetual_calendar(_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                return

            # 取网络数据
            cale_info = get_rtcalendar(rt_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                update_perpetual_calendar(_date, cale_info)  # 保存数据到数据库
                return
            else:  # 查询无结果
                retext = calendar_no_result_msg.format(ated_name=ated_name,
                                                       _date=_date)
                itchat.send(retext, uuid)
            return

    # 垃圾分类查询
    if conf.get('is_rubbish'):
        if re.findall(rubbish_complie, htext, re.I):
            key = re.sub(rubbish_complie, '', htext,
                         flags=re.IGNORECASE).strip()
            if not key:
                retext = rubbish_null_msg.format(ated_name=ated_name)
                itchat.send(retext, uuid)
                return

            _type = find_rubbish(key)
            if _type:
                retext = rubbish_normal_msg.format(ated_name=ated_name,
                                                   name=key,
                                                   _type=_type)
                itchat.send(retext, uuid)
                return
            _type, return_list, other = get_atoolbox_rubbish(key)
            if _type:
                retext = rubbish_normal_msg.format(ated_name=ated_name,
                                                   name=key,
                                                   _type=_type)
                itchat.send_msg(retext, uuid)
            elif other:
                retext = rubbish_other_msg.format(ated_name=ated_name,
                                                  name=key,
                                                  other=other)
                itchat.send_msg(retext, uuid)
            else:
                retext = rubbish_nothing_msg.format(ated_name=ated_name,
                                                    name=key)
                itchat.send_msg(retext, uuid)
            if return_list:
                update_rubbish(return_list)  # 保存数据库
            return

    if conf.get('is_moviebox'):
        if re.findall(moviebox_complie, htext, re.I):
            moviebox_text = re.sub(moviebox_complie, '', htext).strip()
            if moviebox_text:  # 日历后面填上日期了
                dates = re.findall(calendar_date_compile, moviebox_text)
                if not dates:
                    retext = calendar_error_msg.format(ated_name=ated_name)
                    itchat.send(retext, uuid)
                    return
                _date = '{}{:0>2}{:0>2}'.format(*dates[0])
            else:  # 日历 后面没有日期,则默认使用今日。
                _date = datetime.now().strftime('%Y%m%d')
            # 从数据库缓存中记取内容
            mb_info = find_movie_box(_date)
            if mb_info:
                retext = common_msg.format(ated_name=ated_name, text=mb_info)
                itchat.send(retext, uuid)
                return

            is_expired = False
            cur_date = datetime.now().date()
            query_date = datetime.strptime(_date, '%Y%m%d').date()

            if query_date < cur_date:
                is_expired = True

            # 取网络数据
            mb_info = get_maoyan_movie_box(_date, is_expired)
            if mb_info:
                retext = common_msg.format(ated_name=ated_name, text=mb_info)
                itchat.send(retext, uuid)
                update_movie_box(_date, mb_info, is_expired)  # 保存数据到数据库
                return
            else:  # 查询无结果
                retext = moiebox_no_result_msg.format(ated_name=ated_name,
                                                      _date=_date)
                itchat.send(retext, uuid)
            return

    # 处理订单号
    if conf.get('is_express'):
        express_list = re.findall(express_complie, htext, re.I)
        if express_list:
            express_code = express_list[0]
            db_data = find_express(express_code, uuid)
            shipper_code, shipper_name = '', ''
            if db_data:
                if not db_data['is_forced_update']:
                    info = db_data['info']
                    retext = common_msg.format(ated_name=ated_name, text=info)
                    itchat.send(retext, uuid)
                    return
                shipper_code = db_data['shipper_code']
                shipper_name = db_data['shipper_name']

            data = get_express_info(express_code,
                                    shipper_name=shipper_name,
                                    shipper_code=shipper_code)
            if data:
                info = data['info']
                retext = common_msg.format(ated_name=ated_name, text=info)
                itchat.send(retext, uuid)
                update_express(data, uuid)
                return
            else:
                print('未查询到此订单号的快递物流轨迹。')
                return

    # 其他结果都没有匹配到,走自动回复的路
    if conf.get('is_auto_reply'):
        reply_text = get_bot_info(text, ated_uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            reply_text = common_msg.format(ated_name=ated_name,
                                           text=reply_text)
            itchat.send(reply_text, uuid)
            print('回复{}:{}'.format(ated_name, reply_text))
        else:
            print('自动回复失败\n')
예제 #10
0
def handle_group_helper(msg):
    """
    处理群消息
    :param msg:
    :return:
    """
    conf = config.get('group_helper_conf')
    if not conf.get('is_open'):
        return
    text = msg['Text']

    # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息
    if conf.get('is_at') and not msg.isAt:
        return

    uuid = msg.fromUserName  # 群 uid
    ated_uuid = msg.actualUserName  # 艾特你的用户的uuid
    ated_name = msg.actualNickName  # 艾特你的人的群里的名称

    is_all = conf.get('is_all', False)
    user_uuids = conf.get('group_black_uuids') if is_all else conf.get(
        'group_white_uuids')
    # 开启回复所有群,而用户是黑名单,不处理消息
    if is_all and uuid in user_uuids:
        return

    # 未回复所有群,而用户不是白名单,不处理消息
    if not is_all and uuid not in user_uuids:
        return
    # 去掉 at 标记
    text = at_compile.sub('', text)

    # 如果是帮助
    helps = re.findall(help_complie, text, re.I)
    if helps:
        itchat.send(help_group_content, uuid)
        return

    # 是否是明天,用于日历,天气,星座查询
    is_tomorrow = re.findall(tomorrow_compile, text)
    if is_tomorrow:
        is_tomorrow = True
        htext = re.sub(tomorrow_compile, '', text)
    else:
        is_tomorrow = False
        htext = text

    htext = re.sub(punct_complie, '', htext)  # 去句末的标点

    # 已开启天气查询,并包括天气关键词
    if conf.get('is_weather'):
        citys = re.findall(weather_compile, htext)
        if citys:
            for x in citys[0]:
                city = x
                if city:
                    break
            else:
                city = find_user_city(ated_uuid)
                if not city:
                    city = get_city_by_uuid(ated_uuid)
            if city:
                _date = datetime.now().strftime('%Y-%m-%d')
                weather_info = find_weather(_date, city)
                if weather_info:
                    retext = common_msg.format(ated_name=ated_name,
                                               text=weather_info)
                    itchat.send(retext, uuid)
                    return

                weather_info = get_weather_info(city)
                if weather_info:
                    # print(ated_name, city, retext)
                    retext = common_msg.format(ated_name=ated_name,
                                               text=weather_info)
                    itchat.send(retext, uuid)

                    data = {
                        '_date': _date,
                        'city_name': city,
                        'weather_info': weather_info,
                        'userid': ated_uuid,
                        'last_time': datetime.now()
                    }
                    udpate_weather(data)
                    # userid,city_name,last_time,group_name udpate_weather_city
                    data2 = {
                        'userid': ated_uuid,
                        'city_name': city,
                        'last_time': datetime.now()
                    }
                    udpate_user_city(data2)
                    return
                else:
                    weather_error_msg = '@{ated_name}\u2005\n未找到『{city}』城市的相关信息'
                    retext = weather_error_msg.format(ated_name=ated_name,
                                                      city=city)
                    itchat.send(retext, uuid)
                    return
            return

    # 已开启日历,并包含日历
    if conf.get('is_calendar'):
        if re.findall(calendar_complie, htext):
            _date = datetime.now().strftime('%Y-%m-%d')
            cale_info = find_perpetual_calendar(_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                return

            rt_date = datetime.now().strftime('%Y%m%d')
            cale_info = get_rtcalendar(rt_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                update_perpetual_calendar(_date, cale_info)
                return
            return

    # 其他结果都没有匹配到,走自动回复的路
    if conf.get('is_auto_reply'):
        reply_text = get_bot_info(text, ated_uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            reply_text = common_msg.format(ated_name=ated_name,
                                           text=reply_text)
            itchat.send(reply_text, uuid)
            print('回复{}:{}'.format(ated_name, reply_text))
        else:
            print('自动回复失败\n')
예제 #11
0
def handle_friend(msg):
    """ 处理好友信息 """
    try:
        conf = config.get('auto_reply_info')
        if not conf.get('is_auto_reply'):
            return
        # 获取发送者的用户id
        uuid = FILEHELPER if msg[
            'ToUserName'] == FILEHELPER else msg.fromUserName
        is_all = conf.get('is_auto_reply_all')
        auto_uuids = conf.get(
            'auto_reply_black_uuids') if is_all else conf.get(
                'auto_reply_white_uuids')
        # 开启回复所有人,当用户是黑名单,不回复消息
        if is_all and uuid in auto_uuids:
            return

        # 关闭回复所有人,当用户不是白名单,不回复消息
        if not is_all and uuid not in auto_uuids:
            return

        receive_text = msg.text  # 好友发送来的消息内容
        # 好友叫啥,用于打印
        nick_name = FILEHELPER if uuid == FILEHELPER else msg.user.nickName
        print('\n{}发来信息:{}'.format(nick_name, receive_text))

        uuid = msg.fromUserName  # 群 uid
        ated_uuid = uuid  # 艾特你的用户的uuid
        ated_name = nick_name  # 艾特你的人的群里的名称

        # 如果是帮助
        helps = re.findall(help_complie, receive_text, re.I)
        if helps:
            retext = help_group_content
            itchat.send(retext, uuid)
            return

        # 百度文库解析
        if "wenku.baidu" in receive_text:
            retext = parserJS(receive_text)  # 解析文库文档
            retext = '臣妾做不到'
            itchat.send(retext, toUserName=uuid)
            return
        # 已开启天气查询,并包括天气关键词

        if re.findall(weather_compile, receive_text, re.I):
            city = re.sub(weather_clean_compile,
                          '',
                          receive_text,
                          flags=re.IGNORECASE).strip()
            print('--------------------------{}--------------------------'.
                  format(city))
            _date = datetime.now().strftime('%Y-%m-%d')
            weather_info = find_weather(_date, city)
            if weather_info:
                retext = common_msg.format(ated_name=ated_name,
                                           text=weather_info)
                itchat.send(retext, uuid)
                return

            weather_info = get_weather_info(city)
            if weather_info:
                # print(ated_name, city, retext)
                retext = common_msg.format(ated_name=ated_name,
                                           text=weather_info)
                itchat.send(retext, uuid)

                data = {
                    '_date': _date,
                    'city_name': city,
                    'weather_info': weather_info,
                    'userid': ated_uuid,
                    'last_time': datetime.now()
                }
                udpate_weather(data)
                # userid,city_name,last_time,group_name udpate_weather_city
                data2 = {
                    'userid': ated_uuid,
                    'city_name': city,
                    'last_time': datetime.now()
                }
                udpate_user_city(data2)
                return
            else:
                retext = weather_error_msg.format(ated_name=ated_name,
                                                  city=city)
                itchat.send(retext, uuid)
                return
            return

        reply_text = get_bot_info(receive_text, uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            time.sleep(random.randint(1, 2))  # 休眠一秒,保安全。想更快的,可以直接注释。
            reply_text = reply_text if not uuid == FILEHELPER else '机器人回复:' + reply_text
            itchat.send(reply_text, toUserName=uuid)
            print('回复{}:{}'.format(nick_name, reply_text))
        else:
            print('自动回复失败\n')
    except Exception as exception:
        print(str(exception))
예제 #12
0
def handle_group_helper(msg):
    """
    处理群消息
    :param msg:
    :return:
    """

    conf = config.get('group_helper_conf')
    if not conf.get('is_open'):
        return
    text = msg['Text']

    # 如果开启了 『艾特才回复』,而群用户又没有艾特你。不处理消息
    if conf.get('is_at') and not msg.isAt:
        return

    uuid = msg.fromUserName  # 群 uid
    ated_uuid = msg.actualUserName  # 艾特你的用户的uuid
    ated_name = msg.actualNickName  # 艾特你的人的群里的名称

    is_all = conf.get('is_all', False)
    user_uuids = conf.get('group_black_uuids') if is_all else conf.get('group_white_uuids')
    # 开启回复所有群,而用户是黑名单,不处理消息
    if is_all and uuid in user_uuids:
        return

    # 未回复所有群,而用户不是白名单,不处理消息
    if not is_all and uuid not in user_uuids:
        return
    # 去掉 at 标记
    text = re.sub(at_compile, '', text)

    # 如果是帮助
    helps = re.findall(help_complie, text, re.I)
    if helps:
        retext = help_group_content.format(ated_name=ated_name)
        itchat.send(retext, uuid)
        return

    # 是否是明天,用于日历,天气,星座查询
    is_tomorrow = re.findall(tomorrow_compile, text)
    if is_tomorrow:
        is_tomorrow = True
        htext = re.sub(tomorrow_compile, '', text)
    else:
        is_tomorrow = False
        htext = text

    htext = re.sub(punct_complie, '', htext)  # 去句末的标点

    # 已开启天气查询,并包括天气关键词
    if conf.get('is_weather'):
        if re.findall(weather_compile, htext, re.I):
            city = re.sub(weather_clean_compile, '', text)

            if not city:  # 如果只是输入城市名
                # 从缓存数据库找最后一次查询的城市名
                city = find_user_city(ated_uuid)
            if not city:  # 缓存数据库没有保存,通过用户的资料查城市
                city = get_city_by_uuid(ated_uuid)
            if not city:
                retext = '请输入城市名'
                itchat.send(retext, uuid)
                return

            _date = datetime.now().strftime('%Y-%m-%d')
            weather_info = find_weather(_date, city)
            if weather_info:
                retext = common_msg.format(ated_name=ated_name, text=weather_info)
                itchat.send(retext, uuid)
                return

            weather_info = get_weather_info(city)
            if weather_info:
                # print(ated_name, city, retext)
                retext = common_msg.format(ated_name=ated_name, text=weather_info)
                itchat.send(retext, uuid)

                data = {
                    '_date': _date,
                    'city_name': city,
                    'weather_info': weather_info,
                    'userid': ated_uuid,
                    'last_time': datetime.now()
                }
                udpate_weather(data)
                # userid,city_name,last_time,group_name udpate_weather_city
                data2 = {
                    'userid': ated_uuid,
                    'city_name': city,
                    'last_time': datetime.now()
                }
                udpate_user_city(data2)
                return
            else:
                retext = weather_error_msg.format(ated_name=ated_name, city=city)
                itchat.send(retext, uuid)
                return
            return

    # 已开启日历,并包含日历
    if conf.get('is_calendar'):
        if re.findall(calendar_complie, htext, re.I):

            calendar_text = re.sub(calendar_complie, '', htext)
            if calendar_text:  # 日历后面填上日期了
                dates = re.findall(calendar_date_compile, calendar_text)
                if not dates:
                    retext = '请输入正确的日期'
                    itchat.send(retext, uuid)
                    return

                _date = '{}-{:0>2}-{:0>2}'.format(*dates[0])  # 用于保存数据库
                rt_date = '{}{:0>2}{:0>2}'.format(*dates[0])  # 用于查询日历
            else:  # 日历 后面没有日期,则默认使用今日。
                _date = datetime.now().strftime('%Y-%m-%d')
                rt_date = datetime.now().strftime('%Y%m%d')

            # 从 数据库缓存中记取内容
            cale_info = find_perpetual_calendar(_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                return

            # 取网络数据
            cale_info = get_rtcalendar(rt_date)
            if cale_info:
                retext = common_msg.format(ated_name=ated_name, text=cale_info)
                itchat.send(retext, uuid)
                update_perpetual_calendar(_date, cale_info)  # 保存数据到数据库
                return
            return

    if conf.get('is_rubbish'):
        if re.findall(rubbish_complie, htext, re.I):
            key = re.sub(rubbish_complie, '', htext).strip()
            if not key:
                retext = '请填写垃圾名称'
                itchat.send(retext, uuid)
                return

            _type = find_rubbish(key)
            if _type:
                retext = rubbish_normal.format(ated_name=ated_name, name=key, _type=_type)
                itchat.send(retext, uuid)
                return
            _type, return_list, other = get_atoolbox_rubbish(key)
            if _type:
                retext = rubbish_normal.format(ated_name=ated_name, name=key, _type=_type)
                itchat.send_msg(retext, uuid)
            elif other:
                retext = rubbish_other.format(ated_name=ated_name, name=key, other=other)
                itchat.send_msg(retext, uuid)
            else:
                retext = rubbish_nothing.format(ated_name=ated_name, name=key)
                itchat.send_msg(retext, uuid)
            if return_list:
                update_rubbish(return_list)
            return

    # 其他结果都没有匹配到,走自动回复的路
    if conf.get('is_auto_reply'):
        reply_text = get_bot_info(text, ated_uuid)  # 获取自动回复
        if reply_text:  # 如内容不为空,回复消息
            reply_text = common_msg.format(ated_name=ated_name, text=reply_text)
            itchat.send(reply_text, uuid)
            print('回复{}:{}'.format(ated_name, reply_text))
        else:
            print('自动回复失败\n')