Servizio webhook

Per utilizzare il fulfillment in un sistema di produzione, devi implementare ed eseguire il deployment di un servizio webhook. Per gestire il fulfillment, il servizio webhook deve accettare richieste JSON e restituire risposte JSON come specificato in questa guida. Il flusso di elaborazione dettagliato per il fulfillment e i webhook è descritto nel documento Panoramica del fulfillment.

Requisiti per i servizi webhook

Il servizio webhook deve soddisfare i seguenti requisiti:

  • Gestire le richieste HTTPS. HTTP non è supportato. Se ospiti il tuo servizio webhook su Google Cloud utilizzando una soluzione di computing o serverless computing, consulta la documentazione del prodotto per la pubblicazione con HTTPS. Per altre opzioni di hosting, consulta Ottieni un certificato SSL per il tuo dominio.
  • Assicurati che l'URL del servizio webhook sia accessibile pubblicamente.
  • Gestisci le richieste POST con un corpo JSON WebhookRequest.
  • Rispondi alle richieste WebhookRequest con un corpo JSON WebhookResponse.

Autenticazione

X Elemento
Nome utente e password di accesso Per le impostazioni del webhook, puoi specificare i valori facoltativi di nome utente e password di accesso. Se fornito, Dialogflow aggiunge un'intestazione HTTP di autorizzazione alle richieste del webhook. Questa intestazione ha il seguente formato: "authorization: Basic <base 64 encoding of the string username:password>".
Intestazioni di autenticazione Per le impostazioni del webhook, puoi specificare coppie chiave-valore facoltative per l'intestazione HTTP. Se fornite, Dialogflow aggiunge queste intestazioni HTTP alle richieste del webhook. È prassi comune fornire una singola coppia con una chiave authorization.
Autenticazione integrata di Cloud Run Functions Puoi utilizzare l'autenticazione integrata quando utilizzi Cloud Run Functions. Per utilizzare questo tipo di autenticazione, non fornire nome utente, password o intestazioni di autorizzazione. Se fornisci uno di questi campi, non verrà utilizzato per l'autenticazione integrata.
Token di identità del servizio Puoi utilizzare i token di identità del servizio per l'autenticazione. Se non fornisci il nome utente e la password di accesso o un'intestazione con una chiave authorization, Dialogflow presuppone automaticamente che debbano essere utilizzati i token di identità del servizio e aggiunge un'intestazione HTTP di autorizzazione alle richieste webhook. Questa intestazione ha il seguente formato: "authorization: Bearer <identity token>".
Autenticazione TLS reciproca Consulta la documentazione sull'autenticazione TLS reciproca.

Richiesta webhook

Quando viene trovato un intent configurato per il fulfillment, Dialogflow invia una richiesta webhook POST HTTPS al tuo servizio webhook. Il corpo di questa richiesta è un oggetto JSON con informazioni sull'intent corrispondente.

Oltre alla query dell'utente finale, molte integrazioni inviano anche informazioni sull'utente finale. Ad esempio, un ID che identifica in modo univoco l'utente. Queste informazioni sono accessibili utilizzando il campo originalDetectIntentRequest nella richiesta del webhook, che contiene le informazioni inviate dalla piattaforma di integrazione.

Per saperne di più, consulta la documentazione di riferimento di WebhookRequest.

Di seguito è riportato un esempio di richiesta:

{
  "responseId": "response-id",
  "session": "projects/project-id/agent/sessions/session-id",
  "queryResult": {
    "queryText": "End-user expression",
    "parameters": {
      "param-name": "param-value"
    },
    "allRequiredParamsPresent": true,
    "fulfillmentText": "Response configured for matched intent",
    "fulfillmentMessages": [
      {
        "text": {
          "text": [
            "Response configured for matched intent"
          ]
        }
      }
    ],
    "outputContexts": [
      {
        "name": "projects/project-id/agent/sessions/session-id/contexts/context-name",
        "lifespanCount": 5,
        "parameters": {
          "param-name": "param-value"
        }
      }
    ],
    "intent": {
      "name": "projects/project-id/agent/intents/intent-id",
      "displayName": "matched-intent-name"
    },
    "intentDetectionConfidence": 1,
    "diagnosticInfo": {},
    "languageCode": "en"
  },
  "originalDetectIntentRequest": {}
}

Risposta webhook

Dopo aver ricevuto una richiesta, il webhook deve inviare una risposta. Il corpo di questa risposta è un oggetto JSON che contiene le seguenti informazioni:

Alla tua risposta si applicano le seguenti limitazioni:

  • Rispondi entro 10 secondi per le applicazioni dell'Assistente Google o entro 5 secondi per le altre applicazioni; in caso contrario, la richiesta scade.
  • Mantieni le dimensioni della risposta entro i 64 KiB.

Per saperne di più, consulta la documentazione di riferimento di WebhookResponse.

Messaggio di risposta

Di seguito è riportato un esempio di risposta di testo:

{
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "Text response from webhook"
        ]
      }
    }
  ]
}

Risposta della scheda

Di seguito è riportato un esempio di risposta con scheda:

{
  "fulfillmentMessages": [
    {
      "card": {
        "title": "card title",
        "subtitle": "card text",
        "imageUri": "https://example.com/images/example.png",
        "buttons": [
          {
            "text": "button text",
            "postback": "https://example.com/path/for/end-user/to/follow"
          }
        ]
      }
    }
  ]
}

Risposta dell'Assistente Google

Di seguito è riportato un esempio di risposta dell'Assistente Google:

{
  "payload": {
    "google": {
      "expectUserResponse": true,
      "richResponse": {
        "items": [
          {
            "simpleResponse": {
              "textToSpeech": "this is a Google Assistant response"
            }
          }
        ]
      }
    }
  }
}

Contesto

Di seguito è riportato un esempio che imposta il contesto di output:

{
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "Text response from webhook"
        ]
      }
    }
  ],
  "outputContexts": [
    {
      "name": "projects/project-id/agent/sessions/session-id/contexts/context-name",
      "lifespanCount": 5,
      "parameters": {
        "param-name": "param-value"
      }
    }
  ]
}

Evento

Di seguito è riportato un esempio che richiama un evento personalizzato:

{
  "followupEventInput": {
    "name": "event-name",
    "languageCode": "en-US",
    "parameters": {
      "param-name": "param-value"
    }
  }
}

Entità di sessione

Di seguito è riportato un esempio che imposta un'entità di sessione:

{
  "fulfillmentMessages": [
    {
      "text": {
        "text": [
          "Choose apple or orange"
        ]
      }
    }
  ],
  "sessionEntityTypes":[
    {
      "name":"projects/project-id/agent/sessions/session-id/entityTypes/fruit",
      "entities":[
        {
          "value":"APPLE_KEY",
          "synonyms":[
            "apple",
            "green apple",
            "crabapple"
          ]
        },
        {
          "value":"ORANGE_KEY",
          "synonyms":[
            "orange"
          ]
        }
      ],
      "entityOverrideMode":"ENTITY_OVERRIDE_MODE_OVERRIDE"
    }
  ]
}

Payload personalizzato

Di seguito è riportato un esempio che fornisce un payload personalizzato:

{
  "fulfillmentMessages": [
    {
      "payload": {
        "facebook": { // for Facebook Messenger integration
          "attachment": {
            "type": "",
            "payload": {}
          }
        },
        "slack": { // for Slack integration
          "text": "",
          "attachments": []
        },
        "richContent": [ // for Dialogflow Messenger integration
          [
            {
              "type": "image",
              "rawUrl": "https://example.com/images/logo.png",
              "accessibilityText": "Example logo"
            }
          ]
        ],
        // custom integration payload here
      }
    }
  ]
}

Attiva e gestisci il fulfillment

Per abilitare e gestire il fulfillment per l'agente con la console:

  1. Vai alla console di Dialogflow ES.
  2. Seleziona un agente.
  3. Seleziona Fulfillment nel menu della barra laterale.
  4. Imposta il campo Webhook su Abilitato.
  5. Fornisci i dettagli del servizio webhook nel modulo. Se il webhook non richiede l'autenticazione, lascia vuoti i campi di autenticazione.
  6. Fai clic su Salva.

Screenshot dell'attivazione del fulfillment.

Per abilitare e gestire il fulfillment per l'agente con l'API, consulta il riferimento dell'agente. I metodi getFulfillment e updateFulfillment consentono di gestire le impostazioni di fulfillment.

Per attivare il fulfillment per un intent con la console:

  1. Seleziona Intenti nel menu della barra laterale sinistra.
  2. Seleziona un intent.
  3. Vai alla sezione Fulfillment.
  4. Attiva l'opzione Abilita chiamata webhook per questo intent.
  5. Fai clic su Salva.

Per abilitare il fulfillment per un intent con l'API, consulta il riferimento agli intent e imposta il campo webhookState su WEBHOOK_STATE_ENABLED.

Errori webhook

Se il servizio webhook rileva un errore, deve restituire uno dei seguenti codici di stato HTTP:

  • 400: richiesta errata
  • 401: non autorizzato
  • 403: Forbidden
  • 404: non trovato
  • 500: errore interno del server
  • 503: servizio non disponibile

In una qualsiasi delle seguenti situazioni di errore, Dialogflow risponde all'utente finale con la risposta integrata configurata per l'intent corrispondente:

  • Il timeout della risposta è stato superato.
  • È stato ricevuto un codice di stato di errore.
  • La risposta non è valida.
  • Il servizio webhook non è disponibile.

Inoltre, se una chiamata API di rilevamento dell'intent attiva la corrispondenza dell'intent, il campo status nella risposta di rilevamento dell'intent contiene le informazioni sull'errore del webhook. Ad esempio:

"status": {
    "code": 206,
    "message": "Webhook call failed. <details of the error...>"
}

Nuovi tentativi automatici

Dialogflow ES include meccanismi interni che riprovano automaticamente in caso di determinati errori del webhook per migliorare la robustezza. Vengono ripetuti solo gli errori non terminali, come gli errori di timeout o di connessione.

Per ridurre la probabilità di chiamate duplicate:

  • Imposta soglie di timeout del webhook più lunghe.
  • Supporta l'idempotenza nella logica del webhook o deduplica le richieste.

Utilizzo di Cloud Run Functions

Puoi utilizzare Cloud Run Functions per il fulfillment in diversi modi. L'editor in linea di Dialogflow si integra con Cloud Run Functions. Quando utilizzi l'editor in linea per creare e modificare il codice del webhook, Dialogflow stabilisce una connessione sicura alla tua Cloud Function.

Puoi anche utilizzare una Cloud Function non creata dall'editor in linea. Se la Cloud Function si trova nello stesso progetto del tuo agente, quest'ultimo può chiamare il webhook senza bisogno di una configurazione speciale.

Tuttavia, devi configurare manualmente questa integrazione nelle seguenti due situazioni:

  1. Il service account Dialogflow Service Agent con il seguente indirizzo deve esistere per il progetto dell'agente:
    service-agent-project-number@gcp-sa-dialogflow.
    Questo account di servizio speciale e la chiave associata vengono normalmente creati automaticamente quando crei il primo agente per un progetto. Se l'agente è stato creato prima del 10 maggio 2021, potrebbe essere necessario attivare la creazione di questo account di servizio speciale con quanto segue:
    1. Crea un nuovo agente per il progetto.
    2. Esegui questo comando:
      gcloud beta services identity create --service=dialogflow.googleapis.com --project=agent-project-id
  2. Se la funzione webhook si trova in un progetto diverso da quello dell'agente, devi fornire il ruolo IAM Cloud Functions Invoker al account di servizio Dialogflow Service Agent nel progetto della funzione.

Token di identità del servizio

Quando Dialogflow chiama un webhook, fornisce un token di identità Google con la richiesta. Qualsiasi webhook può facoltativamente convalidare il token utilizzando le librerie client di Google o librerie open source come github.com/googleapis/google-auth-library-nodejs. Ad esempio, puoi verificare il campo email del token ID come segue:

service-agent-project-number@gcp-sa-dialogflow.

Esempi

I seguenti esempi mostrano come ricevere un WebhookRequest e inviare un WebhookResponse. Questi esempi utilizzano gli intent creati nella guida rapida.

Go

Per eseguire l'autenticazione in Dialogflow CX, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
)

type intent struct {
	DisplayName string `json:"displayName"`
}

type queryResult struct {
	Intent intent `json:"intent"`
}

type text struct {
	Text []string `json:"text"`
}

type message struct {
	Text text `json:"text"`
}

// webhookRequest is used to unmarshal a WebhookRequest JSON object. Note that
// not all members need to be defined--just those that you need to process.
// As an alternative, you could use the types provided by
// the Dialogflow protocol buffers:
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/dialogflow/v2#WebhookRequest
type webhookRequest struct {
	Session     string      `json:"session"`
	ResponseID  string      `json:"responseId"`
	QueryResult queryResult `json:"queryResult"`
}

// webhookResponse is used to marshal a WebhookResponse JSON object. Note that
// not all members need to be defined--just those that you need to process.
// As an alternative, you could use the types provided by
// the Dialogflow protocol buffers:
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/dialogflow/v2#WebhookResponse
type webhookResponse struct {
	FulfillmentMessages []message `json:"fulfillmentMessages"`
}

// welcome creates a response for the welcome intent.
func welcome(request webhookRequest) (webhookResponse, error) {
	response := webhookResponse{
		FulfillmentMessages: []message{
			{
				Text: text{
					Text: []string{"Welcome from Dialogflow Go Webhook"},
				},
			},
		},
	}
	return response, nil
}

// getAgentName creates a response for the get-agent-name intent.
func getAgentName(request webhookRequest) (webhookResponse, error) {
	response := webhookResponse{
		FulfillmentMessages: []message{
			{
				Text: text{
					Text: []string{"My name is Dialogflow Go Webhook"},
				},
			},
		},
	}
	return response, nil
}

// handleError handles internal errors.
func handleError(w http.ResponseWriter, err error) {
	w.WriteHeader(http.StatusInternalServerError)
	fmt.Fprintf(w, "ERROR: %v", err)
}

// HandleWebhookRequest handles WebhookRequest and sends the WebhookResponse.
func HandleWebhookRequest(w http.ResponseWriter, r *http.Request) {
	var request webhookRequest
	var response webhookResponse
	var err error

	// Read input JSON
	if err = json.NewDecoder(r.Body).Decode(&request); err != nil {
		handleError(w, err)
		return
	}
	log.Printf("Request: %+v", request)

	// Call intent handler
	switch intent := request.QueryResult.Intent.DisplayName; intent {
	case "Default Welcome Intent":
		response, err = welcome(request)
	case "get-agent-name":
		response, err = getAgentName(request)
	default:
		err = fmt.Errorf("Unknown intent: %s", intent)
	}
	if err != nil {
		handleError(w, err)
		return
	}
	log.Printf("Response: %+v", response)

	// Send response
	if err = json.NewEncoder(w).Encode(&response); err != nil {
		handleError(w, err)
		return
	}
}

Java

Per eseguire l'autenticazione in Dialogflow CX, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.


// TODO: add GSON dependency to Pom file
// (https://mvnrepository.com/artifact/com.google.code.gson/gson/2.8.5)
// TODO: Uncomment the line bellow before running cloud function
// package com.example;

import com.google.cloud.functions.HttpFunction;
import com.google.cloud.functions.HttpRequest;
import com.google.cloud.functions.HttpResponse;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.BufferedWriter;

public class Example implements HttpFunction {

  public void service(HttpRequest request, HttpResponse response) throws Exception {
    JsonParser parser = new JsonParser();
    Gson gson = new GsonBuilder().create();

    JsonObject job = gson.fromJson(request.getReader(), JsonObject.class);
    String str =
        job.getAsJsonObject("queryResult")
            .getAsJsonObject("intent")
            .getAsJsonPrimitive("displayName")
            .toString();
    JsonObject o = null;
    String a = '"' + "Default Welcome Intent" + '"';
    String b = '"' + "get-agent-name" + '"';
    String responseText = "";

    if (str.equals(a)) {
      responseText = '"' + "Hello from a Java GCF Webhook" + '"';
    } else if (str.equals(b)) {
      responseText = '"' + "My name is Flowhook" + '"';
    } else {
      responseText = '"' + "Sorry I didn't get that" + '"';
    }

    o =
        parser
            .parse(
                "{\"fulfillmentMessages\": [ { \"text\": { \"text\": [ "
                    + responseText
                    + " ] } } ] }")
            .getAsJsonObject();

    BufferedWriter writer = response.getWriter();
    writer.write(o.toString());
  }
}

Node.js

Per eseguire l'autenticazione in Dialogflow CX, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

const functions = require('@google-cloud/functions-framework');

// TODO: Add handleWebhook to 'Entry point' in the Google Cloud Function
functions.http('handleWebhook', (request, response) => {
  const tag = request.body.queryResult.intent.displayName;

  let jsonResponse = {};
  if (tag === 'Default Welcome Intent') {
    //fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
    jsonResponse = {
      fulfillment_messages: [
        {
          text: {
            //fulfillment text response to be sent to the agent
            text: ['Hello from a GCF Webhook'],
          },
        },
      ],
    };
  } else if (tag === 'get-name') {
    //fulfillment response to be sent to the agent if the request tag is equal to "welcome tag"
    jsonResponse = {
      fulfillment_messages: [
        {
          text: {
            //fulfillment text response to be sent to the agent
            text: ['My name is Flowhook'],
          },
        },
      ],
    };
  } else {
    jsonResponse = {
      //fulfillment text response to be sent to the agent if there are no defined responses for the specified tag
      fulfillment_messages: [
        {
          text: {
            ////fulfillment text response to be sent to the agent
            text: [
              `There are no fulfillment responses defined for "${tag}"" tag`,
            ],
          },
        },
      ],
    };
  }
  response.send(jsonResponse);
});

Python

Per eseguire l'autenticazione in Dialogflow CX, configura le Credenziali predefinite dell'applicazione. Per saperne di più, consulta Configura l'autenticazione per un ambiente di sviluppo locale.

# TODO: change the default Entry Point text to handleWebhook
import functions_framework


@functions_framework.http
def handleWebhook(request):
    req = request.get_json()

    responseText = ""
    intent = req["queryResult"]["intent"]["displayName"]

    if intent == "Default Welcome Intent":
        responseText = "Hello from a GCF Webhook"
    elif intent == "get-agent-name":
        responseText = "My name is Flowhook"
    else:
        responseText = f"There are no fulfillment responses defined for Intent {intent}"

    # You can also use the google.cloud.dialogflowcx_v3.types.WebhookRequest protos instead of manually writing the json object
    res = {"fulfillmentMessages": [{"text": {"text": [responseText]}}]}

    return res