Interact with bots
AWS Lex V2
Send messages to bot
You can send a text message to chatbot backend with send()
command. The method returns a promise that includes the chatbot response.
import { Interactions } from '@aws-amplify/interactions';
let userInput = "I want to reserve a hotel for tonight";
(async () => { // Provide a bot name and user input const response = await Interactions.send({ botName: "TheBotName", message: userInput });
// Log chatbot response console.log(response.message);})()
Display end of chat message
You can use onComplete()
method to register a function to catch errors or chatbot confirmations when the session successfully ends.
import { Interactions } from '@aws-amplify/interactions';
Interactions.onComplete({ botName: "TheBotName", callback: (error?: Error, completion?: {[key: string]: any}) => { if (error) { alert('bot conversation failed'); } else if (completion) { console.debug('done: ' + JSON.stringify(completion, null, 2)); alert('Trip booked. Thank you! What would you like to do next?'); } }});
AWS Lex V1
The Lex V1 module is located in a different path, and we strongly advise you to migrate to the Lex V2 bot (migration guide).
Send messages to bot
The exposed APIs have same signatures. You can send a text message to chatbot backend with send()
command. The method returns a promise that includes the chatbot response.
import { Interactions as InteractionsLexV1 } from '@aws-amplify/interactions/lex-v1';
let userInput = "I want to reserve a hotel for tonight";
(async () => { // Provide a bot name and user input const response = await InteractionsLexV1.send({ botName: "TheBotName", message: userInput });
// Log chatbot response console.log(response.message);})()
Display end of chat message
You can use onComplete()
method to register a function to catch errors or chatbot confirmations when the session successfully ends.
import { Interactions as InteractionsLexV1 } from '@aws-amplify/interactions/lex-v1';
InteractionsLexV1.onComplete({ botName: "TheBotName", callback: (error?: Error, completion?: {[key: string]: any}) => { if (error) { alert('bot conversation failed'); } else if (completion) { console.debug('done: ' + JSON.stringify(completion, null, 2)); alert('Trip booked. Thank you! What would you like to do next?'); } }});