Microsoft Bot Framework Python SDK (Core)
The `botbuilder-core` library provides the foundational abstractions for building conversational AI bots using the Microsoft Bot Framework in Python. It includes base classes for handling activities, managing turns, and accessing conversation state. As of version 4.17.1, the entire Bot Framework Python SDK has reached End-of-Life (EOL) and will no longer receive updates or support. The project is effectively abandoned.
Warnings
- breaking The Microsoft Bot Framework Python SDK, including `botbuilder-core`, has reached End-of-Life (EOL) with version 4.17.1. It is no longer actively maintained, supported, or receiving updates. New development should avoid this library.
- gotcha As of SDK version 4.15.0, the underlying `aiohttp` dependency requires Python 3.8 or newer. Bots deployed on Python 3.7 or older will encounter dependency conflicts or runtime errors.
- gotcha The library is built on `asyncio`. All bot logic, especially I/O operations and callbacks, must be `async` and properly `await`ed. Failure to do so can lead to deadlocks, unresponsive bots, or silent failures.
- gotcha Authentication is crucial for secure bots. Bots typically require `MicrosoftAppId` and `MicrosoftAppPassword` for production deployments. Running locally without these credentials might require specific adapter configurations to disable authentication checks, which is not recommended for production.
- deprecated While a temporary 'deprecation cancelled' notice appeared for version 4.14.5, the SDK ultimately did reach End-of-Life with 4.17.1. This inconsistency might cause confusion for developers checking release notes.
Install
-
pip install botbuilder-core botbuilder-integration-aiohttp
Imports
- ActivityHandler
from botbuilder.core import ActivityHandler
- TurnContext
from botbuilder.core import TurnContext
- BotFrameworkAdapter
from botbuilder.core import BotFrameworkAdapter
- BotFrameworkHttpAdapter
from botbuilder.integration.aiohttp import BotFrameworkHttpAdapter
Quickstart
import os
import asyncio
from aiohttp import web
from botbuilder.core import ActivityHandler, TurnContext
from botbuilder.schema import Activity
from botbuilder.integration.aiohttp import BotFrameworkHttpAdapter
class MyEchoBot(ActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
await turn_context.send_activity(f"You said: {turn_context.activity.text}")
async def on_members_added_activity(self, members_added: [object], turn_context: TurnContext):
for member in members_added:
if member.id != turn_context.activity.recipient.id:
await turn_context.send_activity("Hello and welcome!")
# Configuration (replace with your actual values or env vars)
# For local testing without authentication, these can be empty strings
APP_ID = os.environ.get("MicrosoftAppId", "")
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
# Create the BotFrameworkHttpAdapter
adapter = BotFrameworkHttpAdapter(APP_ID, APP_PASSWORD)
# Create the bot instance
bot = MyEchoBot()
# Listen for incoming requests on /api/messages
async def messages(request):
if "application/json" in request.headers["Content-Type"]:
body = await request.json()
else:
return web.Response(status=400)
activity = Activity().deserialize(body)
auth_header = request.headers["Authorization"] if "Authorization" in request.headers else ""
try:
response = await adapter.process_activity(activity, auth_header, bot.on_turn)
if response:
return web.json_response(data=response.body, status=response.status)
return web.Response(status=200)
except Exception as e:
print(f"Error processing activity: {e}")
return web.Response(status=500, text=str(e))
# Setup AIOHTTP web application
app = web.Application()
app.router.post("/api/messages", messages)
if __name__ == "__main__":
try:
web.run_app(app, host="localhost", port=3978)
except Exception as e:
raise e