Fetch the slot value used for a previous intent(method) and use it in current method?

674 views Asked by At

I am working on building an alexa skill where the requirement is to fetch the value for previous slot..

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}

Final method should fetch values from previous slots(subject and date) and create an appointment. So far i'm able to fetch slot value for current intent...but i would like to fetch the slot value for previous intent and use it in the current method.

*Example

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;
}

Is this possible?If so how can i do it?

Thanks and Regards

3

There are 3 answers

0
Giridhari On BEST ANSWER

I used sessions to achieve my query like this....

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

I believe you should be able to use session.setAttribute() and session.getAttribute() as mentioned here - https://github.com/amzn/alexa-skills-kit-java/blob/master/src/com/amazon/speech/speechlet/Session.java

1
Tiago Carneiro On

like Amit said, you should be able to use session.setAttribute, however, if for some reason you want to keep something that might not be accepted by the session.setAttribute you can always have a field variable that is always available, until the session closes of course, once the session closes it's all gone without a database.

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;
    }

All I did there was initialize the string variable output in a higher scope(outside the method) which makes it available for any other methods.