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

问题描述 投票:1回答:2

我正在研究玩具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对象允许我在整个会话中保留信息。在Alexa Developer控制台中,我在'session'下的JSON中看到了这些数据:'attributes' - 我也看到'session':'user':'userId'

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

python alexa-skills-kit
2个回答
2
投票

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

user_id = handler_input.request_envelope.session.user.user_id

要么

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

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


0
投票

因此,当您将其转换为'dict'时,可以提取handler_input中的数据

step1 = handler_input.__dict__
step2 = step1['request_envelope'].__dict__
step3 = step2['session'].__dict__
step4 = step3['user'].__dict__
userid = step4['user_id']

它可以用更少的步骤来完成,但这就是让它适合我的原因。

© www.soinside.com 2019 - 2024. All rights reserved.