Frontend ๐Ÿ“š/React

[Node] Dialogflow API๋ฅผ ํ™œ์šฉํ•œ ์ฑ—๋ด‡ ์‚ฌ์ดํŠธ - 3. TextQuery Function, EventQuery Function

leejaejae 2024. 8. 16. 16:54

โ˜… ์ด ๋ถ€๋ถ„์€ ์•ž์„œ ๋งŒ๋“ค์—ˆ๋˜ TextQuery Route์™€ EventQuery Route ๋“ฑ์˜ ์„œ๋ฒ„ ๋ถ€๋ถ„๊ณผ ์‘๋‹ตํ•˜๋Š” React ๋ถ€๋ถ„์œผ๋กœ ํ•„์š”์— ์˜ํ•ด ์ƒ๋žต ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค!

1. ์ „์ฒด ์ฝ”๋“œ

// Chatbot.js

import React, { useEffect } from "react";
import Axios from "axios";

function Chatbot() {
  useEffect(() => {
    eventQuery("greeting");  // Dialogflow์— ๋“ฑ๋กํ•ด๋‘” Event
  }, []);

  // Text Query
  const textQuery = async (text) => {
    // ๋‚ด๊ฐ€ ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€ ๋ณด๊ด€
    let conversation = {
      who: "user",
      content: {
        text: {
          text: text,
        },
      },
    };

    // chatbot์ด ๋ณด๋‚ธ ๋ฉ”์„ธ์ง€ ๋ณด๊ด€
    const textQueryVariables = {
      text: text,
    };

    try {
      // text query route์— request ์ „์†ก
      const response = await Axios.post(
        "api/dialogflow/textQuery",
        textQueryVariables
      );
      const content = response.data.fulfillmentMessages[0];

      conversation = {
        who: "bot",
        content: {
          text: {
            text: content,
          },
        },
      };

      console.log(conversation);
    } catch (error) {
      conversation = {
        who: "bot",
        content: {
          text: {
            text: "Error juse occured, please check the problem",
          },
        },
      };
      console.log(conversation);
    }
  };

  // Event Query
  const eventQuery = async (event) => {
    const eventQueryVariables = {
      event,
    };

    try {
      const response = await Axios.post(
        "api/dialogflow/eventQuery",
        eventQueryVariables
      );
      const content = response.data.fulfillmentMessages[0];

      let conversation = {
        who: "bot",
        content: {
          text: {
            text: content,
          },
        },
      };

      console.log(conversation);
    } catch (error) {
      let conversation = {
        who: "bot",
        content: {
          text: {
            text: "Error juse occured, please check the problem",
          },
        },
      };
      console.log(conversation);
    }
  };

  const keyPressHandler = (e) => {
    if (e.key === "Enter") {
      if (!e.target.value) {
        return alert("you need to type something first");
      }
      // text query route๋กœ request ์ „์†ก
      textQuery(e.target.value);
      e.target.value = "";
    }
  };
  return (
    <div
      style={{
        height: 700,
        width: 700,
        border: "3px solid black",
        borderRadius: "7px",
      }}
    >
      <div style={{ height: 639, width: "100%", overflow: "auto" }}></div>

      <input
        style={{
          margin: 0,
          width: "98.5%",
          height: 50,
          borderRadius: "4px",
          padding: "5px",
          fontSize: "1rem",
        }}
        placeholder="Send a message..."
        onKeyDown={keyPressHandler}
        type="text"
      />
    </div>
  );
}
export default Chatbot;

 

2. ๊ฒฐ๊ณผ ๐ŸŽ‰


โ˜… ๋”ฐ๋ผํ•˜๋ฉฐ ๋ฐฐ์šฐ๋Š” ๋…ธ๋“œ, ๋ฆฌ์•กํŠธ ์‹œ๋ฆฌ์ฆˆ - ์ฑ—๋ด‡ ์‚ฌ์ดํŠธ ๋งŒ๋“ค๊ธฐ ๊ฐ•์˜๋ฅผ ์ฐธ๊ณ ํ•ฉ๋‹ˆ๋‹ค.