lex bot web integration using api

0

I'm trying to integrate a lex bot into a simple web page that takes the input from the user and pass it to the api, then the api response should be displayed into the same page as any simple chat bot, the problem is that I always get this error :

caught ReferenceError: AWSRequestsAuth is not defined

although the aws_requests_auth is installed correctly.

this is the script I use fro the web page :

`<!DOCTYPE html>
<html>
<head>
  <title>My Chatbot Page</title>
</head>
<body>
  <h1>My Chatbot Page</h1>
  <input id="user-input" type="text" placeholder="Type your message here">
  <button id="send-btn">Send</button>
  <p id="bot-response"></p>

  <script src="https://sdk.amazonaws.com/js/aws-sdk-2.790.0.min.js"></script>
  <script src="https://unpkg.com/aws-sdk/dist/aws-sdk.min.js"></script>
  <script src="https://sdk.amazonaws.com/js/aws-sdk-2.982.0.min.js"></script>


  <script>
    const API_ENDPOINT = 'https://runtime-v2-lex.us-east-1.amazonaws.com/bots/BOT_ID/aliases/BOT_ALIAS/user/BOT_USER_ID/text';
    const AWS_ACCESS_KEY = 'XXXXXXXXXX';
    const AWS_SECRET_KEY = 'XXXXXX';
    const AWS_REGION = 'us-east-1';

    const userInputElem = document.getElementById('user-input');
    const sendBtn = document.getElementById('send-btn');
    const botResponseElem = document.getElementById('bot-response');

    function sendMessage(userInput) {
      const requestHeaders = new Headers();
      requestHeaders.append('Content-Type', 'application/json');
      requestHeaders.append('X-Amz-Content-Sha256', 'XXXXXXXXX');
      requestHeaders.append('X-Amz-Date', new Date().toISOString());

      const requestOptions = {
        method: 'POST',
        headers: requestHeaders,
        body: JSON.stringify({ text: userInput }),
      };

      const auth = new AWSRequestsAuth({
        accessKeyId: AWS_ACCESS_KEY,
        secretAccessKey: AWS_SECRET_KEY,
        region: AWS_REGION,
        service: 'lex',
      });

      auth.sign(requestOptions);

      fetch(API_ENDPOINT, requestOptions)
        .then(response => response.json())
        .then(response => {
          const messages = response.messages.filter(message => message.contentType === 'PlainText');
          const botMessage = messages.length > 0 ? messages[0].content : 'Sorry, I did not understand that.';
          botResponseElem.textContent = botMessage;
        })
        .catch(error => console.error(error));
    }

    sendBtn.addEventListener('click', () => {
      const userInput = userInputElem.value.trim();
      if (userInput) {
        sendMessage(userInput);
      }
    });
  </script>
</body>
</html>
`
1 回答
0

Try changing AWSRequestsAuth to AWS.AWSRequestsAuth.

profile pictureAWS
已回答 1 年前
  • changing AWSRequestsAuth to AWS.AWSRequestsAuth I got this error : dex2.html:35 Uncaught TypeError: AWS.AWSRequestsAuth is not a constructor

您未登录。 登录 发布回答。

一个好的回答可以清楚地解答问题和提供建设性反馈,并能促进提问者的职业发展。

回答问题的准则

相关内容