azure-mgmt-botservice-py
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAzure Bot Service Management SDK for Python
适用于Python的Azure Bot Service管理SDK
Manage Azure Bot Service resources including bots, channels, and connections.
管理Azure Bot Service资源,包括机器人、渠道和连接。
Installation
安装
bash
pip install azure-mgmt-botservice
pip install azure-identitybash
pip install azure-mgmt-botservice
pip install azure-identityEnvironment Variables
环境变量
bash
AZURE_SUBSCRIPTION_ID=<your-subscription-id>
AZURE_RESOURCE_GROUP=<your-resource-group>bash
AZURE_SUBSCRIPTION_ID=<your-subscription-id>
AZURE_RESOURCE_GROUP=<your-resource-group>Authentication
身份验证
python
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"]
)python
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"]
)Create a Bot
创建机器人
python
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}")python
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"), # 免费层
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}")Get Bot Details
获取机器人详情
python
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}")python
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}")List Bots in Resource Group
列出资源组中的机器人
python
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}")python
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}")List All Bots in Subscription
列出订阅中的所有机器人
python
all_bots = client.bots.list()
for bot in all_bots:
print(f"Bot: {bot.name} in {bot.id.split('/')[4]}")python
all_bots = client.bots.list()
for bot in all_bots:
print(f"Bot: {bot.name} in {bot.id.split('/')[4]}")Update Bot
更新机器人
python
bot = client.bots.update(
resource_group_name=resource_group,
resource_name=bot_name,
properties=BotProperties(
display_name="Updated Bot Name",
description="Updated description"
)
)python
bot = client.bots.update(
resource_group_name=resource_group,
resource_name=bot_name,
properties=BotProperties(
display_name="Updated Bot Name",
description="Updated description"
)
)Delete Bot
删除机器人
python
client.bots.delete(
resource_group_name=resource_group,
resource_name=bot_name
)python
client.bots.delete(
resource_group_name=resource_group,
resource_name=bot_name
)Configure Channels
配置渠道
Add Teams Channel
添加Teams渠道
python
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
)
)
)
)python
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
)
)
)
)Add Direct Line Channel
添加Direct Line渠道
python
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
)
]
)
)
)
)python
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
)
]
)
)
)
)Add Web Chat Channel
添加Web Chat渠道
python
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
)
]
)
)
)
)python
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
)
]
)
)
)
)Get Channel Details
获取渠道详情
python
channel = client.channels.get(
resource_group_name=resource_group,
resource_name=bot_name,
channel_name="DirectLineChannel"
)python
channel = client.channels.get(
resource_group_name=resource_group,
resource_name=bot_name,
channel_name="DirectLineChannel"
)List Channel Keys
列出渠道密钥
python
keys = client.channels.list_with_keys(
resource_group_name=resource_group,
resource_name=bot_name,
channel_name="DirectLineChannel"
)python
keys = client.channels.list_with_keys(
resource_group_name=resource_group,
resource_name=bot_name,
channel_name="DirectLineChannel"
)Access Direct Line keys
Access Direct Line keys
if hasattr(keys.properties, 'properties'):
for site in keys.properties.properties.sites:
print(f"Site: {site.site_name}")
print(f"Key: {site.key}")
undefinedif hasattr(keys.properties, 'properties'):
for site in keys.properties.properties.sites:
print(f"Site: {site.site_name}")
print(f"Key: {site.key}")
undefinedBot Connections (OAuth)
机器人连接(OAuth)
Create Connection Setting
创建连接设置
python
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="<oauth-client-secret>",
scopes="User.Read",
service_provider_id="<service-provider-id>"
)
)
)python
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="<oauth-client-secret>",
scopes="User.Read",
service_provider_id="<service-provider-id>"
)
)
)List Connections
列出连接
python
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}")python
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 Operations
客户端操作
| Operation | Method |
|---|---|
| Bot CRUD operations |
| Channel configuration |
| OAuth connection settings |
| Direct Line channel operations |
| Email channel operations |
| Available operations |
| Host settings operations |
| 操作 | 方法 |
|---|---|
| 机器人CRUD操作 |
| 渠道配置 |
| OAuth连接设置 |
| Direct Line渠道操作 |
| 邮件渠道操作 |
| 可用操作 |
| 主机设置操作 |
SKU Options
SKU选项
| SKU | Description |
|---|---|
| Free tier (limited messages) |
| Standard tier (unlimited messages) |
| SKU | 描述 |
|---|---|
| 免费层(消息数量受限) |
| 标准层(无消息数量限制) |
Channel Types
渠道类型
| Channel | Class | Purpose |
|---|---|---|
| Microsoft Teams | Teams integration |
| Direct Line | Custom client integration |
| Web Chat | Embeddable web widget |
| Slack | Slack workspace integration |
| Messenger integration | |
| Email communication |
| 渠道 | 类 | 用途 |
|---|---|---|
| Microsoft Teams | Teams集成 |
| Direct Line | 自定义客户端集成 |
| Web Chat | 可嵌入的网页小部件 |
| Slack | Slack工作区集成 |
| Messenger集成 | |
| 邮件通信 |
Best Practices
最佳实践
- Use DefaultAzureCredential for authentication
- Start with F0 SKU for development, upgrade to S1 for production
- Store MSA App ID/Secret securely — use Key Vault
- Enable only needed channels — reduces attack surface
- Rotate Direct Line keys periodically
- Use managed identity when possible for bot connections
- Configure proper CORS for Web Chat channel
- 使用DefaultAzureCredential进行身份验证
- 从F0 SKU开始用于开发,生产环境升级到S1
- 安全存储MSA应用ID/密钥 — 使用Key Vault
- 仅启用所需渠道 — 减少攻击面
- 定期轮换Direct Line密钥
- 尽可能使用托管标识用于机器人连接
- 为Web Chat渠道配置合适的CORS