i. Is there a better way to handle this requirement?
From what I remember, postText
is simply the way to manually connect and send user inputs to your Lex bot, instead of going through Facebook Messenger for example. So postText
doesn't handle any recognition of slots, intents or anything like that. That's all done by Lex and then handled with Lambda.
So I would suggest using sessionAttributes
for this to remember both the last_question
and last_response
through multiple turns. Then use postText
again to simply send the thumbs up or down rating.
Lambda will then recognize the rating input as its own slot, and retrieve the last_question
and last_response
from sessionAttributes
and then perform the saving and tracking the rating system however you'd like.
Example:
User: "...(Question A)..."
postText
passes question A to Lex
- Lex receives question A, interprets intent and slots and passes to Lambda
- Lambda receives input, intent, and slot data
- Lambda saves question A in
sessionAttributes
as last_question
- Lambda determines Answer A is the proper response
- Lambda saves Answer A in
sessionAttributes
as last_answer
and passes to Lex
- Lex receives Answer A from Lambda and responds to user with Answer A
Bot: "...(Answer A).... Please rate this answer."
- Lex response delivered through output channel.
User: "Thumbs up."
postText
passes Thumbs up to Lex. -> Lex interprets. -> Lambda receives.
- Lambda saves user input as
user_rating
- Lambda finds
last_question
and last_answer
in sessionAttributes
- Lambda saves
last_question
, last_answer
, and user_rating
in a database
- Lambda erases
last_question
and last_answer
from sessionAttributes
to start again.
- Lambda responds -> Lex responds -> Output channel displays response.
Bot: "Thank you, please ask another question."
ii. How do custom slots work in single request with multiple information, what am I supposed to send to lex?
It sounds like the confusion you are having is that you have to prepare formatted data before sending to Lex inside of postText
when it's much simpler than that. Just pass the user input and let Lex sort it out based on how you've set up intents and slots. Here's how it should go for a multiple-slot single-input.
Lex receives the user input as a string and attempts to parse the string for intents and slots, looking for the best matches. You simply prepare the Lex Console with intents and slots and custom slots and slot values, to improve its matching accuracy.
So let's say you know a user might give a single input with multiple information that you would like to catch as separate slots, for example:
"What is the time in New York?"
You probably want to catch the type of question so you have a slot to determine if the user is asking for "time" or "weather" or "population". So those would be made as one slot question_type
, with those options as slot values.
Then you'd also want to catch where the question is about so you have another slot to determine if the user asked about "New York" or "Paris" or "Bangkok". So those would be made as one slot question_location
, with those options as slot values.
After you prepare those slots and slot values in the Lex Console, you'd also want to prepare Lex for this type of input by narrowing down where in the sentence Lex is likely to find which slots. These would be the utterances for this intent, putting the slot names inside of curly braces "{ }":
what is the {question_type} in {question_location}
Then imagine more ways and in different orders the user might ask those questions:
what {question_type} is it in {question_location}
in {question_location} what is the {question_type}
Lex will parse the input and fill the slots with whatever it detected in that part of the input. Then in Lambda you match the slots with what you would expect and determine the best response.