You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Alexa技能启动请求中触发意图收集用户IP地址

解决方案

你可以实现这个需求,但不能直接从启动请求里“触发”意图,而是通过维持会话并引导用户交互,让Alexa自然进入收集IP的意图流程。具体做法如下:

关键修改点

  • 必须将ShouldEndSession设为false,保持会话处于打开状态,这样用户才能继续回复信息。
  • 在未找到用户ID的分支中,输出引导用户提供IP的语音提示,同时通过会话属性标记当前需要收集IP,方便后续意图处理时识别场景。
  • 确保你的IP收集意图(比如GetIPAddressIntent)已经在技能交互模型中配置了足够的样本表述(例如“我的IP是192.168.1.100”“IP地址是10.0.0.5”)。

修改后的启动请求处理代码

private SkillResponse LaunchRequestHandler(SkillRequest input, ILambdaContext context)
{
    // 初始化响应
    var skillResponse = new SkillResponse
    {
        Version = "1.0",
        Response = new ResponseBody(),
        SessionAttributes = new Dictionary<string, object>() // 初始化会话属性容器
    };

    // 输出语音对象
    SsmlOutputSpeech ssmlResponse = new SsmlOutputSpeech();

    try
    {
        var strUserId = input.Session.User.UserId;

        // 构造DynamoDB查询请求
        var request = new GetItemRequest
        {
            TableName = tableName,
            Key = new Dictionary<string, AttributeValue>() { { "strUserId", new AttributeValue { S = strUserId } } },
        };
        var response = client.GetItemAsync(request);
        var result = response.Result;

        if (result.Item.Count() < 1)
        {
            // 标记当前会话需要收集IP地址
            skillResponse.SessionAttributes.Add("NeedIP", true);
            // 引导用户提供IP
            ssmlResponse.Ssml = "<speak>看起来是你第一次使用,请告诉我你的IP地址。</speak>";
            // 保持会话打开,等待用户回复
            skillResponse.Response.ShouldEndSession = false;
        }
        else
        {
            ssmlResponse.Ssml = "<speak>Hi there. I'm not Cortana.</speak>";
            // 给出命令引导提示(此处可补充具体引导内容)
            skillResponse.Response.ShouldEndSession = true;
        }
    }
    catch (AmazonDynamoDBException e) 
    { 
        ssmlResponse.Ssml = "<speak>" + e.InnerException.Message + "</speak>";
        skillResponse.Response.ShouldEndSession = true;
    }
    catch (AmazonServiceException e) 
    { 
        ssmlResponse.Ssml = "<speak>" + e.Message + "</speak>";
        skillResponse.Response.ShouldEndSession = true;
    }
    catch (Exception e) 
    { 
        ssmlResponse.Ssml = "<speak>" + e.Message + "</speak>";
        skillResponse.Response.ShouldEndSession = true;
    }

    skillResponse.Response.OutputSpeech = ssmlResponse;
    return skillResponse;
}

IP收集意图的处理补充

在你的GetIPAddressIntent处理函数中,可读取会话属性的标记,确认是首次启动的收集场景,完成IP存储后再关闭会话:

private SkillResponse GetIPAddressIntentHandler(SkillRequest input, ILambdaContext context)
{
    var skillResponse = new SkillResponse
    {
        Version = "1.0",
        Response = new ResponseBody()
    };
    SsmlOutputSpeech ssmlResponse = new SsmlOutputSpeech();

    try
    {
        // 从意图槽位获取用户提供的IP地址(假设槽位名为ipAddress)
        var ipAddress = input.Request.Intent.Slots["ipAddress"].Value;
        var userId = input.Session.User.UserId;

        // 将用户ID和IP地址存入DynamoDB
        var putRequest = new PutItemRequest
        {
            TableName = tableName,
            Item = new Dictionary<string, AttributeValue>
            {
                { "strUserId", new AttributeValue { S = userId } },
                { "ipAddress", new AttributeValue { S = ipAddress } }
            }
        };
        client.PutItemAsync(putRequest).Wait();

        ssmlResponse.Ssml = "<speak>已成功保存你的IP地址,现在可以开始使用功能了。</speak>";
        skillResponse.Response.ShouldEndSession = true;
    }
    catch (Exception e)
    {
        ssmlResponse.Ssml = "<speak>保存IP地址时出错了,请重试。</speak>";
        skillResponse.Response.ShouldEndSession = false;
    }

    skillResponse.Response.OutputSpeech = ssmlResponse;
    return skillResponse;
}

内容的提问来源于stack exchange,提问作者luigivampa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 06:20:41