El agente prediseñado que has creado en el paso anterior requiere un webhook. En este tutorial, se usan funciones de Cloud Run para alojar el webhook debido a su sencillez, pero hay muchas otras formas de alojar un servicio de webhook. En el ejemplo también se usa el lenguaje de programación Go, pero puedes usar cualquier lenguaje compatible con Cloud Run Functions.
Crear la función
Las funciones de Cloud Run se pueden crear con la consola de Google Cloud (consulta la documentación o abre la consola). Para crear una función en este tutorial, sigue estos pasos:
Es importante que tu agente de Dialogflow y la función estén en el mismo proyecto. Es la forma más sencilla de que Dialogflow tenga acceso seguro a tu función. Antes de crear la función, selecciona tu proyecto en la Google Cloud consola.
Abre la página de información general de Cloud Run Functions.
Haz clic en Crear función y define los siguientes campos:
- Entorno: 1.ª gen.
- Nombre de la función: tutorial-telecommunications-webhook
- Región: si has especificado una región para tu agente, usa la misma.
- Tipo de activador HTTP: HTTP
- URL: haz clic en el botón de copiar y guarda el valor. Necesitarás esta URL al configurar el webhook.
- Autenticación: requiere autenticación
- Requerir HTTPS: marcada
Haz clic en Guardar.
Haz clic en Siguiente (no necesitas ajustes especiales de tiempo de ejecución, compilación, conexiones ni seguridad).
Configura los siguientes campos:
- Tiempo de ejecución: selecciona el tiempo de ejecución de Go más reciente.
- Código fuente: editor insertado
- Punto de entrada: HandleWebhookRequest
Sustituye el código por el 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 } }
Haz clic en Desplegar.
Espera hasta que el indicador de estado muestre que la función se ha implementado correctamente. Mientras esperas, examina el código que acabas de implementar.
Configurar el webhook de tu agente
Ahora que el webhook existe como servicio, lo asociarás a tu agente. Para configurar el webhook de tu agente, sigue estos pasos:
- Abre la consola de Dialogflow CX.
- Elige tu proyecto de Google Cloud.
- Selecciona tu agente.
- Selecciona la pestaña Gestionar.
- Haz clic en Webhooks.
- Haz clic en el webhook que ya tienes (creado por el agente prediseñado).
- Actualiza la URL del webhook con la URL del webhook que guardaste al crear la función.
- En Subtipo, selecciona Estándar.
- Haz clic en Guardar.
Probar el agente
Tu agente ya está listo para probarse. Haz clic en el botón Agente de prueba para abrir el simulador. Intenta mantener la siguiente conversación con el agente:
Turno de conversación | Tú | Agente |
---|---|---|
1 | Hola | ¡Hola! Soy el agente virtual de telecomunicaciones. Puedo ayudarte a resolver dudas sobre la facturación y los planes, solucionar problemas y añadir planes de viaje y crucero. ¿En qué puedo ayudarte hoy? |
2 | Quiero añadir un plan de crucero | Déjame echar un vistazo. ¿Cuál es tu número de teléfono con nosotros? |
3 | 123-123-1234 | ¿Cuántos días dura el crucero? |
4 | 7 | La opción 1 incluye llamadas y mensajes de texto. Cuesta $[XX] al día. Consulta más información aquí: [URL]. La opción 2 incluye llamadas, mensajes de texto y datos. Cuesta $[XX] al día. Consulta más información aquí: [URL]. ¿Qué plan de crucero quieres añadir? |
5 | Opción 1 | Entendido. He añadido ese plan a tu cuenta. Durante el crucero, es posible que también necesites cobertura en tierra. ¿En qué puerto harás una parada? |
6 | México | ¿Cuántos días vas a pasar allí? |
7 | 3 | ¡Buenas noticias! México tiene cobertura gratuita en tierra. Y ya habrás acabado. ¿Puedo ayudarte en algo más? |
Esta conversación en concreto se ha gestionado mediante el flujo de Cruise Plan
.
En la conversación número 6, has indicado que tu puerto de destino es "México".
El puerto de destino y el número de días que se pasa allí se registran como los parámetros de formulario destination
y trip_duration
de la página Collect Port
.
Busca en tu agente las definiciones de estos parámetros.
En la página Collect Port
, hay una ruta de condición para completar el formulario:
$page.params.status = "FINAL"
.
Una vez que se han proporcionado los dos parámetros del formulario, se llama a esta ruta.
Esta ruta llama a tu webhook
y le proporciona la etiqueta cruisePlanCoverage
.
Si examina el código del webhook anterior, verá que esta etiqueta activa la llamada a la misma función con nombre.
Esta función determina si el destino proporcionado está cubierto por el plan. La función comprueba si se han definido variables de entorno específicas con información para conectarse a la base de datos. Si estas variables de entorno no se definen, la función usa una lista de destinos codificada. En los próximos pasos, cambiarás el entorno de la función para que recupere datos de una base de datos con el fin de validar la cobertura del plan para los destinos.
Solución de problemas
El código del webhook incluye instrucciones de registro. Si tienes problemas, prueba a ver los registros de tu función.
Más información
Para obtener más información sobre los pasos anteriores, consulta los siguientes artículos: