[英]How do I initiate a conversation with AWS LEX from node js?
My context is this: I am attempting to build a chat bot into my Mozilla Hubs client, which is a node js / React project.我的上下文是这样的:我正在尝试在我的 Mozilla Hubs 客户端中构建一个聊天机器人,这是一个节点 js / React 项目。 I have a lex bot created on AWS, and I have installed the client-lex-runtime-v2 package and can import it successfully, but I have no idea how to set up a StartConversationCommand, give it credentials, etc. Most of the javascript examples seem to go the other way, where Lex calls a lambda function after processing a request, but I have user input in my app and I need to send it to Lex, and then deliver the resulting text back to the user inside my application.
我在 AWS 上创建了一个 lex 机器人,并且我已经安装了 client-lex-runtime-v2 package 并且可以成功导入它,但我不知道如何设置 StartConversationCommand,给它提供凭据等。大多数 javascript示例似乎 go 以另一种方式,其中 Lex 调用 lambda function 在处理请求后,我有用户 Lex 在我的应用程序中输入,然后我需要将结果文本发送回我的应用程序。
This seems like very basic Lex usage - if anyone could point me to a code example I would be most grateful.这似乎是非常基本的 Lex 用法——如果有人能指出我的代码示例,我将不胜感激。
John,约翰,
You need to make use of the LexRuntimeV2Client
in the SDK as demonstrated here .您需要使用
LexRuntimeV2Client
中的 LexRuntimeV2Client,如此处所示。
From the linked documentation, the below is how you import and instantiate a new client:从链接的文档中,以下是您导入和实例化新客户端的方式:
import { LexRuntimeV2Client, DeleteSessionCommand } from "@aws-sdk/client-lex-runtime-v2";
const client = new LexRuntimeV2Client({ region: "REGION" });
Once configured with your respective AWS environment details, credentials etc you will be able to invoke your Lex bot (again, from the linked documentation):一旦配置了您各自的 AWS 环境详细信息、凭证等,您将能够调用您的 Lex 机器人(同样,来自链接的文档):
try {
const data = await client.send(command);
// process data.
} catch (error) {
// error handling.
} finally {
// finally.
}
Take a look at this sample repo on GitHub as well: aws-lex-web-ui看看 GitHub 上的这个示例 repo: aws-lex-web-ui
So for anyone else stuck where I was, I cannot say this is the right way to do it, because I never did get it working, but the closest I got to at least forming up my credentials and trying to make a connection was this:因此,对于其他陷入困境的人来说,我不能说这是正确的方法,因为我从来没有让它工作,但我最接近至少形成我的凭据并尝试建立联系的是:
const client = new LexRuntimeV2Client({ region: "us-east-1", credentials: new AWS.Credentials({ accessKeyId: "my_IAM_access_key_id", secretAccessKey: "my_secret_access_key" }) }); const lexparams = { "botAliasId": "my alias_id", "botId": "bot_id_from_lex", "localeId": "en_US", "inputText": "hello, this is a test sample", "sessionId": "some_session_id" }; let cmd = new StartConversationCommand(lexparams); try { const data = await client.send(cmd); console.log("Success. Response is: ", data.message); } catch (err) { console.log("Error responding to message. ", err); }
As said, buyer beware, and best of luck out there.如前所述,买家要小心,祝你好运。 I hope this might help somebody in some slight way.
我希望这可能会对某人有所帮助。 We taking a momentary break on this problem until a member of our team with better aws fu can take a look at it: :-)
我们暂时休息一下这个问题,直到我们团队中拥有更好 aws fu 的成员可以查看它::-)
//This is working for me. //这对我有用。 //-------------------------
//-------------------------
var AWS = require('aws-sdk');
const { LexRuntimeV2 } = require("@aws-sdk/client-lex-runtime-v2");
const lexruntime = new LexRuntimeV2({
region: "us-west-2", // Set your Bot Region
credentials: new AWS.Credentials({
accessKeyId: "***", // Add your access IAM accessKeyId
secretAccessKey: "***" // Add your access IAM secretAccessKey
})
});
const lexparams = {
"botAliasId": "HG****", // Enter the botAliasId
"botId": "HG***", // Enter the botId
"localeId": "en_US",
"text": "Book Car",
"sessionId": "some_session_id"
};
lexruntime.recognizeText(lexparams, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.