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 Antwort
0

Try changing AWSRequestsAuth to AWS.AWSRequestsAuth.

profile pictureAWS
beantwortet vor einem Jahr
  • changing AWSRequestsAuth to AWS.AWSRequestsAuth I got this error : dex2.html:35 Uncaught TypeError: AWS.AWSRequestsAuth is not a constructor

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen