mcpField
A
azure-mgmt-botservice-py

azure-mgmt-botservice-py

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

关于 azure-mgmt-botservice-py

通过管理 SDK 操作 Azure Bot Service 资源的 Python 技能,支持 Bot 生命周期管理、渠道配置和 OAuth 连接设置。

主要功能

  1. 01创建、查看、更新和删除 Bot
  2. 02按资源组或订阅列出 Bot
  3. 03配置 Bot 渠道
  4. 04获取渠道详情和密钥
  5. 05管理 OAuth 连接设置

适用场景

  1. 01检查订阅中的 Bot
  2. 02设置 Teams 或 Web Chat 渠道
  3. 03配置 Bot OAuth 连接

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/sickn33/agentic-awesome-skills --skill azure-mgmt-botservice-py

安装后在 Claude Code 中输入/azure-mgmt-botservice-py确认已加载。

详细介绍

Azure Bot Service Management SDK for Python

管理 Azure Bot Service 资源,包括机器人、频道和连接。

安装

pip install azure-mgmt-botservice
pip install azure-identity

环境变量

AZURE_SUBSCRIPTION_ID=<your-subscription-id>
AZURE_RESOURCE_GROUP=<your-resource-group>

身份验证

from azure.identity import DefaultAzureCredential
from azure.mgmt.botservice import AzureBotService
import os

credential = DefaultAzureCredential()
client = AzureBotService(
    credential=credential,
    subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"]
)

创建机器人

from azure.mgmt.botservice import AzureBotService
from azure.mgmt.botservice.models import Bot, BotProperties, Sku
from azure.identity import DefaultAzureCredential
import os

credential = DefaultAzureCredential()
client = AzureBotService(
    credential=credential,
    subscription_id=os.environ["AZURE_SUBSCRIPTION_ID"]
)

resource_group = os.environ["AZURE_RESOURCE_GROUP"]
bot_name = "my-chat-bot"

bot = client.bots.create(
    resource_group_name=resource_group,
    resource_name=bot_name,
    parameters=Bot(
        location="global",
        sku=Sku(name="F0"),  # Free tier
        kind="azurebot",
        properties=BotProperties(
            display_name="My Chat Bot",
            description="A conversational AI bot",
            endpoint="https://my-bot-app.azurewebsites.net/api/messages",
            msa_app_id="<your-app-id>",
            msa_app_type="MultiTenant"
        )
    )
)

print(f"Bot created: {bot.name}")

获取机器人详细信息

bot = client.bots.get(
    resource_group_name=resource_group,
    resource_name=bot_name
)

print(f"Bot: {bot.properties.display_name}")
print(f"Endpoint: {bot.properties.endpoint}")
print(f"SKU: {bot.sku.name}")

列出资源组中的机器人

bots = client.bots.list_by_resource_group(resource_group_name=resource_group)

for bot in bots:
    print(f"Bot: {bot.name} - {bot.properties.display_name}")

列出订阅中的所有机器人

all_bots = client.bots.list()

for bot in all_bots:
    print(f"Bot: {bot.name} in {bot.id.split('/')[4]}")

更新机器人

bot = client.bots.update(
    resource_group_name=resource_group,
    resource_name=bot_name,
    properties=BotProperties(
        display_name="Updated Bot Name",
        description="Updated description"
    )
)

删除机器人

client.bots.delete(
    resource_group_name=resource_group,
    resource_name=bot_name
)

配置频道

添加 Teams 频道
from azure.mgmt.botservice.models import (
    BotChannel,
    MsTeamsChannel,
    MsTeamsChannelProperties
)

channel = client.channels.create(
    resource_group_name=resource_group,
    resource_name=bot_name,
    channel_name="MsTeamsChannel",
    parameters=BotChannel(
        location="global",
        properties=MsTeamsChannel(
            properties=MsTeamsChannelProperties(
                is_enabled=True
            )
        )
    )
)
添加 Direct Line 频道
from azure.mgmt.botservice.models import (
    BotChannel,
    DirectLineChannel,
    DirectLineChannelProperties,
    DirectLineSite
)

channel = client.channels.create(
    resource_group_name=resource_group,
    resource_name=bot_name,
    channel_name="DirectLineChannel",
    parameters=BotChannel(
        location="global",
        properties=DirectLineChannel(
            properties=DirectLineChannelProperties(
                sites=[
                    DirectLineSite(
                        site_name="Default Site",
                        is_enabled=True,
                        is_v1_enabled=False,
                        is_v3_enabled=True
                    )
                ]
            )
        )
    )
)
添加 Web Chat 频道
from azure.mgmt.botservice.models import (
    BotChannel,
    WebChatChannel,
    WebChatChannelProperties,
    WebChatSite
)

channel = client.channels.create(
    resource_group_name=resource_group,
    resource_name=bot_name,
    channel_name="WebChatChannel",
    parameters=BotChannel(
        location="global",
        properties=WebChatChannel(
            properties=WebChatChannelProperties(
                sites=[
                    WebChatSite(
                        site_name="Default Site",
                        is_enabled=True
                    )
                ]
            )
        )
    )
)

获取频道详细信息

channel = client.channels.get(
    resource_group_name=resource_group,
    resource_name=bot_name,
    channel_name="DirectLineChannel"
)

列出频道密钥

keys = client.channels.list_with_keys(
    resource_group_name=resource_group,
    resource_name=bot_name,
    channel_name="DirectLineChannel"
)

# Access Direct Line keys
if hasattr(keys.properties, 'properties'):
    for site in keys.properties.properties.sites:
        print(f"Site: {site.site_name}")
        # Do not print Direct Line keys; store only in approved secret storage.

机器人连接(OAuth)

创建连接设置
import os
from azure.mgmt.botservice.models import (
    ConnectionSetting,
    ConnectionSettingProperties
)

connection = client.bot_connection.create(
    resource_group_name=resource_group,
    resource_name=bot_name,
    connection_name="graph-connection",
    parameters=ConnectionSetting(
        location="global",
        properties=ConnectionSettingProperties(
            client_id="<oauth-client-id>",
            client_secret=os.environ["BOT_OAUTH_CLIENT_SECRET"],
            scopes="User.Read",
            service_provider_id="<service-provider-id>"
        )
    )
)
列出连接
connections = client.bot_connection.list_by_bot_service(
    resource_group_name=resource_group,
    resource_name=bot_name
)

for conn in connections:
    print(f"Connection: {conn.name}")

客户端操作

操作方法
client.bots机器人 CRUD 操作
client.channels频道配置
client.bot_connectionOAuth 连接设置
client.direct_lineDirect Line 频道操作
client.email电子邮件频道操作
client.operations可用操作
client.host_settings主机设置操作

SKU 选项

SKU描述
F0免费层级(消息数量有限)
S1标准层级(消息数量不限)

频道类型

频道类用途
MsTeamsChannelMicrosoft TeamsTeams 集成
DirectLineChannelDirect Line自定义客户端集成
WebChatChannelWeb Chat可嵌入的 Web 小组件
SlackChannelSlackSlack 工作区集成
FacebookChannelFacebookMessenger 集成
EmailChannel电子邮件电子邮件通信

最佳实践

  1. 使用 DefaultAzureCredential 进行身份验证
  2. 开发时从 F0 SKU 开始,生产环境升级到 S1
  3. 安全存储 MSA App ID/Secret — 使用 Key Vault
  4. 仅启用所需频道 — 减少攻击面
  5. 定期轮换 Direct Line 密钥
  6. 尽可能在机器人连接中使用托管标识
  7. 为 Web Chat 频道配置适当的 CORS

适用场景

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

限制

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

管理示例会创建或更改真实资源和频道:请确认订阅、资源组、机器人/频道 ID 以及预期差异。注册新机器人前,请核实当前支持的身份和应用类型以及频道/API 版本;历史示例中的 MultiTenant 并不能证明当前具备注册资格。F0 是层级名称,并不保证每种集成/频道都免费。不要记录频道密钥或完整管理响应。

评论

支持 Markdown