return new Promise((resolve, reject) => {
x = context.sendActivity({
text: 'hi',
attachments: [CardFactory.adaptiveCard(menuJson)]
})
I am trying to send an adaptive card, which contains a Input.text field in it...Now my question is how to get the input data from the user in my program using a context object ?
i.e How to Handle adaptive cards in bot framework v4 using node js ?
Adaptive Cards send their Submit results a little different than regular user text. When a user types in the chat and sends a normal message, it ends up in
context.activity.text
. When a user fills out an input on an Adaptive Card, it ends up incontext.activity.value
, which is an object where the key names are theid
in yourmenuJson
and the values are the field values in the adaptive card.For example, the json:
.. creates a card that looks like:
If a user enters "Testing Testing 123" in the text box and hits Submit,
context.activity
will look something like:The user submission can be seen in
context.activity.value.userText
.Note that adaptive card submissions are sent as a postBack, which means that the submission data doesn't appear in the chat window as part of the conversation--it stays on the Adaptive Card.
Using Adaptive Cards with Waterfall Dialogs
Your question doesn't quite relate to this, but since you may end up attempting this, I thought it might be important to include in my answer.
Natively, Adaptive Cards don't work like prompts. With a prompt, the prompt will display and wait for user input before continuing. But with Adaptive Cards (even if it contains an input box and a submit button), there is no code in an Adaptive Card that will cause a Waterfall Dialog to wait for user input before continuing the dialog.
So, if you're using an Adaptive Card that takes user input, you generally want to handle whatever the user submits outside of the context of a Waterfall Dialog.
That being said, if you want to use an Adaptive Card as part of a Waterfall Dialog, there is a workaround. Basically, you:
In your Waterfall Dialog file (steps 1 and 2):
In your
bot.ts
file (step 3):I'm using Adaptive Card in a ComponentDialog with WaterfallDialog, I want to handle Input.submit action.
My problem is: How to handle the response, get the input value, and go to next dialog step correctly?
I try 2 ways to resolve my problem.
My adaptive card's json like:
1. Using prompt and prompt validate
This way uses prompt validate function to handle Input.submit postback action.
Because postback action doesn't send a text message (not show in channel), that makes TextPrompt's default validate can't pass (send retryPrompt), so I write a prompt validate function and validate response is postback action.
2. Using Dialog.EndOfTurn
This way uses Dialog.EndOfTurn to end turn. If user sends any response, the bot will go to next dialog step.
Please remember to check if the response is adaptive card submit action (postback), if not, do something to reject it or retry.
In the end, I would choose the second way (Dialog.EndOfTurn) to solve the problem, because I think it is easier to control the dialog step and handle user interrupt, for example, when user wants to cancel this action and return to main dialog.