【问题标题】:Alexa Skill python not working properly with NoIntentAlexa Skill python 无法与 NoIntent 一起正常工作
【发布时间】:2022-11-18 14:40:45
【问题描述】:

我是 Alexa Skill 和 Python 的基础开发人员,但无法理解问题出在哪里。 此技能询问淋浴的开始和结束时间,以计算费用和能源消耗。

当用户完成淋浴时,我问“Alexa 洗澡”而不是 Alexa 问:

  • 你洗完澡了吗? (“是的”)
  • 你用过电吹风吗?

当用户对最后一个问题回答“否”时,Alexa 不理解。 相反,当答复是“是”时,一切都很好。

代码有什么问题? 谢谢

class ValidateShowerIntentHandler(AbstractRequestHandler):
    """Handler for Hello World Intent."""
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("ValidateShowerIntent")(handler_input)
def handle(self, handler_input):
    # type: (HandlerInput) -> Response
    persistent_attributes = handler_input.attributes_manager.persistent_attributes
    if 'startedShower' not in persistent_attributes:
        speak_output = "Ciao, ora calcoliamo il costo della tua doccia. Stai iniziando la doccia?"
    else:
        speak_output = "Hai finito la doccia?"

    return (
        handler_input.response_builder
            .speak(speak_output)
            .ask(speak_output)
            .response
    )
    

class YesIntentHandler(AbstractRequestHandler): """Handler for Help Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("AMAZON.YesIntent")(handler_input)

def handle(self, handler_input):
    logger.info("inside YesIntentHandler()")
    # type: (HandlerInput) -> Response
    persistent_attributes = handler_input.attributes_manager.persistent_attributes
    if 'total_minutes' in persistent_attributes:
        # dryer is used
        minutes = persistent_attributes['total_minutes']
        # calculation
        total_cost = (int(minutes) + 5 ) * 0.07
        userId = handler_input.request_envelope.context.system.user.user_id
        print(userId)
        item = {
            "guid" : str(uuid.uuid4()),
            "created_on" : datetime.utcnow().strftime(DATETIME_FORMAT),
            "user_id": userId,
            "total_minutes": minutes,
            "is_dryer_used": True,
            "total_cost": round(decimal.Decimal(total_cost),3)
        }
        print(item)
        tms_skill_table.put_item(Item = item)
        total_cost_round_off = round(total_cost,3)
        speak_output = f"Il totale per la tua doccia \u00E8 di {total_cost_round_off} EUR"
        # Delete all attributes from the DB
        handler_input.attributes_manager.delete_persistent_attributes()
    elif 'startedShower' not in persistent_attributes:
        persistent_attributes['startedShower'] = "yes"
        start_time = datetime.utcnow().strftime(DATETIME_FORMAT)
        print(start_time)
        persistent_attributes['startTime'] = start_time
        # Write user's name to the DB.
        handler_input.attributes_manager.save_persistent_attributes()
        speak_output = "Okay"
    else:
        start_time = datetime.strptime(persistent_attributes['startTime'], DATETIME_FORMAT)
        current_time = datetime.utcnow()
        time_taken = current_time - start_time
        minutes = time_taken.total_seconds() / 60
        print(minutes)
        persistent_attributes['total_minutes'] = str(round(minutes))
        speak_output = f"Hai usato un asciugacapelli?"
        
        handler_input.attributes_manager.save_persistent_attributes()
        logger.info(f"Alexa message - {speak_output}")
        logger.info(f"session_attributes - {persistent_attributes}")
        return (
            handler_input.response_builder
                .speak(speak_output)
                .ask(speak_output)
                .response
        )
    
    logger.info(f"Alexa message - {speak_output}")
    logger.info(f"session_attributes - {persistent_attributes}")
    return (
        handler_input.response_builder
            .speak(speak_output)
            .set_should_end_session(True)
            .response
    )
    

class NoIntentHandler(AbstractRequestHandler): """Handler for Help Intent.""" def can_handle(self, handler_input): # type: (HandlerInput) -> bool return ask_utils.is_intent_name("AMAZON.NoIntent")(handler_input)

def handle(self, handler_input):
    logger.info("inside NoIntentHandler()")
    persistent_attributes = handler_input.attributes_manager.persistent_attributes
    if 'total_minutes' in persistent_attributes:
        # dryer is used
        minutes = persistent_attributes['total_minutes']
        # calculation
        total_cost = int(minutes) * 0.07
        userId = handler_input.request_envelope.context.system.user.user_id
        print(userId)
        item = {
            "guid" : str(uuid.uuid4()),
            "created_on" : datetime.utcnow().strftime(DATETIME_FORMAT),
            "user_id": userId,
            "total_minutes": minutes,
            "is_dryer_used": False,
            "total_cost": decimal.Decimal(total_cost)
        }
        tms_skill_table.put_item(Item = item)
        speak_output = f"Il totale per la tua doccia \u00E8 di {total_cost} EUR"
        logger.info(f"Alexa message - {speak_output}")
        logger.info(f"session_attributes - {persistent_attributes}")
        # Delete all attributes from the DB
        handler_input.attributes_manager.delete_persistent_attributes()
        persistent_attributes = {}
        return (
            handler_input.response_builder
                .speak(speak_output)
                .set_should_end_session(True)
                .response
        )
    speak_output = "Okay. Secondo te come posso aiutarti??"
    reprompt = f"Forse non hai capito: devi dire: inizio la doccia. "
    logger.info(f"Alexa message - {speak_output}")
    logger.info(f"session_attributes - {persistent_attributes}")
    persistent_attributes = {}
    return (
        handler_input.response_builder
            .speak(speak_output)
            .ask(reprompt)
            .response
    )
    

【问题讨论】:

    标签: python alexa alexa-skill


    【解决方案1】:
    记录器信息 ("inside YesIntentHandler()") # 类型:(HandlerInput) -> 响应分钟) + 5 ) * 0.07 userId = handler_input.request_envelope.context.system.user.user_id print(userId) item = {

    【讨论】:

    • 看不懂,好像是一样的代码
    • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
    猜你喜欢
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2020-08-19
    • 2013-02-06
    • 2019-02-02
    • 2021-10-15
    • 2016-07-05
    相关资源
    最近更新 更多