Page updated Jan 16, 2024

Set up Amplify Interactions

AWS Amplify Interactions category enables AI-powered chatbots in your web or mobile apps. You can use Interactions to configure your backend chatbot provider and to integrate a chatbot UI into your app with just a single line of code.

Interactions with AWS

AWS Amplify implements Amazon Lex as the default chatbots service. Amazon Lex supports creating conversational bots with the same deep learning technologies that power Amazon Alexa.

The recommended approach is to begin your new App integration with AWS LexV2, as the default module exports are associated with AWS LexV2 APIs. Instructions for AWS LexV1 setup can be found in the following section.

Setup AWS LexV2 bot

You can create Amazon Lex V2 chatbot in Amazon Lex console. To create your bot, follow the steps shown in Amazon Lex V2 Developer Guide.

Interactions

With manual setup, you also need to add AWS Lex V2 API permissions to IAM roles and bot details to configure your app.

For adding IAM permissions, find you IAM role and attach the policy below (remember to replace the template with real value):

1{
2 "Version": "2012-10-17",
3 "Statement": [
4 {
5 "Effect": "Allow",
6 "Action": ["lex:RecognizeText", "lex:RecognizeUtterance"],
7 "Resource": "arn:aws:lex:<Region>:<Account>:bot-alias/<BotId>/<BotAliasId>"
8 }
9 ]
10}
1import { Amplify } from 'aws-amplify';
2import amplifyconfig from './amplifyconfiguration.json';
3
4Amplify.configure(amplifyconfig);
5Amplify.configure({
6 ...Amplify.getConfig(),
7 Interactions: {
8 LexV2: {
9 '<V2BotName>': {
10 aliasId: '<V2BotAliasId>',
11 botId: '<V2BotBotId>',
12 localeId: '<V2BotLocaleId>',
13 region: '<V2BotRegion>'
14 }
15 }
16 }
17});

Make sure you call Amplify.configure as early as possible in your application’s life-cycle. A missing configuration or NoCredentials error is thrown if Amplify.configure has not been called before other Amplify JavaScript APIs. Review the Library Not Configured Troubleshooting guide for possible causes of this issue.

Setup AWS LexV1 bot

Create new LexV1 chatbot with Amplify CLI

Prerequisite: Install and configure the Amplify CLI

Run the following command in your project's root folder:

1amplify add interactions

Adding interactions from the CLI only allows you to create a Lex V1 bot. If you want to create a LexV2 bot, you can do so following the instructions here.

The CLI will lead you through the steps to specify the chatbot to be created.

You can choose to start from a sample chatbot or start from scratch. If you choose to start from scratch, the CLI will prompt you with a series of questions to set the intents and slots for the chatbot.

You are allowed to run the amplify add interactions command multiple times to add multiple chatbots into your project.

The Interactions category utilizes the Authentication category behind the scenes to authorize your app to send analytics events.

The add command automatically creates a backend configuration locally. To update your backend in the cloud, run:

1amplify push

Upon successful execution of the push command, a configuration file called amplifyconfiguration.json will be copied to your configured source directory, for example ./src.

If your Interactions resources were created with Amplify CLI version 1.6.4 and below, you will need to manually update your project to avoid Node.js runtime issues with AWS Lambda. Read more

Manual setup

You can create Amazon Lex V1 chatbot in Amazon Lex console. To create your bot, follow the steps shown in Amazon Lex V1 Developer Guide.

Interactions

With manual setup, you need to add AWS Lex API permissions to IAM roles and bot details to configure your app.

For adding AWS Lex API permissions, find you IAM role and attach the policy below (remember to replace the template with real value):

1{
2 "Version": "2012-10-17",
3 "Statement": [
4 {
5 "Effect": "Allow",
6 "Action": ["lex:PostText", "lex:PostContent"],
7 "Resource": "arn:aws:lex:<Region>:<Account>:bot:<BotName>:<BotAlias>"
8 }
9 ]
10}

Configuring bot details in your web app like this:

1import { Amplify } from 'aws-amplify';
2import amplifyconfig from './amplifyconfiguration.json';
3
4Amplify.configure(amplifyconfig);
5Amplify.configure({
6 ...Amplify.getConfig(),
7 Interactions: {
8 LexV1: {
9 '<V1BotName>': {
10 alias: '<V1BotAlias>',
11 region: '<V1BotRegion>'
12 }
13 }
14 }
15});

Make sure you call Amplify.configure as early as possible in your application’s life-cycle. A missing configuration or NoCredentials error is thrown if Amplify.configure has not been called before other Amplify JavaScript APIs. Review the Library Not Configured Troubleshooting guide for possible causes of this issue.

Configure frontend

Make sure that the @aws-amplify/interactions package has the same version number as the aws-amplify package in your package.json file.

Install Amplify and its dependencies

Instructions for React Native version 0.72 and below

@aws-amplify/react-native requires a minimum iOS deployment target of 13.0 if you are using react-native version less than or equal to 0.72. Open the Podfile located in the ios directory and update the target value:

1- platform :ios, min_ios_version_supported
2 + platform :ios, 13.0
1npm install aws-amplify @aws-amplify/react-native @aws-amplify/interactions @react-native-community/netinfo @react-native-async-storage/async-storage react-native-get-random-values

You need to add the crypto.getRandomValues polyfills to your application's entry point file (in most React Native apps this will be the top level index.js).

1// Example index.js
2import 'react-native-get-random-values';
3
4import { AppRegistry } from 'react-native';
5import App from './App';
6import { name as appName } from './app.json';
7
8AppRegistry.registerComponent(appName, () => App);

Configure Amplify

Import and load the configuration file in your app. It's recommended you add the Amplify configuration step to your app's root entry point. For example, App.js (Expo) or index.js (React Native CLI).

1import { Amplify } from 'aws-amplify';
2import amplifyconfig from './amplifyconfiguration.json';
3
4Amplify.configure(amplifyconfig);

Known Issues

You may encounter the following error when starting the bundler:

Error: Unable to resolve module stream from /path/to/node_modules/@aws-sdk/...

This is a known issue. Please follow the steps outlined in the linked issue to resolve the error.