获取用于先前intent(方法)的槽值并在当前方法中使用它?

时间:2017-09-11 08:11:30

标签: amazon-web-services alexa alexa-skills-kit alexa-skill alexa-slot

我正在努力建立一个alexa技能,其中要求是获取前一个插槽的值..

User:  Alexa,create an appointment
Alexa: please state the subject for appointment
User : subject is {subject}
Alexa: on which day?
User : {Day}
Alexa : Appointment has been created with {subject} on {day}

最终方法应从先前的广告位(主题和日期)中获取值并创建约会。 到目前为止,我能够获取当前意图的插槽值...但我想获取先前意图的插槽值并在当前方法中使用它。

*实施例

protected SpeechletResponse method1(Intent intent,Session session)throws 
Exception{
    SpeechletResponse response=null;

Map<String, Slot> slots =  intent.getSlots();
Slot example = slots.get(SampleSlot);
String value=example.getValue().toString();
String output="something";

response = SpeechletResponseHelper.getSpeechletResponse(output, reprompt, 
true);
    return response;
}

protected SpeechletResponse method2(Intent intent,Session session)throws 
Exception{
SpeechletResponse response=null;

****How to fetch the slot value used for method1 and use it in method2? ****

response = SpeechletResponseHelper.getSpeechletResponse(xxxx, reprompt, 
true);
return response;
}

这可能吗?如果可以的话我该怎么做?

谢谢和问候

3 个答案:

答案 0 :(得分:0)

我相信您应该可以使用此处提到的session.setAttribute()session.getAttribute() - https://github.com/amzn/alexa-skills-kit-java/blob/master/src/com/amazon/speech/speechlet/Session.java

答案 1 :(得分:0)

像Amit说的那样,你应该可以使用session.setAttribute,但是,如果由于某种原因你想要保留session.setAttribute可能无法接受的内容,你总是可以拥有字段变量始终可用,直到会话结束为止,一旦会话关闭,它就没有数据库了。

String output;

    protected SpeechletResponse method1(Intent intent,Session session)throws 
    Exception{
        SpeechletResponse response=null;

    Map<String, Slot> slots =  intent.getSlots();
    Slot example = slots.get(SampleSlot);
    String value=example.getValue().toString();
    output="something";

    response = SpeechletResponseHelper.getSpeechletResponse(output, reprompt, 
    true);
        return response;
    }

    protected SpeechletResponse method2(Intent intent,Session session)throws 
    Exception{
    SpeechletResponse response=null;

    ****How to fetch the slot value used for method1 and use it in method2? ****

    response = SpeechletResponseHelper.getSpeechletResponse(output, reprompt, 
    true);
    return response;
    }

我所做的就是在更高的范围内(在方法之外)初始化字符串变量output,这使得它可用于任何其他方法。

答案 2 :(得分:0)

我使用会话来实现我的查询......

Map<String, Slot> slots =  intent.getSlots();
Slot example = slots.get(SampleSlot);
session.setAttributes(Sample_String);
session.getAttributes(SampleSessionString);