category
本文介绍了如何使用Azure CLI为您的机器人创建资源,准备部署机器人,并将机器人部署到Azure。
本文假设您已经准备好部署机器人。有关如何创建简单echobot的信息,请参阅使用bot Framework SDK创建bot。您还可以使用Bot Framework samples存储库中提供的示例之一。
小贴士
本文为您的机器人创建了一个Azure机器人资源。使用Web App机器人资源或机器人频道注册资源的现有机器人将继续工作,但您无法创建使用这些资源类型的新机器人。
注:
Bot Framework JavaScript、C#和Python SDK将继续得到支持,但Java SDK将退役,最终的长期支持将于2023年11月结束。
使用Java SDK构建的现有机器人将继续运行。
对于新的机器人构建,可以考虑使用Power Virtual Agents,并阅读有关选择正确的聊天机器人解决方案的信息。
有关更多信息,请参阅机器人构建的未来。
先决条件
对于Java机器人,请安装Maven。- 此过程使用两个Azure资源管理器模板(ARM模板)为您的机器人创建资源。
如果您没有当前的模板,请在您的机器人项目中创建deploymentTemplates文件夹的副本:C#、JavaScript、Python或Java。
要使用Azure CLI配置和发布机器人,您需要:
- 具有活动订阅的Azure帐户。创建一个免费帐户。
- Azure CLI的安装。
对于您的编程语言,请使用以下版本的Azure CLI。某些步骤不适用于较新版本的CLI。
Language | CLI version |
---|---|
C# and JavaScript | 2.39.0 or later |
Python | 2.55.0 or later |
Java | 2.29.2 |
注:
如果你的机器人使用额外的资源,如存储服务或语言服务,这些资源需要单独部署。
规划您的部署
在开始之前,做出这些决定。
Decision | Notes |
---|---|
How you'll manage the identities of your bot resources in Azure | You can use a user-assigned managed identity, a single-tenant app registration, or a mutli-tenant app registration. For more information, see Create an identity resource. |
In which resource group or resource groups you'll create your bot resources | Until you're familiar with this process, we recommend using one resource group. For more information, see Manage Azure resources. |
Whether your bot will be regional or global | For information about regional bots, see Regionalization in Azure AI Bot Service. |
您的机器人身份可以在Azure中通过几种不同的方式进行管理。
- 作为用户分配的托管身份,这样你就不需要自己管理机器人的凭据。
- 作为单租户应用程序。
- 作为一个多租户应用程序。
C#、JavaScript和Python的Bot Framework SDK中添加了对用户分配的托管身份和单租户应用程序类型的支持。其他语言或Bot Framework Composer、Bot Framework Emulator或ngrok不支持这些应用程序类型。
App type | Support |
---|---|
User-assigned managed identity | Azure AI Bot Service and the C#, JavaScript, and Python SDKs |
Single-tenant | Azure AI Bot Service and the C#, JavaScript, and Python SDKs |
Multi-tenant | Azure AI Bot Service, all Bot Framework SDK languages, Composer, the Emulator, and ngrok |
重要事项Python机器人不能部署到包含Windows服务或机器人的资源组中。但是,可以将多个Python机器人部署到同一资源组。在不同的资源组中创建其他服务,如Azure AI服务。
Azure资源
在部署机器人之前,您需要创建(或配置)它所需的Azure资源。对于某些步骤,您可以使用现有资源或创建新资源。
您可能会发现,提前决定要创建的新资源的名称和要使用的现有资源的名称很有帮助。你的机器人将使用这些类型的资源。
- 您将用于配置、发布和管理机器人的Azure订阅
- 一个或多个资源组
- 用户分配的托管身份或Microsoft Entra ID应用程序注册
- 应用服务计划资源
- 应用服务资源
- Azure Bot资源
跨资源使用的信息
当您在Azure中创建资源时,Azure将生成或请求您在后续步骤中需要的ID、密码和其他信息。下表列出了您需要记录的资源名称以外的信息,它是在哪个步骤生成的,以及在哪些步骤中使用的。
注意安全
这些ID和密码中的许多都是敏感信息。有关常见安全准则的信息,请参阅Bot Framework安全准则。
用户分配的托管身份
单租户
多用户
登录并选择订阅
打开命令窗口。
登录到Azure。
Azure命令行界面
az login
浏览器窗口将打开。完成登录过程。
成功后,该命令将输出您的帐户有权访问的订阅列表。
要设置要使用的订阅,请运行:
Azure命令行界面
az account set --subscription "<subscription>"
对于<subscription>,请使用要使用的订阅的ID或名称。
如果要创建用户分配的托管身份或单租户机器人,请记录订阅的tenantId。您将在以下步骤中使用租户ID。
小贴士
如果您需要在非公共云中工作,请参阅使用Azure CLI的Azure云管理。
创建资源组
如果您还没有合适的资源组,请使用az group create命令创建所需的新资源组。
Azure命令行界面
az group create --name "<group>" --location "<region>"
选项说明
name要创建的资源组的名称。
location创建资源组的区域。
有关更多信息,请参阅如何使用Azure CLI管理Azure资源组。
创建身份资源
用户分配的托管身份
-
To create a user-assigned managed identity, use the
az identity create
command. On success, the command generates JSON output.Azure CLIaz identity create --resource-group "<group>" --name "<identity>"
Option Description resource-group The name of the resource group in which to create the identity. name The name of the identity resource to create. For more information, see the az identity reference.
-
Record values you'll need in later steps.
- The resource group name for the identity resource
- The name of the identity resource
- The
clientId
from the command output
单租户
Use the following commands to create your app registration and set its password. On success, these commands generate JSON output.
-
Use the
az ad app create
command to create an Microsoft Entra ID app registration. This command generates an app ID that you'll use in the next step.Azure CLIaz ad app create --display-name "<app-registration-display-name>" --sign-in-audience "AzureADMyOrg"
Option Description display-name The display name for your app registration. sign-in-audience The supported Microsoft accounts for the app. Use AzureADMyOrg
for a single tenant app. -
Use the
az ad app credential reset
command to generate a new password for your app registration.Azure CLIaz ad app credential reset --id "<appId>"
-
Record values you'll need in later steps: the app ID and password from the command output.
For more information about az ad app
, see the command reference. For more information about the sign-in-audience
parameter, see sigInAudience values.
多用户
C#/JavaScript/Python
For Azure CLI 2.39.0 or later, use the following commands to create your app registration and set its password. On success, these commands generate JSON output.
-
Use the
az ad app create
command to create an Microsoft Entra ID app registration. This command generates an app ID that you'll need in later steps.Azure CLIaz ad app create --display-name "<app-registration-display-name>" --sign-in-audience "AzureADandPersonalMicrosoftAccount"
Option Description display-name The display name for your app registration. sign-in-audience The supported Microsoft accounts for the app. Use AzureADandPersonalMicrosoftAccount
for a multi-tenant app. -
Use the
az ad app credential reset
command to generate a new password for your app registration.Azure CLIaz ad app credential reset --id "<appId>"
-
Record values you'll need in later steps: the app ID and password from the command output.
For more information about az ad app
, see the command reference. For more information about the sign-in-audience
parameter, see sigInAudience values.
Java
For Azure CLI 2.36.0 or earlier, use the following command to create your app registration and set its password. On success, this command generates JSON output.
-
Use the
az ad app create
command to create an Microsoft Entra ID app registration.Azure CLIaz ad app create --display-name "<name>" --password "<password>" --available-to-other-tenants
Option Description display-name The display name for the app registration. password The password, or client secret, for the application. It must be at least 16 characters long and contain at least one upper-case or lower-case alphabetical character, at least one numeric character, and at least one special character. available-to-other-tenants Include this flag to create a multi-tenant bot. It allows the application to be accessible from any Microsoft Entra ID tenant. -
Record values you'll need in later steps.
- The password you used
- The app ID from the command output
使用ARM模板创建资源
为您的机器人创建应用服务和Azure机器人资源。这两个步骤都使用ARM模板和az部署组Create Azure CLI命令来创建资源。
为您的机器人创建应用服务资源。应用服务可以在新的或现有的应用服务计划中。
- 有关详细步骤,请参阅使用Azure CLI创建应用服务。
为您的机器人创建Azure机器人资源。
- 有关详细步骤,请参阅使用Azure CLI创建或更新Azure Bot。
重要事项
您可以按任何顺序执行这些步骤。但是,如果您先创建Azure Bot,则需要在创建应用服务资源后更新其消息传递端点。
更新项目配置设置
机器人身份信息
按照以下步骤将身份信息添加到机器人的配置文件中。该文件因您用于创建机器人的编程语言而异。
重要事项
Bot Framework SDK的Java版本仅支持多租户机器人。C#、JavaScript和Python版本支持用于管理机器人身份的所有三种应用程序类型。
Language | File name | Notes |
---|---|---|
C# | appsettings.json | Supports all three application types for managing your bot's identity. |
JavaScript | .env | Supports all three application types for managing your bot's identity. |
Java | application.properties | Only supports multi-tenant bots. |
Python | config.py | Supports all three application types for managing your bot's identity. |
您需要添加的身份信息取决于机器人的应用程序类型。在配置文件中提供以下值。
用户分配的托管身份
Available for C#, JavaScript, and Python bots.
Property | Value |
---|---|
MicrosoftAppType |
UserAssignedMSI |
MicrosoftAppId |
The client ID of the user-assigned managed identity. |
MicrosoftAppPassword |
Not applicable. Leave this blank for a user-assigned managed identity bot. |
MicrosoftAppTenantId |
The tenant ID of the user-assigned managed identity. |
单租户
Available for C#, JavaScript, and Python bots.
Property | Value |
---|---|
MicrosoftAppType |
SingleTenant |
MicrosoftAppId |
The bot's app ID. |
MicrosoftAppPassword |
The bot's app password. |
MicrosoftAppTenantId |
The bot's app tenant ID. |
多用户
适用于所有编程语言的机器人:C#、JavaScript、Java和Python。
Available for bots in all programming languages: C#, JavaScript, Java, and Python.
Property | Value |
---|---|
MicrosoftAppType |
MultiTenant |
MicrosoftAppId |
The bot's app ID. |
MicrosoftAppPassword |
The bot's app password. |
MicrosoftAppTenantId |
Not applicable. Leave this blank for a multi-tenant bot. |
准备项目文件
在部署机器人之前准备好项目文件。
C#
-
Switch to your project's root folder. For C#, the root is the folder that contains the .csproj file.
-
Do a clean rebuild in release mode.
-
If you haven't done so before, run
az bot prepare-deploy
to add required files to the root of your local source code directory. This command generates a.deployment
file in your bot project folder.Azure CLIaz bot prepare-deploy --lang Csharp --code-dir "." --proj-file-path "<my-cs-proj>"
Option Description lang The language or runtime of the bot. Use Csharp
.code-dir The directory to place the generated deployment files in. Use your project's root folder. Default is the current directory. proj-file-path The path to the .csproj file for your bot, relative to the code-dir
option. -
Within your project's root folder, create a zip file that contains all files and subfolders.
JavaScript
-
Switch to your project's root folder.
- For JavaScript, the root is the folder that contains the app.js or index.js file.
- For TypeScript, the root is the folder that contains the src folder (where the bot.ts and index.ts files are).
-
Run
npm install
. -
If you haven't done so before, run
az bot prepare-deploy
to add required files to the root of your local source code directory. This command generates aweb.config
file in your project folder. Azure App Services requires each Node.js bot to include a web.config file in its project root folder.Azure CLIaz bot prepare-deploy --lang <language> --code-dir "."
Option Description lang The language or runtime of the bot. Use Javascript
orTypescript
.code-dir The directory to place the generated deployment files in. Use your project's root folder. Default is the current directory. -
Within your project's root folder, create a zip file that contains all files and subfolders.
Java
-
Switch to your project's root folder.
-
In the project directory, run the following command from the command line:
Consolemvn clean package
Python
Tip
For Python bots, dependency installation is performed on the server. The ARM templates require your dependencies to be listed in a
requirements.txt
file.
- If you're using a dependency and package manager:
- Convert your dependencies list to a
requirements.txt
file - Add
requirements.txt
to the folder that containsapp.py
.
- Convert your dependencies list to a
- Within your project's root folder, create a zip file that contains all files and subfolders.
将你的机器人发布到Azure
此时,您已经准备好将机器人的代码部署到您的应用服务资源中。
注:
此步骤可能需要几分钟才能完成。此外,从部署完成到您的机器人可供测试之间,可能还需要几分钟的时间。
C#/JavaScript/Python
Run the az webapp deploy
command from the command line to perform deployment using the Kudu zip push deployment for your app service (web app).
Option | Description |
---|---|
resource-group | The name of the Azure resource group that contains your bot. |
name | Name of the app service you used earlier. |
src | The absolute or relative path to the zipped project file you created. |
Tip
By default, this command deploys to the production slot. Use the optional
--slot
parameter to specify a different slot. For more information, see theaz webapp deploy
command reference documentation.
Java
In the project directory, run the following command from the command line.
mvn azure-webapp:deploy -Dgroupname="<resource-group-name>" -Dbotname="<name-of-web-app>"
Option | Description |
---|---|
Dgroupname | The name of the Azure resource group that contains your bot. |
Dbotname | Name of the app service you used earlier. |
网络聊天中的测试
在浏览器中,导航到Azure门户。
转到您的机器人资源。
打开“网络聊天中的测试”窗格。
与您部署的机器人进行交互。
有关机器人注册的更多信息,请参阅向机器人服务注册机器人。
清理资源
如果您不打算发布此应用程序,请按照以下步骤删除关联的资源:
在Azure门户中,打开机器人的资源组。
选择“删除资源组”以删除该组及其包含的所有资源。
在确认窗格中输入资源组名称,然后选择删除。
如果您创建了单租户或多租户应用程序:
转到Microsoft Entra ID刀片。
找到您用于机器人的应用程序注册,并将其删除。
额外资源
有关用于托管机器人的Azure应用程序和资源的更多信息,请参阅这些文章。
Subject | Article |
---|---|
Azure CLI | What is the Azure CLI? |
Azure subscription management | How to manage Azure subscriptions with the Azure CLI |
Azure regions | Regions and availability zones |
Resource groups and resource management | Manage Azure resources |
Managed identities | What are managed identities for Azure resources? |
Single-tenant and multi-tenant apps | Tenancy in Microsoft Entra ID |
Web applications | App Service |
Compute resources for web applications | App Service plans |
Azure Resource Manager templates (ARM templates) | What are ARM templates? and How to use Azure Resource Manager (ARM) deployment templates with Azure CLI |
Azure billing | Billing and cost management |
Kudu文件
web应用程序部署命令使用Kudu部署C#、JavaScript和Python机器人。使用未配置的zip部署API部署机器人程序代码时,行为如下:
Kudu默认情况下假设.zip文件中的部署已准备好运行,并且在部署过程中不需要额外的构建步骤,例如npm install或dotnet restore/dotnet publish。
在部署的zip文件中包含您的构建代码以及所有必要的依赖关系非常重要;否则,你的机器人将无法按预期工作。有关更多信息,请参阅有关如何将文件部署到应用服务的Azure文档。
下一步
- 登录 发表评论
- 2 次浏览
Tags
最新内容
- 1 day 19 hours ago
- 1 day 19 hours ago
- 1 day 19 hours ago
- 1 day 19 hours ago
- 2 days 20 hours ago
- 2 days 20 hours ago
- 2 days 20 hours ago
- 2 days 20 hours ago
- 2 days 20 hours ago
- 2 days 20 hours ago