简体   繁体   English

AWS Amplify MissingRequiredParameter userId 错误

[英]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:当我在 Interactions 上调用send方法时,出现以下错误:

(node:27796) UnhandledPromiseRejectionWarning: MissingRequiredParameter: Missing required key 'userId' in params (节点:27796)未处理的PromiseRejectionWarning:MissingRequiredParameter:参数中缺少必需的键“userId”

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 .看起来 Interactions 需要一个userId参数,该参数在@aws-amplify/interactions/lib/Providers/AWSLexProvider.js中应该是从credentials.identityId提取的。 However, when I log credentials , it is type SharedIniFileCredentials , which does not have an identityId property according to the documentation .但是,当我记录credentials ,它是SharedIniFileCredentials类型, 根据文档,它没有identityId属性。

From reading the docs , identityId would have to be a Cognito user.通过阅读文档identityId必须是 Cognito 用户。 AWSLexProvider.js makes no attempt at calling CognitoIdentityCredentials to get Cognito credentials. AWSLexProvider.js不会尝试调用CognitoIdentityCredentials来获取 Cognito 凭证。

Hence, I am not sure where identityId is supposed to come from .因此,我不确定identityId应该来自哪里

My code is the example from the Amplify website:我的代码是来自 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();

So what am I missing here?那么我在这里错过了什么?

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.添加一个我使用完整的放大设置手动创建的机器人时,我遇到了同样的问题,但只是使用放大反应前端 SDK 来使用聊天机器人组件。 Turned out that I used the wrong identityPoolId for Cognito Auth.原来我在 Cognito Auth 中使用了错误的identityPoolId 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.当使用正确的方法时,如此处所述,在 cognito 联合身份部分中可以找到身份池 ID的位置,错误消失了,机器人开始工作。 Additionally I assured that the custom_auth_role assigned to that identity pool has addtionally the following properties under actions:此外,我放心, custom_auth_role分配给身份池有addtionally下行动以下特性:

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

This can be assgined in the IAM -> Roles Section for that role.这可以在 IAM -> 角色部分中为该角色分配。 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);

I've not used AWS Amplify before so this answer may not be the reason, but I have used Amazon Lex many times before.我之前没有使用过 AWS Amplify,所以这个答案可能不是原因,但我之前多次使用过 Amazon Lex。 The UserId field that this is looking for may be the UserId parameter for the Lex PostText/PostContent request (please see below code)正在寻找的 UserId 字段可能是 Lex PostText/PostContent 请求的 UserId 参数(请参见下面的代码)

From the PostText Documentation:来自PostText 文档:

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

and the PostContent Documentation:PostContent 文档:

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

Now, like I said earlier, I've not used AWS Amplify before so I honestly am not sure, but hopefully this points you in the right direction.现在,就像我之前所说的,我之前没有使用过 AWS Amplify,所以老实说我不确定,但希望这为您指明了正确的方向。

您只需要使用附加了“运行 AWS lex chatbot”权限角色组/策略的用户登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM