簡體   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