azure-mgmt-botservice-py

npx skills add https://github.com/microsoft/skills --skill azure-mgmt-botservice-py

SDK de Gestion Azure Bot Service pour Python

Gérez les ressources Azure Bot Service incluant les bots, les canaux et les connexions.

Installation

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

Variables d'environnement

AZURE_SUBSCRIPTION_ID=<your-subscription-id>  # Requis pour toutes les méthodes d'authentification
AZURE_RESOURCE_GROUP=<your-resource-group>  # Requis pour toutes les méthodes d'authentification
AZURE_TOKEN_CREDENTIALS=prod # Requis uniquement si DefaultAzureCredential est utilisé en production

Authentification

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

# Dev local : DefaultAzureCredential. Production : définissez AZURE_TOKEN_CREDENTIALS=prod ou AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Ou utilisez une credential spécifique directement en production :
# Voir https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()

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

Créer un Bot

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"),  # Tier gratuit
        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}")

Obtenir les Détails du Bot

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}")

Lister les Bots dans un Groupe de Ressources

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}")

Lister tous les Bots dans un Abonnement

all_bots = client.bots.list()

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

Mettre à Jour un Bot

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

Supprimer un Bot

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

Configurer les Canaux

Ajouter le Canal 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
            )
        )
    )
)

Ajouter le Canal 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
                    )
                ]
            )
        )
    )
)

Ajouter le Canal 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
                    )
                ]
            )
        )
    )
)

Obtenir les Détails du Canal

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

Lister les Clés du Canal

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

# Accéder aux clés Direct Line
if hasattr(keys.properties, 'properties'):
    for site in keys.properties.properties.sites:
        print(f"Site: {site.site_name}")
        print(f"Key: {site.key}")

Connexions Bot (OAuth)

Créer un Paramètre de Connexion

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>"
        )
    )
)

Lister les Connexions

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}")

Opérations Client

Opération Méthode
client.bots Opérations CRUD de bot
client.channels Configuration de canal
client.bot_connection Paramètres de connexion OAuth
client.direct_line Opérations du canal Direct Line
client.email Opérations du canal Email
client.operations Opérations disponibles
client.host_settings Opérations de paramètres d'hôte

Options de SKU

SKU Description
F0 Tier gratuit (messages limités)
S1 Tier standard (messages illimités)

Types de Canaux

Canal Classe Objectif
MsTeamsChannel Microsoft Teams Intégration Teams
DirectLineChannel Direct Line Intégration de client personnalisé
WebChatChannel Web Chat Widget web intégrable
SlackChannel Slack Intégration d'espace de travail Slack
FacebookChannel Facebook Intégration Messenger
EmailChannel Email Communication par email

Bonnes Pratiques

  1. Utilisez DefaultAzureCredential pour l'authentification
  2. Commencez par le SKU F0 pour le développement, passez à S1 pour la production
  3. Stockez l'ID/Secret MSA App de façon sécurisée — utilisez Key Vault
  4. Activez uniquement les canaux nécessaires — réduit la surface d'attaque
  5. Renouvelez les clés Direct Line périodiquement
  6. Utilisez une identité managée si possible pour les connexions de bot
  7. Configurez CORS correctement pour le canal Web Chat

Skills similaires