AWS Amplify MissingRequiredParameter userId错误

时间:2019-01-11 14:31:23

标签: amazon-web-services aws-sdk amazon-lex aws-amplify amplifyjs

我正在遵循从Interactions开始的指南。当我在交互上调用send方法时,出现以下错误:

  

(节点:27796)UnhandledPromiseRejectionWarning:MissingRequiredParameter:参数中缺少必需的键'userId'

Interactions似乎期望使用 userId 参数,该参数应在@aws-amplify/interactions/lib/Providers/AWSLexProvider.js中从credentials.identityId中提取。但是,当我登录credentials时,它是类型SharedIniFileCredentials,它没有identityId属性according to the documentation

reading the docs中,identityId必须是Cognito用户。 AWSLexProvider.js不会尝试调用CognitoIdentityCredentials来获取Cognito凭据。

因此,我不确定identityId应该来自哪里

我的代码是Amplify网站上的示例:

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();

那我在这里想念什么?

3 个答案:

答案 0 :(得分:1)

添加Bot时,我遇到了相同的问题,该Bot是使用完整的放大设置手动创建的,但没有使用放大的React Frontend SDK来使用Chatbot组件。原来我为Cognito Auth使用了错误的identityPoolId。当使用正确的密码时,如此处where to find identity pool id所示,在“认知联合身份”部分中可以找到该错误,并且机器人开始工作。另外,我保证分配给该身份池的custom_auth_role在操作中还具有以下属性:

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

这可以在该角色的IAM->角色部分中提出。不确定是否绝对必要。

所以最终这是它的样子:

    //...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);

答案 1 :(得分:0)

我之前没有使用过AWS Amplify,因此可能不是这个答案,但是我之前已经使用过Amazon Lex多次。正在查找的UserId字段可能是Lex PostText / PostContent请求的UserId参数(请参见下面的代码)

PostText Documentation:

var params = {
  botAlias: 'STRING_VALUE', /* required */
  botName: 'STRING_VALUE', /* required */
  inputText: 'STRING_VALUE', /* required */
  userId: 'STRING_VALUE', /* required - THIS IS MISSING FROM YOUR EXAMPLE */
  requestAttributes: {
    '<String>': 'STRING_VALUE',
    /* '<String>': ... */
  },
  sessionAttributes: {
    '<String>': 'STRING_VALUE',
    /* '<String>': ... */
  }
};
lexruntime.postText(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

PostContent Documentation:

var params = {
  botAlias: 'STRING_VALUE', /* required */
  botName: 'STRING_VALUE', /* required */
  contentType: 'STRING_VALUE', /* required */
  inputStream: new Buffer('...') || 'STRING_VALUE' || streamObject, /* required */
  userId: 'STRING_VALUE', /* required - THIS IS MISSING FROM YOUR EXAMPLE */
  accept: 'STRING_VALUE',
  requestAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */,
  sessionAttributes: any /* This value will be JSON encoded on your behalf with JSON.stringify() */
};
lexruntime.postContent(params, function(err, data) {
  if (err) console.log(err, err.stack); // an error occurred
  else     console.log(data);           // successful response
});

现在,就像我之前说过的那样,我以前从未使用过AWS Amplify,所以我确实不确定,但是希望这可以为您指明正确的方向。

答案 2 :(得分:0)

您只需使用具有“运行 AWS lex chatbot”权限角色组/策略的用户登录即可。