mcpField
A
azure-mgmt-botservice-dotnet

azure-mgmt-botservice-dotnet

github.com/sickn33云与 DevOps48.7k49 次安装今天更新
代码仓库

关于 azure-mgmt-botservice-dotnet

azure-mgmt-botservice-dotnet 是通过 Azure Resource Manager 管理 Azure Bot Service 资源的 .NET 技能,介绍 Bot 创建与生命周期操作、渠道配置、连接设置和密钥重新生成,并强调验证历史示例及谨慎处理凭据和破坏性更改。

主要功能

  1. 01创建和更新 Bot 资源
  2. 02配置支持的 Bot 渠道
  3. 03列出 Bot 和渠道
  4. 04管理 OAuth 连接设置
  5. 05重新生成 DirectLine 密钥

适用场景

  1. 01创建 Azure Bot 资源
  2. 02配置 Web Chat 或 Teams 渠道
  3. 03检查已配置的 Bot 渠道
  4. 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 LineBotChannelName.DirectLineChannelDirectLineChannel
Direct Line SpeechBotChannelName.DirectLineSpeechChannelDirectLineSpeechChannel
Microsoft TeamsBotChannelName.MsTeamsChannelMsTeamsChannel
Web ChatBotChannelName.WebChatChannelWebChatChannel
SlackBotChannelName.SlackChannelSlackChannel
FacebookBotChannelName.FacebookChannelFacebookChannel
EmailBotChannelName.EmailChannelEmailChannel
TelegramBotChannelName.TelegramChannelTelegramChannel
TelephonyBotChannelName.TelephonyChannelTelephonyChannel

关键类型参考

类型用途
ArmClient所有 ARM 操作的入口
BotResource表示 Azure Bot 资源
BotCollection用于 Bot CRUD 操作的集合
BotDataBot 资源定义
BotPropertiesBot 配置属性
BotChannelResource频道配置
BotChannelCollection频道集合
BotChannelData频道配置数据
BotConnectionSettingResourceOAuth 连接设置

BotServiceKind 值

值描述
BotServiceKind.AzurebotAzure Bot(推荐)
BotServiceKind.Bot旧版 Bot Framework Bot
BotServiceKind.DesignerComposer Bot
BotServiceKind.FunctionFunction Bot
BotServiceKind.SdkSDK Bot

BotServiceSkuName 值

值描述
BotServiceSkuName.F0免费层
BotServiceSkuName.S1标准层

BotMsaAppType 值

值描述
BotMsaAppType.MultiTenant多租户应用
BotMsaAppType.SingleTenant单租户应用
BotMsaAppType.UserAssignedMSI用户分配的托管标识

最佳实践

  1. 始终使用 DefaultAzureCredential — 支持多种身份验证方式
  2. 同步操作使用 WaitUntil.Completed
  3. 处理 RequestFailedException,以应对 API 错误
  4. 所有操作都使用异步方法(*Async)
  5. 安全存储 MSA 应用凭据 — 使用 Key Vault 保存机密
  6. 生产环境中的 Bot 使用托管标识(BotMsaAppType.UserAssignedMSI)
  7. 在生产环境中为 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.BotServiceBot 管理(此 SDK)dotnet add package Azure.ResourceManager.BotService
Microsoft.Bot.BuilderBot Framework SDKdotnet add package Microsoft.Bot.Builder
Microsoft.Bot.Builder.Integration.AspNet.CoreASP.NET Core 集成dotnet add package Microsoft.Bot.Builder.Integration.AspNet.Core

参考链接

资源URL
NuGet 包https://www.nuget.org/packages/Azure.ResourceManager.BotService
API 参考https://learn.microsoft.com/dotnet/api/azure.resourcemanager.botservice
GitHub 源码https://github.com/Azure/azure-sdk-for-net/tree/main/sdk/botservice/Azure.ResourceManager.BotService
Azure Bot Service 文档https://learn.microsoft.com/azure/bot-service/

适用场景

当需要执行概述中描述的工作流或操作时,可使用此技能。

限制

  • 仅当任务明确符合上述范围时,才使用此技能。
  • 不要将输出视为环境特定验证、测试或专家审查的替代品。
  • 如果缺少必需的输入、权限、安全边界或成功标准,请暂停并请求澄清。

评论

支持 Markdown