//Packagemainisthemainpackagepackagemainimport("log""net/http""os""text/template")vartemplates*template.Templatefuncinit(){templates=template.Must(template.New("").ParseGlob("templates/*"))}//indexHandlerhandlesthehomepage.funcindexHandler(whttp.ResponseWriter,r*http.Request){ifr.URL.Path!="/"{http.NotFound(w,r)return}iferr:=templates.ExecuteTemplate(w,"index.html",nil);err!=nil{log.Fatal(err)}}funcmain(){//Registerthehandlershttp.HandleFunc("/",indexHandler)port:=os.Getenv("PORT")ifport==""{port="8080"log.Printf("Defaulting to port %s",port)}log.Printf("Listening on port %s",port)iferr:=http.ListenAndServe(":"+port,nil);err!=nil{log.Fatal(err)}}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[[["\u003cp\u003eThis tutorial guides you through hosting a Dialogflow agent using App Engine, which is chosen for its ease of setup and scalability.\u003c/p\u003e\n"],["\u003cp\u003eThe process involves creating a Go application with specific file structures (\u003ccode\u003ego-app/\u003c/code\u003e, \u003ccode\u003etemplates/\u003c/code\u003e, \u003ccode\u003eindex.html\u003c/code\u003e, \u003ccode\u003eapp.yaml\u003c/code\u003e, \u003ccode\u003emain.go\u003c/code\u003e) and content to be hosted.\u003c/p\u003e\n"],["\u003cp\u003eDeployment of the web service is managed using the \u003ccode\u003egcloud\u003c/code\u003e tool, ensuring it's configured with the same project and region as your Dialogflow agent.\u003c/p\u003e\n"],["\u003cp\u003eDialogflow Messenger is used as the agent's user interface, and its embed code is integrated into the \u003ccode\u003eindex.html\u003c/code\u003e file to enable chat functionality.\u003c/p\u003e\n"],["\u003cp\u003eAfter deployment, it's important to clean up billable resources, such as the database instance, the function, and the app, to avoid incurring additional charges.\u003c/p\u003e\n"]]],[],null,["# Host your agent\n\nThe final step of this tutorial is to host your Dialogflow agent.\n[App Engine](/appengine/docs)\nis used for hosting,\nbecause it is simple to set up and scales well.\nThe\n[Dialogflow Messenger](/dialogflow/es/docs/integrations/dialogflow-messenger)\nintegration is used for an agent user interface.\n\nProject configuration\n---------------------\n\nIdeally, your Dialogflow agent and the App Engine instance\nare both in the same project.\nAlso, you must enable the Cloud Build API.\n\n1. Before creating the instance,\n select your project from the Google Cloud console.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n2. Enable the Cloud Build API for the project.\n\n [Enable the Cloud Build API](https://console.cloud.google.com/flows/enableapi?apiid=cloudbuild.googleapis.com)\n\nEnvironment setup\n-----------------\n\nYou need to install and configure some things\nin order to develop a Go application for App Engine.\nFollow any steps you haven't already completed at\n[Setting up your development environment](/appengine/docs/standard/go/setting-up-environment).\n\nCreate the web service code\n---------------------------\n\nThe example code for this tutorial is written in Go,\nbut you can use any language supported by App Engine.\nCreate the following file structure anywhere on your local machine:\n\n- `go-app/`: directory for your Go service.\n - `templates/`: directory for your Go HTML templates.\n - `index.html`: Go HTML template.\n - `app.yaml`: Your service's configuration settings.\n - `main.go`: Your application code.\n\nThe next three sections provide content for the three files.\n\n### Contents of the `index.html` file\n\nThis Go HTML template file contains the HTML for the homepage.\nPopulate this file with the following contents: \n\n```matlab\n\u003c!DOCTYPE html\u003e\n\u003chtml lang=\"en-US\"\u003e\n \u003chead\u003e\n \u003cmeta charset=\"utf-8\"\u003e\n \u003cmeta name=\"viewport\" content=\"width=device-width\"\u003e\n \u003ctitle\u003eTutorial agent\u003c/title\u003e\n \u003c/head\u003e\n \u003cbody\u003e\n \u003cp\u003eOpen the chat window in the bottom right corner.\u003c/p\u003e\n \u003c/body\u003e\n\u003c/html\u003e\n```\n\n### Contents of the `app.yaml` file\n\nThis is a configuration file which\nspecifies your service's runtime environment settings.\nYou can reference the\n[Go 1.12+ Runtime Environment document](/appengine/docs/standard/go/runtime)\nto see the list of supported Go versions.\nPopulate this file with the following contents: \n\n```actionscript-3\nruntime: go116 # or another supported version\n```\n\n### Contents of the `main.go` file\n\nThis file contains your application code.\nPopulate this file with the following contents: \n\n```python\n// Package main is the main package\npackage main\n\nimport (\n\t\"log\"\n\t\"net/http\"\n\t\"os\"\n\t\"text/template\"\n)\n\nvar templates *template.Template\n\nfunc init() {\n\ttemplates = template.Must(template.New(\"\").ParseGlob(\"templates/*\"))\n}\n\n// indexHandler handles the homepage.\nfunc indexHandler(w http.ResponseWriter, r *http.Request) {\n\tif r.URL.Path != \"/\" {\n\t\thttp.NotFound(w, r)\n\t\treturn\n\t}\n\tif err := templates.ExecuteTemplate(w, \"index.html\", nil); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n\nfunc main() {\n\t// Register the handlers\n\thttp.HandleFunc(\"/\", indexHandler)\n\n\tport := os.Getenv(\"PORT\")\n\tif port == \"\" {\n\t\tport = \"8080\"\n\t\tlog.Printf(\"Defaulting to port %s\", port)\n\t}\n\n\tlog.Printf(\"Listening on port %s\", port)\n\tif err := http.ListenAndServe(\":\"+port, nil); err != nil {\n\t\tlog.Fatal(err)\n\t}\n}\n```\n\nDeploy your web service\n-----------------------\n\nYour code is now ready to deploy.\nThese steps use the\n[gcloud tool](/sdk/gcloud)\nfor deployment:\n\n1. Ensure that gcloud is configured with the same project\n as your Dialogflow agent.\n To check the project:\n\n ```\n gcloud config get-value project\n ```\n\n To change the project: \n\n ```\n gcloud config set project YOUR_PROJECT\n ```\n2. In your `go-app` directory where your `app.yaml` file is located, deploy your\n web service to App Engine using the following command:\n\n ```\n gcloud app deploy\n ```\n\n The command will prompt you for a region.\n Be sure to select the same region as your Dialogflow agent.\n The command will output the `target url` value,\n which is the URL for your web service.\n3. To launch your browser and view your web service,\n you can open the target URL from the previous step,\n or run the following command:\n\n ```\n gcloud app browse\n ```\n\nSetup Dialogflow Messenger\n--------------------------\n\nTo set up and enable Dialogflow Messenger:\n\n1. Go to the [Dialogflow ES console](https://dialogflow.cloud.google.com).\n2. Click **Integrations** in the left sidebar menu.\n3. Click **Dialogflow Messenger**.\n4. A configuration dialog opens.\n5. Choose an [environment](/dialogflow/docs/agents-versions).\n6. Click **Enable**.\n7. Copy the embed code for pasting in your website.\n8. Click **Close**.\n\nEmbed the agent in your web service\n-----------------------------------\n\nPaste the embed code you copied above in your `index.html` file.\nThe `\u003cscript\u003e` and `\u003cdf-messenger\u003e` HTML elements\nshould be in the `\u003cbody\u003e` element of your page.\n\nDeploy your web service app again with gcloud.\nOnce deployed,\nyou can interact with your agent through the webpage\nby clicking the chat icon in the lower right corner.\n\nYou now have a fully deployed Dialogflow agent!\nTry having a conversation with the agent.\n\nCleanup\n-------\n\nWhile going through the steps of this tutorial,\n[you created billable resources](/dialogflow/es/docs/tutorials/deploy#cost).\nTo avoid incurring additional charges to your Google Cloud account:\n\n1. [Delete your database instance](/spanner/docs/create-manage-instances#delete-instance).\n2. Delete your function:\n\n ```\n gcloud functions delete tutorial-telecommunications-webhook\n ```\n3. [Disable your app](/appengine/docs/standard/go/console#disabling_an_application).\n\n4. [Delete your agent](/dialogflow/es/docs/agents-manage#delete)\n\nMore information\n----------------\n\nFor more information about the steps above, see:\n\n- [Building a Go App on App Engine](/appengine/docs/standard/go/building-app)\n- [Dialogflow Messenger](/dialogflow/es/docs/integrations/dialogflow-messenger)"]]