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
답변함 일 년 전
  • changing AWSRequestsAuth to AWS.AWSRequestsAuth I got this error : dex2.html:35 Uncaught TypeError: AWS.AWSRequestsAuth is not a constructor

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠