问题
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?
回答1:
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 identityPoolId
for 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);
回答2:
I've not used AWS Amplify before so this answer may not be the reason, but I have used Amazon Lex many times before. The UserId field that this is looking for may be the UserId parameter for the Lex PostText/PostContent request (please see below code)
From the 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
});
and the 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
});
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.
来源:https://stackoverflow.com/questions/54148545/aws-amplify-missingrequiredparameter-userid-error