Crea un servicio de webhook

El agente compilado previamente que creaste en el último paso requiere un webhook. En este instructivo, se usan Cloud Functions para alojar el webhook debido a su simplicidad, pero hay muchas otras formas en las que puedes alojarlo. En el ejemplo, también se usa el lenguaje de programación Go, pero puedes usar cualquier lenguaje compatible con Cloud Functions.

Crea la función

Cloud Functions se puede crear con la consola de Google Cloud (consulta la documentación, abre la consola). Sigue estos pasos para crear una función en este instructivo:

  1. Es importante que tu agente de Dialogflow y la función estén en el mismo proyecto. Esta es la forma más fácil de que Dialogflow tenga un acceso seguro a tu función. Antes de crear la función, selecciona tu proyecto en la consola de Google Cloud.

    Ir al selector de proyectos

  2. Abre la página Descripción general de Cloud Functions:

    Ir a la descripción general de Cloud Functions

  3. Haz clic en Crear función y configura los siguientes campos:

    • Entorno: 1a gen.
    • Nombre de la función: tutorial-teCommunication-webhook
    • Región: Si especificaste una región para tu agente, usa la misma región.
    • Tipo de activador HTTP: HTTP
    • URL: Haz clic en el botón Copiar aquí y guarda el valor. Necesitarás esta URL cuando configures el webhook.
    • Autenticación: Solicitar autenticación
    • Requerir HTTPS: marcado
  4. Haz clic en Guardar.

  5. Haz clic en Siguiente (no necesitas un entorno de ejecución, una compilación, conexiones ni una configuración de seguridad especiales).

  6. Configura los siguientes campos:

    • Entorno de ejecución: Selecciona el entorno de ejecución de Go más reciente.
    • Código fuente: Editor intercalado
    • Punto de entrada: HandleWebhookRequest
  7. Reemplaza el código por lo siguiente:

    package cxtwh
    
    import (
    	"context"
    	"encoding/json"
    	"fmt"
    	"log"
    	"net/http"
    	"os"
    	"strings"
    
    	"cloud.google.com/go/spanner"
      "google.golang.org/grpc/codes"
    )
    
    // client is a Spanner client, created only once to avoid creation
    // for every request.
    // See: https://cloud.google.com/functions/docs/concepts/go-runtime#one-time_initialization
    var client *spanner.Client
    
    func init() {
    	// If using a database, these environment variables will be set.
    	pid := os.Getenv("PROJECT_ID")
    	iid := os.Getenv("SPANNER_INSTANCE_ID")
    	did := os.Getenv("SPANNER_DATABASE_ID")
    	if pid != "" && iid != "" && did != "" {
    		db := fmt.Sprintf("projects/%s/instances/%s/databases/%s",
    			pid, iid, did)
    		log.Printf("Creating Spanner client for %s", db)
    		var err error
    		// Use the background context when creating the client,
    		// but use the request context for calls to the client.
    		// See: https://cloud.google.com/functions/docs/concepts/go-runtime#contextcontext
    		client, err = spanner.NewClient(context.Background(), db)
    		if err != nil {
    			log.Fatalf("spanner.NewClient: %v", err)
    		}
    	}
    }
    
    type fulfillmentInfo struct {
    	Tag string `json:"tag"`
    }
    
    type sessionInfo struct {
    	Session    string                 `json:"session"`
    	Parameters map[string]interface{} `json:"parameters"`
    }
    
    type text struct {
    	Text []string `json:"text"`
    }
    
    type responseMessage struct {
    	Text text `json:"text"`
    }
    
    type fulfillmentResponse struct {
    	Messages []responseMessage `json:"messages"`
    }
    
    // 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://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/dialogflow/cx/v3#WebhookRequest
    type webhookRequest struct {
    	FulfillmentInfo fulfillmentInfo `json:"fulfillmentInfo"`
    	SessionInfo     sessionInfo     `json:"sessionInfo"`
    }
    
    // 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://pkg.go.dev/google.golang.org/genproto/googleapis/cloud/dialogflow/cx/v3#WebhookResponse
    type webhookResponse struct {
    	FulfillmentResponse fulfillmentResponse `json:"fulfillmentResponse"`
    	SessionInfo         sessionInfo         `json:"sessionInfo"`
    }
    
    // detectCustomerAnomaly handles same-named tag.
    func detectCustomerAnomaly(ctx context.Context, request webhookRequest) (
    	webhookResponse, error) {
    	// Create session parameters that are populated in the response.
    	// This example hard codes values, but a real system
    	// might look up this value in a database.
    	p := map[string]interface{}{
    		"anomaly_detect":        "false",
    		"purchase":              "device protection",
    		"purchase_amount":       "12.25",
    		"bill_without_purchase": "54.34",
    		"total_bill":            "66.59",
    		"first_month":           "January 1",
    	}
    	// Build and return the response.
    	response := webhookResponse{
    		SessionInfo: sessionInfo{
    			Parameters: p,
    		},
    	}
    	return response, nil
    }
    
    // validatePhoneLine handles same-named tag.
    func validatePhoneLine(ctx context.Context, request webhookRequest) (
    	webhookResponse, error) {
    	// Create session parameters that are populated in the response.
    	// This example hard codes values, but a real system
    	// might look up this value in a database.
    	p := map[string]interface{}{
    		"domestic_coverage":   "true",
    		"phone_line_verified": "true",
    	}
    	// Build and return the response.
    	response := webhookResponse{
    		SessionInfo: sessionInfo{
    			Parameters: p,
    		},
    	}
    	return response, nil
    }
    
    // cruisePlanCoverage handles same-named tag.
    func cruisePlanCoverage(ctx context.Context, request webhookRequest) (
    	webhookResponse, error) {
    	// Get the existing parameter values
    	port := request.SessionInfo.Parameters["destination"].(string)
    	port = strings.ToLower(port)
    	// Check if the port is covered
    	covered := "false"
    	if client != nil {
    		// A Spanner client exists, so access the database.
    		// See: https://pkg.go.dev/cloud.google.com/go/spanner#ReadOnlyTransaction.ReadRow
    		row, err := client.Single().ReadRow(ctx,
    			"Destinations",
    			spanner.Key{port},
    			[]string{"Covered"})
    		if err != nil {
    			if spanner.ErrCode(err) == codes.NotFound {
    				log.Printf("Port %s not found", port)
    			} else {
    				return webhookResponse{}, err
    			}
    		} else {
    			// A row was returned, so check the value
    			var c bool
    			err := row.Column(0, &c)
    			if err != nil {
    				return webhookResponse{}, err
    			}
    			if c {
    				covered = "true"
    			}
    		}
    	} else {
    		// No Spanner client exists, so use hardcoded list of ports.
    		coveredPorts := map[string]bool{
    			"anguilla": true,
    			"canada":   true,
    			"mexico":   true,
    		}
    		_, ok := coveredPorts[port]
    		if ok {
    			covered = "true"
    		}
    	}
    	// Create session parameters that are populated in the response.
    	// This example hard codes values, but a real system
    	// might look up this value in a database.
    	p := map[string]interface{}{
    		"port_is_covered": covered,
    	}
    	// Build and return the response.
    	response := webhookResponse{
    		SessionInfo: sessionInfo{
    			Parameters: p,
    		},
    	}
    	return response, nil
    }
    
    // internationalCoverage handles same-named tag.
    func internationalCoverage(ctx context.Context, request webhookRequest) (
    	webhookResponse, error) {
    	// Get the existing parameter values
    	destination := request.SessionInfo.Parameters["destination"].(string)
    	destination = strings.ToLower(destination)
    	// Hardcoded list of covered international monthly destinations
    	coveredMonthly := map[string]bool{
    		"anguilla":  true,
    		"australia": true,
    		"brazil":    true,
    		"canada":    true,
    		"chile":     true,
    		"england":   true,
    		"france":    true,
    		"india":     true,
    		"japan":     true,
    		"mexico":    true,
    		"singapore": true,
    	}
    	// Hardcoded list of covered international daily destinations
    	coveredDaily := map[string]bool{
    		"brazil":    true,
    		"canada":    true,
    		"chile":     true,
    		"england":   true,
    		"france":    true,
    		"india":     true,
    		"japan":     true,
    		"mexico":    true,
    		"singapore": true,
    	}
    	// Check coverage
    	coverage := "neither"
    	_, monthly := coveredMonthly[destination]
    	_, daily := coveredDaily[destination]
    	if monthly && daily {
    		coverage = "both"
    	} else if monthly {
    		coverage = "monthly_only"
    	} else if daily {
    		coverage = "daily_only"
    	}
    	// Create session parameters that are populated in the response.
    	// This example hard codes values, but a real system
    	// might look up this value in a database.
    	p := map[string]interface{}{
    		"coverage": coverage,
    	}
    	// Build and return the response.
    	response := webhookResponse{
    		SessionInfo: sessionInfo{
    			Parameters: p,
    		},
    	}
    	return response, nil
    }
    
    // cheapestPlan handles same-named tag.
    func cheapestPlan(ctx context.Context, request webhookRequest) (
    	webhookResponse, error) {
    	// Create session parameters that are populated in the response.
    	// This example hard codes values, but a real system
    	// might look up this value in a database.
    	p := map[string]interface{}{
    		"monthly_cost":   70,
    		"daily_cost":     100,
    		"suggested_plan": "monthly",
    	}
    	// Build and return the response.
    	response := webhookResponse{
    		SessionInfo: sessionInfo{
    			Parameters: p,
    		},
    	}
    	return response, nil
    }
    
    // Define a type for handler functions.
    type handlerFn func(ctx context.Context, request webhookRequest) (
    	webhookResponse, error)
    
    // Create a map from tag to handler function.
    var handlers map[string]handlerFn = map[string]handlerFn{
    	"detectCustomerAnomaly": detectCustomerAnomaly,
    	"validatePhoneLine":     validatePhoneLine,
    	"cruisePlanCoverage":    cruisePlanCoverage,
    	"internationalCoverage": internationalCoverage,
    	"cheapestPlan":          cheapestPlan,
    }
    
    // handleError handles internal errors.
    func handleError(w http.ResponseWriter, err error) {
    	log.Printf("ERROR: %v", err)
    	http.Error(w,
    		fmt.Sprintf("ERROR: %v", err),
    		http.StatusInternalServerError)
    }
    
    // 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)
    
    	// Get the tag from the request, and call the corresponding
    	// function that handles that tag.
    	tag := request.FulfillmentInfo.Tag
    	if fn, ok := handlers[tag]; ok {
    		response, err = fn(r.Context(), request)
    	} else {
    		err = fmt.Errorf("Unknown tag: %s", tag)
    	}
    	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
    	}
    }
    
    

  8. Haz clic en Implementar.

  9. Espera hasta que el indicador de estado muestre que la función se implementó de forma correcta. Mientras esperas, examina el código que acabas de implementar.

Configura el webhook para tu agente

Ahora que el webhook existe como un servicio, lo asociarás con tu agente. Si deseas configurar el webhook para tu agente, sigue estos pasos:

  1. Abre la consola de Dialogflow CX.
  2. Elige tu proyecto de Google Cloud.
  3. Selecciona el agente.
  4. Selecciona la pestaña Administrar.
  5. Haz clic en Webhooks.
  6. Haz clic en el webhook existente (creado por el agente compilado previamente).
  7. Actualiza la URL de webhook a la URL de webhook que guardaste cuando creaste la función.
  8. Configura el Subtipo como Estándar.
  9. Haz clic en Guardar.

Probar el agente

Tu agente ya está listo para intentarlo. Haz clic en el botón Probar agente para abrir el simulador. Intenta mantener la siguiente conversación con el agente:

Giro de conversación Agente
1 Hola ¡Hola! Soy el agente de telecomunicaciones virtuales. Puedo ayudarte a resolver preguntas relacionadas con la facturación y los planes, solucionar problemas y agregar planes de viajes y cruceros. ¿En qué puedo ayudarte hoy?
2 Quiero agregar un plan de cruceros Déjame revisar tu caso. ¿Cuál es el número de teléfono que tienes con nosotros?
3 123-123-1234 ¿Cuántos días dura su crucero?
4 7 La opción 1 incluye llamadas y mensajes de texto. Cuesta USD [XX] por día. Obtén más información aquí [URL]. La opción 2 incluye llamadas, mensajes de texto y datos. Cuesta USD [XX] por día. Obtén más información aquí [URL]. ¿Qué plan de cruceros quieres agregar?
5 Opción 1 Entendido Agregué ese plan a tu cuenta. Durante el crucero, es posible que también necesites cobertura terrestre. ¿En qué puerto detendrás?
6 México Y, ¿cuántos días pasas allí?
7 3 ¡Excelentes noticias! México tiene cobertura gratuita de tierra. ¡Listo! ¿Hay algo más en lo que pueda ayudarte?

El flujo Cruise Plan administró esta conversación en particular.

En el turno 6 de la conversación, proporcionaste "México" como tu puerto de destino. El puerto de destino y la cantidad de días se capturan como los parámetros de formulario destination y trip_duration de la página Collect Port. Explora tu agente para encontrar estas definiciones de parámetros.

Captura de pantalla de la página Recopilar puerto en la consola de Dialogflow

En la página Collect Port, hay una ruta de condición para que el formulario se complete: $page.params.status = "FINAL". Una vez que se proporcionan los dos parámetros de formulario, se llama a esta ruta. Esta ruta llama a tu webhook y le proporciona la etiqueta cruisePlanCoverage. Si examinas el código de webhook que se muestra más arriba, verás que esta etiqueta activa la misma función con nombre para que se la llame.

Esta función determina si el destino proporcionado está cubierto por el plan. La función verifica si las variables de entorno específicas están configuradas con información para conectarse a la base de datos. Si no se establecen estas variables de entorno, la función usa una lista codificada de destinos. En los próximos pasos, modificarás el entorno de la función para que recupere datos de una base de datos a fin de validar la cobertura del plan para los destinos.

Soluciona problemas

El código de webhook incluye instrucciones de registro. Si tienes problemas, intenta ver los registros de tu función.

Más información

Para obtener más información sobre los pasos anteriores, consulta lo siguiente: