Microsoft Bot Framework Bot Builder SDK for Python (Connector)
The `botframework-connector` library, part of the Microsoft Bot Framework Bot Builder SDK for Python (version 4.17.1), provides the necessary components for a bot to communicate directly with the Bot Connector service. This allows for sending and receiving activities (messages, events, etc.) to users across various channels configured in the Bot Framework Portal. However, the Bot Framework Python SDK reached its end-of-life with the 4.17.1 release and will no longer be updated, maintained, or supported through service tickets in the Azure portal. For new agent development, Microsoft recommends considering the Microsoft 365 Agents SDK.
Warnings
- breaking The Bot Framework Python SDK, including `botframework-connector`, has reached its End-of-Life (EOL) with version 4.17.1. It will no longer receive updates, maintenance, or support through Azure portal service tickets.
- breaking Beginning with SDK version 4.15.0, `aiohttp` package version 3.9+ (a core dependency) now requires Python 3.8 or newer. Bots running on Python 3.7 or earlier will encounter compatibility issues.
- gotcha Proper authentication using `MicrosoftAppId` and `MicrosoftAppPassword` is critical for production bots. Misconfiguration or absence of these credentials will prevent your bot from communicating with the Bot Connector Service.
- gotcha The `botframework-connector` library is typically used for low-level interactions with the Bot Connector Service, such as sending proactive messages. For a complete bot experience (handling incoming activities, managing turn context, dialogs), you would usually integrate it with `botbuilder-core` and its `BotFrameworkAdapter`.
Install
-
pip install botframework-connector
Imports
- ConnectorClient
from botframework.connector import ConnectorClient
- MicrosoftAppCredentials
from botframework.connector.auth import MicrosoftAppCredentials
- Activity
from botbuilder.schema import Activity
- ActivityTypes
from botbuilder.schema import ActivityTypes
- ChannelAccount
from botbuilder.schema import ChannelAccount
- ConversationParameters
from botbuilder.schema import ConversationParameters
Quickstart
import os
from botbuilder.schema import Activity, ActivityTypes, ChannelAccount, ConversationParameters
from botframework.connector import ConnectorClient
from botframework.connector.auth import MicrosoftAppCredentials
APP_ID = os.environ.get("MicrosoftAppId", "")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
SERVICE_URL = os.environ.get("BotServiceUrl", "https://slack.botframework.com") # Replace with your bot's service URL
CHANNEL_ID = os.environ.get("ChannelId", "slack") # Example channel ID, replace as needed
BOT_ID = os.environ.get("BotId", "your_bot_id") # Replace with your bot's ID
RECIPIENT_ID = os.environ.get("RecipientId", "user_id") # Replace with the recipient's user ID
if not APP_ID or not APP_PASSWORD:
print("WARNING: MicrosoftAppId and MicrosoftAppPassword environment variables not set. Authentication might fail for production scenarios.")
try:
credentials = MicrosoftAppCredentials(APP_ID, APP_PASSWORD)
connector = ConnectorClient(credentials, base_url=SERVICE_URL)
conversation_parameters = ConversationParameters(
is_group=False,
bot=ChannelAccount(id=BOT_ID),
members=[ChannelAccount(id=RECIPIENT_ID)],
# channel_data={"tenantId": "your_tenant_id"} # Optional: Add tenant ID for Teams, etc. if needed
)
conversation = connector.conversations.create_conversation(conversation_parameters)
activity = Activity(
type=ActivityTypes.message,
channel_id=CHANNEL_ID,
recipient=ChannelAccount(id=RECIPIENT_ID),
from_property=ChannelAccount(id=BOT_ID),
text='Hello World from botframework-connector!'
)
response = connector.conversations.send_to_conversation(conversation.id, activity)
print(f"Message sent. Activity ID: {response.id}")
except Exception as e:
print(f"An error occurred: {e}")
print("Ensure APP_ID, APP_PASSWORD, SERVICE_URL, BOT_ID, RECIPIENT_ID are correctly configured and accessible.")