4

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

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

看起来 Interactions 期待一个userId参数,它@aws-amplify/interactions/lib/Providers/AWSLexProvider.js应该从credentials.identityId. 但是,当我登录时credentials,它是 type ,根据文档SharedIniFileCredentials它没有identityId属性。

通过阅读文档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();

那么我在这里错过了什么?

4

3 回答 3

2

我在添加一个我没有使用完整放大设置手动创建的机器人时遇到了同样的问题,但只是使用放大反应前端 SDK 来使用聊天机器人组件。原来我使用了错误identityPoolId的 Cognito Auth。当使用正确的选项时,可以在where to find identity pool id in the cognito federated identities 部分中找到,错误消失并且机器人开始工作。此外,我保证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);
于 2019-08-27T07:08:44.430 回答
0

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

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

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

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

于 2019-01-13T23:56:21.470 回答
0

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

于 2021-03-28T23:47:40.407 回答