Skip to main content
 首页 » 编程设计

Python之如何使用 ask_sdk_core.handler_input 在 Alexa Skill 中获取 UserID

2024年09月03日30txw1958

我正在研究一种玩具 Alexa 技能,我正在按照猜数字游戏 (code here) 中的示例进行操作。在他们的例子中

from ask_sdk_core.handler_input import HandlerInput 
 
@sb.request_handler(can_handle_func=is_request_type("LaunchRequest")) 
def launch_request_handler(handler_input): 
    """Handler for Skill Launch. 
 
    Get the persistence attributes, to figure out the game state. 
    """ 
    # type: (HandlerInput) -> Response 
    attr = handler_input.attributes_manager.persistent_attributes 

这个 attr 对象允许我在整个 session 中保留信息。在 Alexa Developer 控制台中,我在“session”下的 JSON 中看到此数据:“attributes” - 我还看到“session”:“user”:“userId”

如何在此函数中使用 handler_input 访问 userId 数据?

请您参考如下方法:

你实际上可以从两个地方得到它:

user_id = handler_input.request_envelope.session.user.user_id 

或

user_id = handler_input.request_envelope.context.system.user.user_id 

没有必要将对象转换为字典