Dialogflow 사용하기_2

김동호·2022년 4월 29일
0

Dialogflow

목록 보기
4/4
post-thumbnail

서버 적용

  • Dialogflow를 사용했을 때 앱뿐 아니라 웹에서도 사용하기 위해 웹 서버에서 Dialogflow를 요청해 보았습니다.
  • Python Flask를 개발해 기반으로 Dialogflow를 사용하였습니다.
  • Dialogflow인증 방식은 V2를 사용하였습니다.
    • 웹에서 인증 방법을 검색하였을때 V1방식에 대한 자료가 많았으나 Dialogflow에서 V2를 권장하여 V2로 인증을 받았습니다.

인증 방식

  1. V1 인증

    1. 기존의 인증 방식으로 Dialogflow Agent를 생성하면 설정 > General에 Access Key라고 표시가 되어 있는 곳이 있다.

      • 버전 업으로 인하여 내가 만든 Agent에는 Access Key라고 작성된 부분이 아예 없었다.
    2. Access Key를 request 할 때 Header에 포함하여 요청을 하면 인증이 끝난다.

  2. V2 인증

적용

  1. Module Install

    • dialogflow 0.5.1
    • google-api-core 1.4.1
  2. 환경 설정

    • 개발 테스트를 위해 프로젝트가 처음 실행 되는 곳에다가 입력 하였습니다.
    •    import os
          os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="auth.json"
          if __name__ == "__main__": # 이 코드가 있는 파일
           # app.debug = True 
          app.run()
  3. 작성( python 이라.. 들여쓰기 표현이 .. 엉성합니다... )

    •    import os
         import dialogflow from google.api_core.exceptions 
         import InvalidArgument
         
          DIALOGFLOW_PROJECT_ID = '[PROJECT_ID]'
          DIALOGFLOW_LANGUAGE_CODE = '[LANGUAGE]
          SESSION_ID = 'me'
         
         text_to_be_analyzed = "Howdy" # 요청 문장
         session_client = dialogflow.SessionsClient()
         session = session_client.session_path(DIALOGFLOW_PROJECT_ID, SESSION_ID)
         text_input = dialogflow.types.TextInput(text=text_to_be_analyzed, language_code=DIALOGFLOW_LANGUAGE_CODE)
         query_input = dialogflow.types.QueryInput(text=text_input)
        try:
      	response = session_client.detect_intent(session=session, query_input=query_input)
          except InvalidArgument:
          raise
          nprint("Query text:", response.query_result.query_text)
          print("Detected intent:", response.query_result.intent.display_name)
          print("Detected intent confidence:", response.query_result.intent_detection_confidence)
          print("Fulfillment text:",  response.query_result.fulfillment_text)
      

      참고 : https://medium.com/swlh/working-with-dialogflow-using-python-client-cb2196d579a4

profile
Backend Dev

0개의 댓글