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.
1import { Interactions } from '@aws-amplify/interactions';2
3let userInput = "I want to reserve a hotel for tonight";4
5(async () => {6 // Provide a bot name and user input7 const response = await Interactions.send({8 botName: "TheBotName",9 message: userInput10 });11
12 // Log chatbot response13 console.log(response.message);14})()
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.
1import { Interactions } from '@aws-amplify/interactions';2
3Interactions.onComplete({4 botName: "TheBotName",5 callback: (error?: Error, completion?: {[key: string]: any}) => {6 if (error) {7 alert('bot conversation failed');8 } else if (completion) {9 console.debug('done: ' + JSON.stringify(completion, null, 2));10 alert('Trip booked. Thank you! What would you like to do next?');11 }12 }13});
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.
1import { Interactions as InteractionsLexV1 } from '@aws-amplify/interactions/lex-v1';2
3let userInput = "I want to reserve a hotel for tonight";4
5(async () => {6 // Provide a bot name and user input7 const response = await InteractionsLexV1.send({8 botName: "TheBotName",9 message: userInput10 });11
12 // Log chatbot response13 console.log(response.message);14})()
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.
1import { Interactions as InteractionsLexV1 } from '@aws-amplify/interactions/lex-v1';2
3InteractionsLexV1.onComplete({4 botName: "TheBotName",5 callback: (error?: Error, completion?: {[key: string]: any}) => {6 if (error) {7 alert('bot conversation failed');8 } else if (completion) {9 console.debug('done: ' + JSON.stringify(completion, null, 2));10 alert('Trip booked. Thank you! What would you like to do next?');11 }12 }13});