예제 #1
0
def send_alarm_msg():
    print('\n获取消息...')
    gf = config.get('alarm_info').get('girlfriend_infos')[0]
    is_tomorrow = gf.get('is_tomorrow', False)
    # calendar_info = get_calendar_info(gf.get('calendar'), gf.get('app_token'), is_tomorrow)
    weather = get_weather_info(gf.get('city_name'), gf.get('app_token'), is_tomorrow)
    horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow)
    dictum = get_dictum_info(gf.get('dictum_channel'))
    diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg'))
    air_quality = get_air_quality(gf.get('air_quality_city'))
    sweet_words = gf.get('sweet_words')

    list_data = []
    for x in [weather, air_quality, horoscope, dictum, diff_time, sweet_words]:
        if x:
            list_data.append(x)
            
    send_msg = '  \n'.join(list_data) # 必须添加两个空格加换行
    print('\n' + send_msg + '\n')
    # pyperclip.copy(send_msg)
    with open('./result.txt', 'w', encoding='utf-8') as f:
        f.write(send_msg)
    form = {
        'title': '每日一句',
        'desp': send_msg
    }
    send_key = os.environ.get('SEND_KEY')
    # print(send_key)
    resp = requests.post('https://sctapi.ftqq.com/{}.send'.format(send_key), form)
    print(resp)
    if resp.status_code == 200:
        print('发送成功!')
예제 #2
0
def send_alarm_msg(key):
    """ 发送定时提醒 """
    print('\n启动定时自动提醒...')
    conf = config.get('alarm_info').get('alarm_dict')

    gf = conf.get(key)
    # print(gf)
    is_tomorrow = gf.get('is_tomorrow', False)
    calendar_info = get_calendar_info(gf.get('calendar'), is_tomorrow)
    weather = get_weather_info(gf.get('city_name'), is_tomorrow)
    horoscope = get_constellation_info(gf.get("horescope"), is_tomorrow)
    dictum = get_dictum_info(gf.get('dictum_channel'))
    diff_time = get_diff_time(gf.get('start_date'), gf.get('start_date_msg'))
    sweet_words = gf.get('sweet_words')
    send_msg = '\n'.join(
        x for x in
        [calendar_info, weather, horoscope, dictum, diff_time, sweet_words]
        if x)
    # print('\n' + send_msg + '\n')
    if not send_msg or not is_online(): return
    uuid_list = gf.get('uuid_list')
    for uuid in uuid_list:
        time.sleep(1)
        itchat.send(send_msg, toUserName=uuid)
    print('\n定时内容:\n{}\n发送成功...\n\n'.format(send_msg))
    print('自动提醒消息发送完成...\n')
예제 #3
0
def send_alarm_msg():
    """ 发送定时提醒 """
    print('\n启动定时自动提醒...')
    conf = get_yaml()
    for gf in conf.get('girlfriend_infos'):
        dictum = get_dictum_info(gf.get('dictum_channel'))
        weather = get_weather_info(gf.get('city_name'))
        diff_time = get_diff_time(gf.get('start_date'))
        sweet_words = gf.get('sweet_words')
        horoscope = get_xzw_info(gf.get("birthday"))
        joke = get_joke_info(gf.get('is_joke', False))
        # 如果渠道是一个·ONE 就发图片
        # send_image_path = get_one_image(gf.get('dictum_channel'))
        send_image_path = get_one_image(1)  #强制发one图片

        send_msg = '\n'.join(x for x in [
            weather, "\r", dictum, "\r", diff_time, sweet_words, "\r",
            horoscope
        ] if x)
        print(send_msg)

        if not send_msg or not is_online(): continue
        # 给微信好友发信息
        wechat_name = gf.get('wechat_name')
        if wechat_name:
            if wechat_name.lower() in FILEHELPER_MARK:
                if send_image_path:
                    itchat.send_image(send_image_path, toUserName=FILEHELPER)
                itchat.send(send_msg, toUserName=FILEHELPER)
                if joke:
                    itchat.send(joke, toUserName=FILEHELPER)
                print('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(
                    wechat_name, send_msg))
            else:
                wechat_users = itchat.search_friends(name=wechat_name)
                if not wechat_users: continue
                if send_image_path:
                    wechat_users[0].send_image(send_image_path)
                wechat_users[0].send(send_msg)
                if joke:
                    wechat_users[0].send(joke)
                print('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(
                    wechat_name, send_msg))

        # 给群聊里发信息
        group_name = gf.get('group_name')
        if group_name:
            group = get_group(group_name)
            if group:
                if send_image_path:
                    group.send_image(send_image_path)
                group.send(send_msg)
                if joke:
                    group.send(joke)
                print('定时给群聊『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(
                    group_name, send_msg))

    print('自动提醒消息发送完成...\n')
예제 #4
0
def send_alarm_msg():
    """ 发送定时提醒 """
    print('\n启动定时自动提醒...')
    conf = get_yaml()
    for gf in conf.get('girlfriend_infos'):
        dictum = get_dictum_info(gf.get('dictum_channel'))
        weather = get_weather_info(gf.get('city_name'))
        horoscope = get_xzw_info(gf.get("birthday"))

        send_msg = '\n'.join(x for x in [weather, dictum, horoscope] if x)

        if not send_msg or not is_online(): continue
        # 给微信好友发信息
        wechat_name = gf.get('wechat_name')
        if wechat_name:
            if wechat_name.lower() in FILEHELPER_MARK:
                itchat.send(send_msg, toUserName=FILEHELPER)
                print('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(
                    wechat_name, send_msg))
            else:
                wechat_users = itchat.search_friends(name=wechat_name)
                if not wechat_users: continue
                wechat_users[0].send(send_msg)
                print('定时给『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(
                    wechat_name, send_msg))

        # 给群聊里发信息
        group_name = gf.get('group_name')
        if group_name:
            group = get_group(group_name)
            if group:
                group.send(send_msg)
                print('定时给群聊『{}』发送的内容是:\n{}\n发送成功...\n\n'.format(
                    group_name, send_msg))

    print('自动提醒消息发送完成...\n')
예제 #5
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')
예제 #6
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')
예제 #7
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))
예제 #8
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')
예제 #9
0
def handle_msg_helper(text, uuid, u_name):
    """
    处理文本消息
    :param msg:
    :return text:
    """
    conf = config.get('group_helper_conf')
    if not conf.get('is_open'):
        return None
    # 去掉 at 标记
    text = re.sub(at_compile, '', text)
    if u_name is None:
        ated_name = ''
    else:
        ated_name = '@' + u_name + '\u2005\n'
    # 如果是帮助
    helps = re.findall(help_complie, text, re.I)
    if helps:
        retext = help_group_content.format(ated_name=ated_name)
        return retext

    # 是否是明天,用于日历,天气,星座查询
    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(uuid)
            if not city:  # 缓存数据库没有保存,通过用户的资料查城市
                city = get_city_by_uuid(uuid)
            if not city:
                retext = weather_null_msg.format(ated_name=ated_name)
                return retext

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

            weather_info = get_weather_info(city)
            if weather_info:
                # print(ated_name, city, retext)

                data = {
                    '_date': _date,
                    'city_name': city,
                    'weather_info': weather_info,
                    'userid': uuid,
                    'last_time': datetime.now()
                }
                udpate_weather(data)
                # userid,city_name,last_time,group_name udpate_weather_city
                data2 = {
                    'userid': uuid,
                    'city_name': city,
                    'last_time': datetime.now()
                }
                udpate_user_city(data2)
                return common_msg.format(ated_name=ated_name,
                                         text=weather_info)

    # 已开启日历,并包含日历
    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:
                    return calendar_error_msg.format(ated_name=ated_name)

                _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:
                return common_msg.format(ated_name=ated_name, text=cale_info)

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

    if conf.get('is_rubbish'):
        if re.findall(rubbish_complie, htext, re.I):
            key = re.sub(rubbish_clear_compile, '', htext,
                         flags=re.IGNORECASE).strip()
            if not key:
                return rubbish_null_msg.format(ated_name=ated_name)

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

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