Search code examples
amazon-web-servicesaws-sdkamazon-lexaws-amplifyamplifyjs

AWS Amplify MissingRequiredParameter userId error


I'm following the guide for starting with Interactions. When I call the send method on Interactions, I get the following error:

(node:27796) UnhandledPromiseRejectionWarning: MissingRequiredParameter: Missing required key 'userId' in params

It looks like Interactions is expecting a userId param, which in @aws-amplify/interactions/lib/Providers/AWSLexProvider.js, is supposed to be pulled from credentials.identityId. However, when I log credentials, it is type SharedIniFileCredentials, which does not have an identityId property according to the documentation.

From reading the docs, identityId would have to be a Cognito user. AWSLexProvider.js makes no attempt at calling CognitoIdentityCredentials to get Cognito credentials.

Hence, I am not sure where identityId is supposed to come from.

My code is the example from the Amplify website:

import Amplify, { Interactions } from 'aws-amplify';
import aws_exports from './aws-exports';

Amplify.configure(aws_exports);

async function test() {
    let userInput = "I want to reserve a hotel for tonight";

    // Provide a bot name and user input
    const response = await Interactions.send("BookTrip", userInput);

    // Log chatbot response
    console.log (response['message']);
}

test();

So what am I missing here?


Solution

  • I had the same issue when adding a Bot which i creatred manually w/o using the full amplify setup, but just using the amplify React Frontend SDK to use the Chatbot-Component. Turned out that I used the wrong identityPoolIdfor Cognito Auth. When using the right one, which can be found as described here where to find identity pool id in the cognito federeated identities section, the error disappeared and the bots started working. Additionally I assured that the custom_auth_role assigned to that identity pool has addtionally the following properties under actions:

                "Action": [
                 ...
                "lex:PostContent",
                "lex:PostText"
            ],
    

    This can be assgined in the IAM -> Roles Section for that role. Not sure if that is absolutely required though.

    So finally this is what it looks like:

        //...all other React imports, etc
        import { ChatBot, withAuthenticator } from "aws-amplify-react";
        import Amplify, { Auth } from "aws-amplify";
    
        Amplify.configure({
             Auth: {
              identityPoolId: "eu-west-1:XX-XX-XX-XXX-XXX", //<-here the right Id needs to be set
              region: "eu-west-1",
              userPoolId: "eu-west-1_XXXXXX",
              userPoolWebClientId: "XXXXXX"
             },
             Interactions: {
              bots: {
               botNameAsInAwsConsole: {
                name: "someName",
                alias: "$LATEST",
                region: "eu-west-1"
               }
             }
            }
        });
    
        function App() {
         return (
          <ChatBot
            title="Demo Bot"
            theme={myTheme}
            botName="botNameAsInAwsConsole"
            welcomeMessage="Welcome, how can I help you today?"
            onComplete={handleComplete}
            clearOnComplete={true}
            conversationModeOn={false}
            voiceEnabled={false}
          />
      );
    }
    
    export default withAuthenticator(App, true);