部署您的 BotDeploy your bot

適用于: SDK v4APPLIES TO: SDK v4

在本文中,我們將說明如何將基本 Bot 部署至 Azure。In this article we will show you how to deploy a basic bot to Azure. 我們將說明如何準備您的 Bot 以進行部署、將 Bot 部署至 Azure,以及在網路聊天中測試您的 Bot。We will explain how to prepare your bot for deployment, deploy your bot to Azure, and test your bot in Web Chat. 在依照步驟執行前閱讀本文很有用,您可完全了解部署 Bot 的相關事項。It would be useful to read this article before following the steps, so that you fully understand what is involved in deploying a bot.

重要

請確定您使用的是最新版的 Azure CLIMake sure you are using the latest version of the Azure CLI. 如果您使用 2.2.0 版以前的 Azure CLI,則您會遇到 CLI 命令淘汰的錯誤。If you are using an Azure CLI version older than 2.2.0, you will encounter errors of CLI commands deprecation. 此外,請不要混用 Azure CLI 部署 (如本文所示) 與 Azure 入口網站部署。Also, do not mix Azure CLI deployment shown in this article with Azure portal deployment.

必要條件Prerequisites

準備開始部署Prepare for deployment

如果您要部署 C# Bot,請確定其已內建發行模式If you are deploying a C# bot make sure that it has been built in Release mode. 在 Visual Studio 中,請確定 [解決方案組態] 已設為 [發行],並執行解決方案的全新重建,再繼續進行。In Visual Studio, make sure that the solution configuration is set to Release and perform a clean rebuild of the solution before continuing. 如果解決方案設定設定為 Debug,則部署可能會失敗。The deployment may fail if the solution configuration is set to Debug.

當您使用 Visual Studio 範本建立 bot 時,所產生的原始程式碼會包含 DeploymentTemplates 包含 ARM 範本的資料夾。When you create a bot using a Visual Studio template, the source code generated includes a DeploymentTemplates folder that contains ARM templates. 此處說明的部署程序會使用其中一個 ARM 範本,然後以 Azure CLI 在 Azure 中佈建 Bot 所需的資源。The deployment process documented here uses one of the ARM templates to provision required resources for the bot in Azure by using the Azure CLI.

在 Bot Framework SDK 4.3 版本中,我們已 不再 使用 .bot 檔案。With the release of Bot Framework SDK 4.3, we have deprecated the use of a .bot file. 相反地,我們會使用 appsettings.json 設定檔來管理 bot 資源。Instead, we use the appsettings.json configuration file to manage bot resources. 如需將設定從 bot 檔案遷移至設定檔的詳細資訊,請參閱 管理 bot 資源For information on migrating settings from the .bot file to a configuration file, see managing bot resources.

注意

VSIX封裝包含 .net core 2.1 和 .net core 3.1 版本的 c # 範本。The VSIX package includes both .NET Core 2.1 and .NET Core 3.1 versions of the C# templates. 在 Visual Studio 2019 中建立新的 Bot 時,您應該使用 .NET Core 3.1 範本。When creating new bots in Visual Studio 2019, you should use the .NET Core 3.1 templates. 目前的 Bot 範例會使用 .NET Core 3.1 範本。The current bot samples use .NET Core 3.1 templates. 您可以在 BotBuilder-Samples 存放庫的 4.7-archive 分支中,找到使用 .NET Core 2.1 範本的範例。You can find the samples that use .NET Core 2.1 templates in the 4.7-archive branch of the BotBuilder-Samples repository. 如需將 .NET Core 3.1 bot 部署至 Azure 的詳細資訊,請參閱如何將 您的 bot 部署至 azureFor information about deploying .NET Core 3.1 bots to Azure, see how to deploy your bot to Azure.

1.登入 Azure1. Login to Azure

在本機建立並測試 Bot 之後,您即可將其部署至 Azure。Once you've created and tested a bot locally, you can deploy it to Azure. 開啟命令提示字元以登入 Azure 入口網站。Open a command prompt to log in to the Azure portal.

az login

瀏覽器視窗會隨即開啟,以便您登入。A browser window will open, allowing you to sign in.

注意

如果您將 Bot 部署至非 Azure 雲端 (例如 US Gov),您必須在 az login 之前執行 az cloud set --name <name-of-cloud>,其中,<name-of-cloud> 是已註冊的雲端名稱,例如 AzureUSGovernmentIf you deploy your bot to a non-Azure cloud such as US Gov, you need to run az cloud set --name <name-of-cloud> before az login, where <name-of-cloud> is the name of a registered cloud, such as AzureUSGovernment. 如果您要返回公用雲端,可以執行 az cloud set --name AzureCloudIf you want to go back to public cloud, you can run az cloud set --name AzureCloud.

2.設定訂用帳戶2. Set the subscription

設定要使用的預設訂用帳戶。Set the default subscription to use.

az account set --subscription "<azure-subscription-id>"

如果您不確定要使用哪個訂用帳戶來部署 bot,可以使用命令來查看您帳戶的訂用帳戶清單 az account listIf you aren't sure which subscription to use for deploying the bot, you can view the list of subscriptions for your account by using az account list command.

3.建立應用程式註冊3. Create the application registration

在此步驟中,您會建立可允許下列動作的 Azure 應用程式註冊:In this step you will create an Azure application registration, which will allow:

  • 使用者可透過一組通道 (例如「網路聊天」**) 與 Bot 互動。The user to interact with the bot via a set of channels such as Web Chat.
  • 可使用「OAuth 連線設定」** 來驗證使用者,以及建立 Bot 用來代表使用者存取受保護資源的「權杖」**。The definition of OAuth Connection Settings to authenticate a user and to create a token used by the bot to access protected resources on behalf of the user.

3.1 建立 Azure 應用程式註冊3.1 Create the Azure application registration

若要建立 Azure 應用程式註冊,請執行下列命令:To create an Azure application registration, execute the following command:

az ad app create --display-name "displayName" --password "AtLeastSixteenCharacters_0" --available-to-other-tenants
選項Option 描述Description
display-namedisplay-name 應用程式的顯示名稱。The display name of the application. 此名稱會列在 Azure 入口網站的一般資源清單和其所屬的資源群組中。It is listed in the Azure portal in the general resources list and in the resource group it belongs.
密碼password 應用程式的密碼,也稱為用戶端密碼The password, also known as client secret, for the application. 這是您為此資源建立的密碼。This is a password you create for this resource. 密碼必須至少有 16 個字元長,至少包含 1 個大寫或小寫字母,以及至少包含 1 個特殊字元。It must be at least 16 characters long, contain at least 1 upper or lower case alphabetical character, and contain at least 1 special character.
available-to-other-tenantsavailable-to-other-tenants 指出您可以從任何 Azure AD 租用戶使用應用程式。Indicates that the application can be used from any Azure AD tenant. 設定此選項可讓您的 Bot 使用 Azure Bot 服務通道。Set this to enable your bot to work with the Azure Bot Service channels.

3.2 記錄 appId 和 appSecret 值3.2 Record the appId and appSecret values

在執行上一個步驟 (步驟 3.1) 中的命令之後,您必須記錄兩個值:You will need to record two values after executing the command in the previous step (step 3.1):

  • 您在上一個步驟中建立的 passwordThe password you create in the previous step.
  • 您可以在輸出 JSON 中找到的 appIdThe appId which you can find in the output JSON.

複製並儲存 appIdpassword 值。Copy and save the appId and password values. 您需要在 ARM 部署步驟中使用這些值,分別將值指派給 appIdappSecret 參數。You will need to use them in the ARM deployment step, to assign values to the appId and the appSecret parameters, respectively.

4.建立 Bot 應用程式服務4. Create the bot application service

建立 Bot 應用程式服務時,您可以在新的或現有的資源群組中部署您的 Bot,都是透過 Azure Resource Manager (ARM) 範本When creating the bot application service, you can deploy your bot in a new or in an existing resource group, both via the Azure Resource Manager (ARM) template. ARM 範本是一個 JSON 檔案,可以宣告方式定義一或多個 Azure 資源,並定義所部署資源之間的相依性。An ARM template is a JSON file that declaratively defines one or more Azure resources and that defines dependencies between the deployed resources. 請確定您有 Bot 專案 ARM 部署範本目錄 DeploymentTemplates 的正確路徑,您需要此路徑來將值指派給範本檔案。Make sure that you have the correct path to your bot project ARM deployment templates directory DeploymentTemplates, you need it to assign the value to the template file. 選擇最適合您的選項:Choose the option that works best for you:

重要

Python Bot 無法部署至包含 Windows 服務/Bot 的資源群組。Python bots cannot be deployed to a resource group that contains Windows services/bots. 您可以將多個 Python bot 部署至相同的資源群組,但您必須在另一個資源群組中建立其他服務, (LUIS、QnA 等 ) 。Multiple Python bots can be deployed to the same resource group, but you need to create other services (LUIS, QnA, etc.) in another resource group.

透過 ARM 範本部署 (使用 資源群組)Deploy via ARM template (with new Resource Group)

在此步驟中,您會建立 Bot 應用程式服務來設定 Bot 的部署階段。In this step, you create a bot application service which sets the deployment stage for the bot. 您可以使用 ARM 範本、新的服務方案和新的資源群組。You use an ARM template, a new service plan and a new resource group. 執行下列 Azure CLI 命令,從本機範本檔案在訂用帳戶範圍開始部署。Run the following Azure cli command to start a deployment at subscription scope from a local template file.

提示

使用 ARM 範本作為 的資源群組, template-with-new-rg.js開啟Use the ARM template for a new resource group, template-with-new-rg.json.

az deployment sub create --template-file "<path-to-template-with-new-rg.json" --location <region-location-name> --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" botSku=F0 newAppServicePlanName="<new-service-plan-name>" newWebAppName="<bot-app-service-name>" groupName="<new-group-name>" groupLocation="<region-location-name>" newAppServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"

注意

完成此步驟需要數分鐘的時間。This step can take a few minutes to complete.

選項Option 描述Description
NAMEname 部署名稱。The deployment name.
template-filetemplate-file ARM 範本的路徑。The path to the ARM template. 通常,Bot 專案資料夾 deploymentTemplates 中會提供 template-with-new-rg.json 檔案。Usually, the template-with-new-rg.json file is provided in the deploymentTemplates folder of the bot project. 這是現有範本檔案的路徑。This is a path to an existing template file. 其可以是絕對路徑,或目前目錄的相對路徑。It can be an absolute path, or relative to the current directory. 所有 Bot 範本都會產生 ARM 範本檔案。All bot templates generate ARM template files.
locationlocation 位置。Location. 值的來源:az account list-locationsValues from: az account list-locations. 您可以使用 az configure --defaults location=<location> 來設定預設位置。You can configure the default location using az configure --defaults location=<location>.
參數parameters 以「索引鍵=值」配對清單形式提供的部署參數。Deployment parameters, provided as a list of key=value pairs. 輸入下列參數值:Enter the following parameter values:
  • appId - 建立應用程式註冊步驟從 JSON 輸出所產生的 應用程式識別碼 值。appId - The app id value from the JSON output generated by the create the application registration step.
  • appSecret - 您在建立應用程式註冊步驟中提供的密碼。appSecret - The password you provided in the create the application registration step.
  • botId -要建立的 Bot 通道註冊資源名稱。botId - A name for the Bot Channels Registration resource to create. 此名稱必須是全域唯一的。It must be globally unique. 並且會用來作為不可變的 Bot 識別碼。It is used as the immutable bot ID. 也可用來作為預設的顯示名稱,而這是可變動的。It is also used as the default display name, which is mutable.
  • botSku - 定價層;可以是 F0 (免費) 或 S1 (標準)。botSku - The pricing tier; it can be F0 (Free) or S1 (Standard).
  • newAppServicePlanName - 新應用程式服務方案的名稱。newAppServicePlanName - The name of the new application service plan.
  • newWebAppName - Bot 應用程式服務的名稱。newWebAppName - A name for the bot application service.
  • groupName - 新資源群組的名稱。groupName - A name for the new resource group.
  • groupLocation - Azure 資源群組的位置。groupLocation - The location of the Azure resource group.
  • newAppServicePlanLocation - 應用程式服務方案的位置。newAppServicePlanLocation - The location of the application service plan.

透過 ARM 範本部署 (使用 現有 的資源群組)Deploy via ARM template (with existing Resource Group)

在此步驟中,您會建立 Bot 應用程式服務來設定 Bot 的部署階段。In this step, you create a bot application service which sets the deployment stage for the bot. 使用現有的資源群組時,您可以使用現有的應用程式服務方案或建立新的方案。When using an existing resource group, you can either use an existing app service plan or create a new one. 選擇最適合您的選項:Choose the option that works best for you:

注意

完成此步驟需要數分鐘的時間。This step can take a few minutes to complete.

選項 1:現有的 App Service 方案Option 1: Existing App Service Plan

在此案例中,我們將使用現有的 App Service 方案,但建立新的 Web 應用程式和 Bot 通道註冊。In this case, we are using an existing App Service Plan, but creating a new Web App and Bot Channels Registration.

以下命令會設定 Bot 的識別碼和顯示名稱。This command below sets the bot's ID and display name. botId 參數應該是全域唯一,而且會用來作為不可變的 Bot 識別碼。The botId parameter should be globally unique and is used as the immutable bot ID. Bot 的顯示名稱是可變動的。The bot's display name is mutable.

提示

使用 現有 資源群組的 ARM 範本, template-with-preexisting-rg.js開啟Use the ARM template for an existing resource group, template-with-preexisting-rg.json.

az deployment group create --resource-group "<name-of-resource-group>" --template-file "<path-to-template-with-preexisting-rg.json>" --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" newWebAppName="<bot-app-service-name>" existingAppServicePlan="<name-of-app-service-plan>" appServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"
選項 2:新的 App Service 方案Option 2: New App Service Plan

在此案例中,我們會建立 App Service 方案、Web 應用程式和 Bot 通道註冊。In this case, we are creating App Service Plan, Web App, and Bot Channels Registration.

提示

使用 現有 資源群組的 ARM 範本, template-with-preexisting-rg.js開啟Use the ARM template for an existing resource group, template-with-preexisting-rg.json.

az deployment group create --resource-group "<name-of-resource-group>" --template-file "<path-to-template-with-preexisting-rg.json>" --parameters appId="<app-id-from-previous-step>" appSecret="<password-from-previous-step>" botId="<id or bot-app-service-name>" newWebAppName="<bot-app-service-name>" newAppServicePlanName="<name-of-app-service-plan>" newAppServicePlanLocation="<region-location-name>" --name "<bot-app-service-name>"
選項Option 描述Description
NAMEname 部署名稱。The deployment name.
resource-groupresource-group Azure 資源群組的名稱。Name of the azure resource group.
template-filetemplate-file ARM 範本的路徑。The path to the ARM template. 通常,專案資料夾 deploymentTemplates 中會提供 template-with-preexisting-rg.json 檔案。Usually, the template-with-preexisting-rg.json file is provided in the deploymentTemplates folder of the project. 這是現有範本檔案的路徑。This is a path to an existing template file. 其可以是絕對路徑,或目前目錄的相對路徑。It can be an absolute path, or relative to the current directory. 所有 Bot 範本都會產生 ARM 範本檔案。All bot templates generate ARM template files.
locationlocation 位置。Location. 值的來源:az account list-locationsValues from: az account list-locations. 您可以使用 az configure --defaults location=<location> 來設定預設位置。You can configure the default location using az configure --defaults location=<location>.
參數parameters 以「索引鍵=值」配對清單形式提供的部署參數。Deployment parameters, provided as a list of key=value pairs. 輸入下列參數值:Enter the following parameter values:
  • appId - 由 3.1 建立應用程式註冊步驟所產生的 appId 值。appId - The appId value generated by the 3.1 create the application registration step.
  • appSecret - 您在 3.1 建立應用程式註冊步驟中提供的密碼。appSecret - The password you provided in the the 3.1 create the application registration step.
  • botId -要建立的 Bot 通道註冊資源名稱。botId - A name for the Bot Channels Registration resource to create. 此名稱必須是全域唯一的。It must be globally unique. 並且會用來作為不可變的 Bot 識別碼。It is used as the immutable bot ID. 也可用來作為預設的顯示名稱,而這是可變動的。It is also used as the default display name, which is mutable.
  • newWebAppName - Bot 應用程式服務的名稱。newWebAppName - A name for the bot application service.
  • newAppServicePlanName - 要建立的應用程式服務方案資源名稱。newAppServicePlanName - A name for the application service plan resource to create.
  • appServicePlanLocation - 應用程式服務方案的位置。appServicePlanLocation - The location of the application service plan.

5.準備您的程式碼以進行部署5. Prepare your code for deployment

5.1 取出或建立必要的檔案5.1 Retrieve or create necessary files

您必須先備妥專案檔,才能部署 Bot。You need to prepare your project files before you can deploy your bot.

確定您是在 bot 的專案資料夾中。Make sure you're in your bot's project folder. 然後準備要部署的 bot 程式碼。Then prepare your bot code for deployment.

az bot prepare-deploy --lang Csharp --code-dir "." --proj-file-path "MyBot.csproj"

您必須提供與 --code-dir 對應的 .csproj 檔案路徑。You must provide the path to the .csproj file relative to --code-dir. 這可以透過 --proj-file-path 引數來執行。This can be performed via the --proj-file-path argument. 此命令會將 --code-dir 和 --proj-file-path 解析為 "./MyBot.csproj"。The command would resolve --code-dir and --proj-file-path to "./MyBot.csproj".

此命令會在您的 Bot 專案資料夾中產生 .deployment 檔案。This command generates a .deployment file in your bot project folder.

5.2 手動壓縮程式碼目錄5.2 Zip up the code directory manually

使用未設定的 zip 部署 API來部署您 Bot 的程式碼時,Web 應用程式/Kudu 的行為會如下所示:When using the non-configured zip deploy API to deploy your bot's code, Web App/Kudu's behavior is as follows:

根據預設,Kudu 會假設來自 zip 檔案的部署都已可供執行,而且部署期間不需要額外的建置步驟,例如 npm install 或 dotnet restore/dotnet publish。Kudu assumes by default that deployments from zip files are ready to run and do not require additional build steps during deployment, such as npm install or dotnet restore/dotnet publish.

務必在要部署的 zip 檔案中包含建置程式碼和所有必要相依性,否則您的 Bot 不會如預期般運作。It is important to include your built code with all necessary dependencies in the zip file being deployed, otherwise your bot will not work as intended.

重要

壓縮專案檔之前,請確定您位在 Bot 的專案資料夾中。Before zipping your project files, make sure that you are in the bot's project folder.

  • 針對 C# Bot,這是具有 .csproj 檔案的資料夾。For C# bots, it is the folder that has the .csproj file.
  • 針對 JavaScript Bot,這是具有 app.js 或 index.js 檔案的資料夾。For JavaScript bots, it is the folder that has the app.js or index.js file.
  • 針對 TypeScript Bot,這是包含 src 資料夾的資料夾( bot.ts 和 index.ts 檔案的所在位置)。For TypeScript bots, it is the folder that includes the src folder (where the bot.ts and index.ts files are).
  • 針對 Python Bot,這是具有 app.py 檔案的資料夾。For Python bots, it is the folder that has the app.py file.

在專案資料夾 ,請務必先選取所有檔案和資料夾,再執行命令以建立 ZIP 檔案。Within the project folder, make sure you select all the files and folders before running the command to create the zip file. 這會在專案資料夾內建立單一 ZIP 檔案。This will create a single zip file within the project folder. 如果您的根資料夾位置不正確,Bot 將無法在 Azure 入口網站中執行If your root folder location is incorrect, the bot will fail to run in the Azure portal.

將程式碼部署至 AzureDeploy code to Azure

此時,我們已準備好將程式碼部署至 Azure Web 應用程式。At this point we are ready to deploy the code to the Azure Web App. 從命令列執行下列命令,即可對 Web 應用程式使用 kudu zip 推送部署來執行部署。Run the following command from the command line to perform deployment using the kudu zip push deployment for a web app.

az webapp deployment source config-zip --resource-group "<resource-group-name>" --name "<name-of-web-app>" --src "<project-zip-path>"
選項Option 描述Description
resource-groupresource-group 包含 Bot 的 Azure 資源群組的名稱。The name of the Azure resource group that contains your bot. (這會是您為 Bot 建立應用程式註冊時所使用或建立的資源群組。)(This will be the resource group you used or created when creating the app registration for your bot.)
NAMEname 您稍早使用的 Web 應用程式名稱。Name of the Web App you used earlier.
srcsrc 您所建立的 zip 專案檔案路徑。The path to the zipped project file you created.

注意

完成此步驟需要數分鐘的時間。This step can take a few minutes to complete. 此外,部署完成後可能需要幾分鐘的時間,才能對 Bot 進行測試。Also it can take a few more minutes between when the deployment finishes and when your bot is available to test.

在網路聊天中測試Test in Web Chat

  1. 在瀏覽器中導覽至 Azure 入口網站In your browser, navigate to the Azure portal.
  2. 在左側面板中,按一下 [資源群組] 。In the left panel, click Resource groups.
  3. 在右側面板中,搜尋您的群組。In the right panel, search for your group.
  4. 按一下群組名稱。Click on your group name.
  5. 按一下 Bot 通道註冊的連結。Click the link of your Bot Channels Registration.
  6. 在 [Bot 通道註冊] 面板中,按一下 [在網路聊天中測試] 。In the Bot Channels Registration panel, click Test in Web Chat. 或者,在右側面板中按一下 [測試] 方塊。Alternatively, in the right panel, click the Test box.

如需 Bot 通道註冊的詳細資訊,請參閱向 Bot 服務註冊聊天機器人For more information about bot channels registration, see Register a bot with Bot Service.

其他資訊Additional information

將 Bot 部署至 Azure 時,需支付您所使用的服務。Deploying your bot to Azure will involve paying for the services you use. 計費與成本管理一文協助您了解 Azure 計費方式、監視使用量和成本,以及管理帳戶和訂用帳戶。The billing and cost management article helps you understand Azure billing, monitor usage and costs, and manage your account and subscriptions. 另請參閱 Azure Command-Line Interface (CLI) 檔Azure CLI 版本資訊。See also Azure Command-Line Interface (CLI) documentation and Azure CLI release notes.

後續步驟Next steps