Page updated Jan 16, 2024

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); })()
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 input
7 const response = await Interactions.send({
8 botName: "TheBotName",
9 message: userInput
10 });
11
12 // Log chatbot response
13 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.

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?'); } } });
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.

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); })()
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 input
7 const response = await InteractionsLexV1.send({
8 botName: "TheBotName",
9 message: userInput
10 });
11
12 // Log chatbot response
13 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.

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?'); } } });
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});