Example #1
0
def handle(text, mic, profile, iot_client=None, chatbot=None):
    mic.say('正在查询天气...')

    fc_client = FcClient.get_instance()

    tree = ET.parse(APP_PATH +
                    '/src/plugins/resources/weather-moji-citys.xml')  # 载入数据
    xml_root = tree.getroot()  #获取根节点
    elements = xml_root.findall('./city[@name="' + city + '"]')
    try:
        city_id = elements[0].get('id')
    except:
        mic.say('没有找到你设定的城市,请修改profile配置文件')
    finally:
        if city_id is None:
            mic.say('没有找到你设定的城市,请修改profile配置文件')

    data = {
        'host': 'http://freecityid.market.alicloudapi.com',
        'path': '/whapi/json/alicityweather/briefforecast3days',
        'method': 'POST',
        'appcode': ali_appcode,
        'payload': {
            'cityId': city_id
        },
        'bodys': {},
        'querys': ''
    }
    return_text = ''
    result_raw = json.loads(
        fc_client.call_function('aliyun_apimarket',
                                payload=data).data.decode('utf8'))
    if result_raw['msg'] == 'success':
        return_text += myname + '为您播报,' + result_raw['data']['city'][
            'name'] + '天气预报,'
        if is_all_word_segment_in_text(['明天', '明日'], text):
            forecast = result_raw['data']['forecast'][1]
            day = '明天'
        elif is_all_word_segment_in_text(['后天', '明日'], text):
            forecast = result_raw['data']['forecast'][2]
            day = '后天'
        else:
            forecast = result_raw['data']['forecast'][0]
            day = '今天'
        forecast_output = return_text + day + forecast['conditionDay']+',白天气温,'+forecast['tempDay'].replace('-', '零下')+\
                       '摄氏度,夜间气温,'+forecast['tempNight'].replace('-', '零下')+\
                       '摄氏度,'+forecast['windDirNight']+forecast['windLevelDay'].replace('-', '到')+'级'
        plugin_output(text, mic, forecast_output)
    else:
        mic.say('天气获取失败')
Example #2
0
def is_valid(text):
    """
        Returns True if input is related to the time.

        Arguments:
        text -- user-input, typically transcribed speech
    """
    return is_all_word_segment_in_text(WORDS, text)
Example #3
0
def handle(text, mic, profile, iot_client=None,chatbot=None):
    """
        Reports the current time based on the user's timezone.

        Arguments:
        text -- user-input, typically transcribed speech
        mic -- used to interact with the user (for both input and output)
        profile -- contains information related to the user (e.g., phone
                   number)
        wxBot -- wechat robot
    """
    tz = pytz.timezone(profile.timezone)
    now = dt.datetime.now(tz=tz)
    if is_all_word_segment_in_text(['时间', '几点'], text):
        plugin_output(text, mic, "现在时间,%s " % now.strftime("%p%I时%M分").replace('AM', '上午').replace('PM', '下午'))
    else:
        plugin_output(text, mic, "今天是," + now.strftime("%Y年%m月%d日") + ',星期'+week_map[int(now.strftime('%w'))])
Example #4
0
def is_valid(text):
    return is_all_word_segment_in_text(WORDS, text)