빌드 처리 (Dialogflow)

처리는 사용자 입력을 가져오기 위한 작업 프로젝트와 입력을 처리하고 최종적으로 작업을 처리하는 로직을 정의합니다.

개요

처리는 어시스턴트로부터 요청을 수신하고 요청을 처리하고 응답합니다. 이러한 요청 및 응답 프로세스를 통해 최초 사용자 요청을 처리할 때까지 대화를 계속 진행합니다.

다음 단계에서는 Node.js 또는 자바/Kotlin 클라이언트 라이브러리와 함께 Actions SDK를 사용하여 처리를 빌드하는 방법을 설명합니다.

  1. ActionsSdkApp 객체를 초기화합니다.
  2. 처리 로직에서 요청을 처리하는 함수를 만듭니다.

대화상자 빌드

ActionsSdkApp 객체 초기화

다음 코드는 ActionsSdkApp를 인스턴스화하고 Google Cloud Functions에 대한 상용구 Node.js 설정을 수행합니다.

Actions SDK (Node.js)
'use strict';

const {actionssdk} = require('actions-on-google');
const functions = require('firebase-functions');

const app = actionssdk({debug: true});

app.intent('actions.intent.MAIN', (conv) => {
  conv.ask('Hi!');
});

// More intent handling if needed
exports.myFunction = functions.https.onRequest(app);
Actions SDK (자바)
ResponseBuilder responseBuilder = getResponseBuilder(request).add("Hi!");
return responseBuilder.build();
JSON
{
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "Hi!"
              }
            }
          ]
        }
      },
      "possibleIntents": [
        {
          "intent": "actions.intent.TEXT"
        }
      ]
    }
  ],
  "conversationToken": "{\"data\":{}}",
  "userStorage": "{\"data\":{}}"
}

요청을 처리하는 함수 만들기

사용자가 문구를 말하면 Google 어시스턴트로부터 요청을 받게 됩니다. 요청에 들어오는 인텐트를 처리하려면 트리거된 인텐트를 처리하는 함수를 만듭니다.

요청을 처리하려면 다음 단계를 따르세요.

  1. 사용자 입력을 처리하는 데 필요한 로직을 실행합니다.

  2. 인수로 표시하려는 응답을 전달하는 conv.ask() 함수를 호출합니다.

다음 코드는 간단한 응답을 빌드하는 방법을 보여줍니다.

Actions SDK (Node.js)
conv.ask(`Hi! Say something, and I'll repeat it.`);
Actions SDK (자바)
ResponseBuilder responseBuilder =
    getResponseBuilder(request).add("Hi! Say something, and I'll repeat it.");
return responseBuilder.build();
JSON
{
  "expectUserResponse": true,
  "expectedInputs": [
    {
      "inputPrompt": {
        "richInitialPrompt": {
          "items": [
            {
              "simpleResponse": {
                "textToSpeech": "Hi! Say something, and I'll repeat it."
              }
            }
          ]
        }
      },
      "possibleIntents": [
        {
          "intent": "actions.intent.TEXT"
        }
      ]
    }
  ],
  "conversationToken": "{\"data\":{}}",
  "userStorage": "{\"data\":{}}"
}

인텐트 처리

트리거된 인텐트를 처리할 모든 함수가 준비되었으면 app.intent를 사용하여 인텐트에 핸들러를 할당합니다.

Actions SDK (Node.js)
app.intent('actions.intent.TEXT', (conv) => {
  // handle text intent.
});
app.intent('actions.intent.MAIN', (conv) => {
  // handle main intent.
});
Actions SDK (자바)
@ForIntent("actions.intent.MAIN")
public ActionResponse main(ActionRequest request) {
  // handle main intent
  // ...
}

@ForIntent("actions.intent.TEXT")
public ActionResponse text(ActionRequest request) {
  // handle text intent
  // ...
}

대화 종료

반환되는 사용자 입력을 더 이상 원하지 않고 대화를 종료하려면 conv.close() 함수를 호출합니다. 이 함수는 Google 어시스턴트에게 사용자에게 텍스트를 말하고 마이크를 닫아 대화를 종료하라고 지시합니다.

기본 호출 인텐트 처리

사용자가 app.intent.action.MAIN 인텐트를 트리거하면 일반적으로 사용자 입력 처리를 실행할 필요가 없습니다. 작업 패키지에 많은 작업이 포함되어 있고 많은 사용 사례가 포함되어 있다면 사용자가 할 수 있는 몇 가지 작업을 알려주어 사용자가 방향을 찾는 것이 좋습니다.

  1. 응답을 인수로 전달하는 conv.ask() 함수를 호출합니다. Google 어시스턴트는 사용자에게 응답을 말한 다음 지정된 인텐트 중 하나를 사용자가 트리거할 때까지 기다립니다.

다음 스니펫은 간단한 시작 인텐트를 처리하는 방법을 보여줍니다.

Actions SDK (Node.js)
// handle the initialTrigger
function handleMainIntent(conv, input) {
  conv.ask(input);
}
Actions SDK (자바)
private ActionResponse handleMainIntent(ResponseBuilder rb, String input) {
  return rb.add(input).build();
}

대화 상태

대화 HTTP/JSON 웹훅 API를 사용하는 경우 사용자와 Google 어시스턴트 간에 전달되는 JSON 형식의 객체 (conversationToken)를 사용하여 대화 상태를 유지할 수 있습니다. Node.js 클라이언트 라이브러리를 사용하는 경우 conv.data 필드에서 직접 쓰고 읽을 수 있습니다. 이 필드는 요청과 응답 간에 자동으로 전달됩니다.

Actions SDK (Node.js)
conv.data = {something: 10};
let value = conv.data.something;
Actions SDK (자바)
ResponseBuilder rb = getResponseBuilder(request);
rb.getConversationData().put("something", 10);
Object value = rb.getConversationData().get("something");