关于 azure-mgmt-botservice-dotnet
azure-mgmt-botservice-dotnet 是通过 Azure Resource Manager 管理 Azure Bot Service 资源的 .NET 技能,介绍 Bot 创建与生命周期操作、渠道配置、连接设置和密钥重新生成,并强调验证历史示例及谨慎处理凭据和破坏性更改。
主要功能
- 01创建和更新 Bot 资源
- 02配置支持的 Bot 渠道
- 03列出 Bot 和渠道
- 04管理 OAuth 连接设置
- 05重新生成 DirectLine 密钥
适用场景
- 01创建 Azure Bot 资源
- 02配置 Web Chat 或 Teams 渠道
- 03检查已配置的 Bot 渠道
- 04谨慎轮换 DirectLine 密钥
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ sickn33/ agentic-awesome-skills --skill azure-mgmt-botservice-dotnet安装后在 Claude Code 中输入/azure-mgmt-botservice-dotnet确认已加载。
详细介绍
Azure.ResourceManager.BotService(.NET)
通过 Azure Resource Manager 配置和管理 Azure Bot Service 资源的管理平面 SDK。
配置和生命周期范围
这些是历史 SDK 代码片段。创建资源前,请确认当前的软件包和服务生命周期、支持的应用标识类型以及目标租户;不要假设所有频道或多租户配置都适用于新建机器人。请选择确切的订阅和资源组,不要依赖非预期的默认值。固定软件包版本,验证签名和导入内容,并使用获批的测试资源。创建频道会开放消息交互入口;删除机器人和重新生成 DirectLine 密钥可能会中断客户端。操作时必须具备相应的用户权限,分阶段轮换密钥,并避免将返回的密钥写入日志。在生产环境中,必须审慎选择凭据链。
安装
dotnet add package Azure.ResourceManager.BotService
dotnet add package Azure.Identity
历史示例版本:v1.1.1 / v1.1.0-beta.1;使用前请查看支持的版本和迁移指南。
环境变量
AZURE_SUBSCRIPTION_ID=<your-subscription-id>
# For service principal auth (optional)
AZURE_TENANT_ID=<tenant-id>
AZURE_CLIENT_ID=<client-id>
AZURE_CLIENT_SECRET=<client-secret>
身份验证
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.BotService;
// Authenticate using DefaultAzureCredential
var credential = new DefaultAzureCredential();
ArmClient armClient = new ArmClient(credential);
// Get subscription and resource group
SubscriptionResource subscription = await armClient.GetDefaultSubscriptionAsync();
ResourceGroupResource resourceGroup = await subscription.GetResourceGroups().GetAsync("myResourceGroup");
// Access bot collection
BotCollection botCollection = resourceGroup.GetBots();
资源层级
ArmClient
└── SubscriptionResource
└── ResourceGroupResource
└── BotResource
├── BotChannelResource (DirectLine, Teams, Slack, etc.)
├── BotConnectionSettingResource (OAuth connections)
└── BotServicePrivateEndpointConnectionResource
核心工作流
1. 创建 Bot 资源
using Azure.ResourceManager.BotService;
using Azure.ResourceManager.BotService.Models;
// Create bot data
var botData = new BotData(AzureLocation.WestUS2)
{
Kind = BotServiceKind.Azurebot,
Sku = new BotServiceSku(BotServiceSkuName.F0),
Properties = new BotProperties(
displayName: "MyBot",
endpoint: new Uri("https://mybot.azurewebsites.net/api/messages"),
msaAppId: "<your-msa-app-id>")
{
Description = "My Azure Bot",
MsaAppType = BotMsaAppType.MultiTenant
}
};
// Create or update the bot
ArmOperation<BotResource> operation = await botCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
"myBotName",
botData);
BotResource bot = operation.Value;
Console.WriteLine($"Bot created: {bot.Data.Name}");
2. 配置 DirectLine 频道
// Get the bot
BotResource bot = await resourceGroup.GetBots().GetAsync("myBotName");
// Get channel collection
BotChannelCollection channels = bot.GetBotChannels();
// Create DirectLine channel configuration
var channelData = new BotChannelData(AzureLocation.WestUS2)
{
Properties = new DirectLineChannel()
{
Properties = new DirectLineChannelProperties()
{
Sites =
{
new DirectLineSite("Default Site")
{
IsEnabled = true,
IsV1Enabled = false,
IsV3Enabled = true,
IsSecureSiteEnabled = true
}
}
}
}
};
// Create or update the channel
ArmOperation<BotChannelResource> channelOp = await channels.CreateOrUpdateAsync(
WaitUntil.Completed,
BotChannelName.DirectLineChannel,
channelData);
Console.WriteLine("DirectLine channel configured");
3. 配置 Microsoft Teams 频道
var teamsChannelData = new BotChannelData(AzureLocation.WestUS2)
{
Properties = new MsTeamsChannel()
{
Properties = new MsTeamsChannelProperties()
{
IsEnabled = true,
EnableCalling = false
}
}
};
await channels.CreateOrUpdateAsync(
WaitUntil.Completed,
BotChannelName.MsTeamsChannel,
teamsChannelData);
4. 配置 Web Chat 频道
var webChatChannelData = new BotChannelData(AzureLocation.WestUS2)
{
Properties = new WebChatChannel()
{
Properties = new WebChatChannelProperties()
{
Sites =
{
new WebChatSite("Default Site")
{
IsEnabled = true
}
}
}
}
};
await channels.CreateOrUpdateAsync(
WaitUntil.Completed,
BotChannelName.WebChatChannel,
webChatChannelData);
5. 获取 Bot 并列出频道
// Get bot
BotResource bot = await botCollection.GetAsync("myBotName");
Console.WriteLine($"Bot: {bot.Data.Properties.DisplayName}");
Console.WriteLine($"Endpoint: {bot.Data.Properties.Endpoint}");
// List channels
await foreach (BotChannelResource channel in bot.GetBotChannels().GetAllAsync())
{
Console.WriteLine($"Channel: {channel.Data.Name}");
}
6. 重新生成 DirectLine 密钥
var regenerateRequest = new BotChannelRegenerateKeysContent(BotChannelName.DirectLineChannel)
{
SiteName = "Default Site"
};
BotChannelResource channelWithKeys = await bot.GetBotChannelWithRegenerateKeysAsync(regenerateRequest);
7. 更新 Bot
BotResource bot = await botCollection.GetAsync("myBotName");
// Update using patch
var updateData = new BotData(bot.Data.Location)
{
Properties = new BotProperties(
displayName: "Updated Bot Name",
endpoint: bot.Data.Properties.Endpoint,
msaAppId: bot.Data.Properties.MsaAppId)
{
Description = "Updated description"
}
};
await bot.UpdateAsync(updateData);
8. 删除 Bot
BotResource bot = await botCollection.GetAsync("myBotName");
await bot.DeleteAsync(WaitUntil.Completed);
支持的频道类型
| 频道 | 常量 | 类 |
|---|---|---|
| Direct Line | BotChannelName.DirectLineChannel | DirectLineChannel |
| Direct Line Speech | BotChannelName.DirectLineSpeechChannel | DirectLineSpeechChannel |
| Microsoft Teams | BotChannelName.MsTeamsChannel | MsTeamsChannel |
| Web Chat | BotChannelName.WebChatChannel | WebChatChannel |
| Slack | BotChannelName.SlackChannel | SlackChannel |
BotChannelName.FacebookChannel | FacebookChannel | |
BotChannelName.EmailChannel | EmailChannel | |
| Telegram | BotChannelName.TelegramChannel | TelegramChannel |
| Telephony | BotChannelName.TelephonyChannel | TelephonyChannel |
关键类型参考
| 类型 | 用途 |
|---|---|
ArmClient | 所有 ARM 操作的入口 |
BotResource | 表示 Azure Bot 资源 |
BotCollection | 用于 Bot CRUD 操作的集合 |
BotData | Bot 资源定义 |
BotProperties | Bot 配置属性 |
BotChannelResource | 频道配置 |
BotChannelCollection | 频道集合 |
BotChannelData | 频道配置数据 |
BotConnectionSettingResource | OAuth 连接设置 |
BotServiceKind 值
| 值 | 描述 |
|---|---|
BotServiceKind.Azurebot | Azure Bot(推荐) |
BotServiceKind.Bot | 旧版 Bot Framework Bot |
BotServiceKind.Designer | Composer Bot |
BotServiceKind.Function | Function Bot |
BotServiceKind.Sdk | SDK Bot |
BotServiceSkuName 值
| 值 | 描述 |
|---|---|
BotServiceSkuName.F0 | 免费层 |
BotServiceSkuName.S1 | 标准层 |
BotMsaAppType 值
| 值 | 描述 |
|---|---|
BotMsaAppType.MultiTenant | 多租户应用 |
BotMsaAppType.SingleTenant | 单租户应用 |
BotMsaAppType.UserAssignedMSI | 用户分配的托管标识 |
最佳实践
- 始终使用
DefaultAzureCredential— 支持多种身份验证方式 - 同步操作使用
WaitUntil.Completed - 处理
RequestFailedException,以应对 API 错误 - 所有操作都使用异步方法(
*Async) - 安全存储 MSA 应用凭据 — 使用 Key Vault 保存机密
- 生产环境中的 Bot 使用托管标识(
BotMsaAppType.UserAssignedMSI) - 在生产环境中为 DirectLine 频道启用安全站点
错误处理
using Azure;
try
{
var operation = await botCollection.CreateOrUpdateAsync(
WaitUntil.Completed,
botName,
botData);
}
catch (RequestFailedException ex) when (ex.Status == 409)
{
Console.WriteLine("Bot already exists");
}
catch (RequestFailedException ex)
{
Console.WriteLine($"ARM Error: {ex.Status} - {ex.ErrorCode}: {ex.Message}");
}
相关 SDK
| SDK | 用途 | 安装 |
|---|---|---|
Azure.ResourceManager.BotService | Bot 管理(此 SDK) | dotnet add package Azure.ResourceManager.BotService |
Microsoft.Bot.Builder | Bot Framework SDK | dotnet add package Microsoft.Bot.Builder |
Microsoft.Bot.Builder.Integration.AspNet.Core | ASP.NET Core 集成 | dotnet add package Microsoft.Bot.Builder.Integration.AspNet.Core |
参考链接
适用场景
当需要执行概述中描述的工作流或操作时,可使用此技能。
限制
- 仅当任务明确符合上述范围时,才使用此技能。
- 不要将输出视为环境特定验证、测试或专家审查的替代品。
- 如果缺少必需的输入、权限、安全边界或成功标准,请暂停并请求澄清。
评论
支持 Markdown