הסוכן המובנה מראש שיצרתם בשלב הקודם לא יכול לספק נתונים דינמיים כמו יתרות בחשבון, כי הכול מוטמע בסוכן. בשלב הזה של המדריך, תיצרו webhook שיכול לספק נתונים דינמיים לסוכן. במדריך הזה נשתמש בפונקציות Cloud Run כדי לארח את ה-webhook, כי הן פשוטות. אבל יש עוד הרבה דרכים לארח שירות webhook. בדוגמה נעשה שימוש בשפת התכנות Go, אבל אפשר להשתמש בכל שפה שנתמכת על ידי פונקציות Cloud Run.
יצירת הפונקציה
אפשר ליצור פונקציות Cloud Run באמצעות מסוף Google Cloud (למשאבי העזרה, לפתיחת המסוף). כדי ליצור פונקציה למדריך הזה:
חשוב שהסוכן של Dialogflow והפונקציה יהיו שייכים לאותו פרויקט. זו הדרך הקלה ביותר להעניק ל-Dialogflow גישה מאובטחת לפונקציה. לפני שיוצרים את הפונקציה, צריך לבחור את הפרויקט במסוף Google Cloud .
פותחים את דף הסקירה הכללית של Cloud Run functions.
לוחצים על Create Function (יצירת פונקציה) ומגדירים את השדות הבאים:
- סביבה: דור ראשון
- שם הפונקציה: tutorial-banking-webhook
- Region: אם ציינתם אזור לסוכן, צריך להשתמש באותו אזור.
- HTTP Trigger type: HTTP
- כתובת URL: לוחצים על לחצן ההעתקה ושומרים את הערך. תצטרכו את כתובת ה-URL הזו כשמגדירים את ה-webhook.
- אימות: נדרש אימות
- נדרש HTTPS: מסומן
לוחצים על Save.
לוחצים על הבא (אין צורך בהגדרות מיוחדות של זמן ריצה, build, חיבורים או אבטחה).
מגדירים את השדות הבאים:
- Runtime: בוחרים את זמן הריצה האחרון של Go.
- קוד מקור: עורך מוטבע
- נקודת כניסה: HandleWebhookRequest
מחליפים את הקוד בקוד הבא:
package estwh 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 queryResult struct { Action string `json:"action"` Parameters map[string]any `json:"parameters"` } 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"` } // accountBalanceCheck handles the similar named action func accountBalanceCheck(ctx context.Context, request webhookRequest) ( webhookResponse, error) { account := request.QueryResult.Parameters["account"].(string) account = strings.ToLower(account) var table string if account == "savings account" { table = "Savings" } else { table = "Checking" } s := "Your balance is $0" 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, table, spanner.Key{1}, // The account ID []string{"Balance"}) if err != nil { if spanner.ErrCode(err) == codes.NotFound { log.Printf("Account %d not found", 1) } else { return webhookResponse{}, err } } else { // A row was returned, so check the value var balance int64 err := row.Column(0, &balance) if err != nil { return webhookResponse{}, err } s = fmt.Sprintf("Your balance is $%.2f", float64(balance)/100.0) } } response := webhookResponse{ FulfillmentMessages: []message{ { Text: text{ Text: []string{s}, }, }, }, } return response, nil } // Define a type for handler functions. type handlerFn func(ctx context.Context, request webhookRequest) ( webhookResponse, error) // Create a map from action to handler function. var handlers map[string]handlerFn = map[string]handlerFn{ "account.balance.check": accountBalanceCheck, } // 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 action from the request, and call the corresponding // function that handles that action. action := request.QueryResult.Action if fn, ok := handlers[action]; ok { response, err = fn(r.Context(), request) } else { err = fmt.Errorf("Unknown action: %s", action) } 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 } }
לוחצים על פריסה.
מחכים עד שאינדיקטור הסטטוס מראה שהפונקציה נפרסה בהצלחה. בזמן ההמתנה, כדאי לבדוק את הקוד שפרסתם.
הגדרת ה-webhook של הסוכן
עכשיו, כשה-webhook קיים כשירות, צריך לשייך אותו לסוכן. הפעולה הזו מתבצעת באמצעות מילוי הזמנות. כדי להפעיל ולנהל את תהליך השלמת ההזמנה של הסוכן:
- עוברים אל מסוף Dialogflow ES.
- בוחרים את הסוכן המובנה שיצרתם.
- בתפריט הצדדי הימני, לוחצים על השלמת הזמנות.
- מעבירים את המתג בשדה Webhook למצב Enabled (מופעל).
- מזינים את כתובת ה-URL שהעתקתם למעלה. משאירים את כל שאר השדות ריקים.
- לוחצים על שמירה בתחתית הדף.

עכשיו, אחרי שהפעלתם את התכונה 'השלמת בקשות' לסוכן, אתם צריכים להפעיל אותה גם לישות:
- בתפריט הצדדי הימני, בוחרים באפשרות כוונות.
- בוחרים את כוונת המשתמש account.balance.check.
- גוללים למטה לקטע Fulfillment (מימוש).
- מפעילים את האפשרות הפעלת קריאה ל-webhook עבור הכוונה הזו.
- לוחצים על Save.
התנסות בסוכן
הסוכן מוכן עכשיו לשימוש. לוחצים על הלחצן Test Agent (בדיקת הסוכן) כדי לפתוח את הסימולטור. נסו לנהל את השיחה הבאה עם הסוכן:
| תור בשיחה | את/ה | סוכן |
|---|---|---|
| 1 | שלום | שלום, תודה שבחרת ב-ACME Bank. |
| 2 | אני רוצה לדעת מה היתרה בחשבון שלי | מהי היתרה שאתה רוצה לדעת: בחשבון החיסכון או בחשבון העובר ושב? |
| 3 | עובר ושב | היתרה האחרונה שלך: 0.00 $ |
בפנייה לשיחה מספר 3, ציינת את סוג החשבון 'בדיקה'. ל-Intent account.balance.check יש פרמטר בשם account. הפרמטר הזה מוגדר לערך 'בדיקה' בשיחה הזו. לאובייקט ה-Intent יש גם ערך פעולה account.balance.check. שירות ה-webhook נקרא, והוא מקבל את ערכי הפרמטר והפעולה.
אם בוחנים את קוד ה-webhook שלמעלה, אפשר לראות שהפעולה הזו מפעילה פונקציה בעלת שם דומה. הפונקציה קובעת את היתרה בחשבון. הפונקציה בודקת אם משתני סביבה ספציפיים מוגדרים עם מידע להתחברות למסד הנתונים. אם משתני הסביבה האלה לא מוגדרים, הפונקציה משתמשת ביתרת חשבון שמוגדרת בתוך הקוד. בשלבים הבאים, תשנו את הסביבה של הפונקציה כך שהיא תאחזר נתונים ממסד נתונים.
פתרון בעיות
קוד ה-webhook כולל הצהרות רישום ביומן. אם נתקלתם בבעיות, נסו לצפות ביומנים של הפונקציה.
מידע נוסף
מידע נוסף על השלבים שלמעלה: