繁体   English   中英

如何从节点 js 发起与 AWS LEX 的对话?

[英]How do I initiate a conversation with AWS LEX from node js?

我的上下文是这样的:我正在尝试在我的 Mozilla Hubs 客户端中构建一个聊天机器人,这是一个节点 js / React 项目。 我在 AWS 上创建了一个 lex 机器人,并且我已经安装了 client-lex-runtime-v2 package 并且可以成功导入它,但我不知道如何设置 StartConversationCommand,给它提供凭据等。大多数 javascript示例似乎 go 以另一种方式,其中 Lex 调用 lambda function 在处理请求后,我有用户 Lex 在我的应用程序中输入,然后我需要将结果文本发送回我的应用程序。

这似乎是非常基本的 Lex 用法——如果有人能指出我的代码示例,我将不胜感激。

约翰,

您需要使用LexRuntimeV2Client中的 LexRuntimeV2Client,如此处所示

从链接的文档中,以下是您导入和实例化新客户端的方式:

import { LexRuntimeV2Client, DeleteSessionCommand } from "@aws-sdk/client-lex-runtime-v2";
const client = new LexRuntimeV2Client({ region: "REGION" });

一旦配置了您各自的 AWS 环境详细信息、凭证等,您将能够调用您的 Lex 机器人(同样,来自链接的文档):

try {
  const data = await client.send(command);
  // process data.
} catch (error) {
  // error handling.
} finally {
  // finally.
}

看看 GitHub 上的这个示例 repo: aws-lex-web-ui

因此,对于其他陷入困境的人来说,我不能说这是正确的方法,因为我从来没有让它工作,但我最接近至少形成我的凭据并尝试建立联系的是:

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

如前所述,买家要小心,祝你好运。 我希望这可能会对某人有所帮助。 我们暂时休息一下这个问题,直到我们团队中拥有更好 aws fu 的成员可以查看它::-)

//这对我有用。 //-------------------------

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.

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