Microsoft Bot Framework Bot Builder core functionality for building conversational AI bots and chatbots in Python.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Core functionality for processing bot activities including messages, member additions, invokes, and other Bot Framework activity types. The ActivityHandler provides event-driven methods for handling different scenarios in conversational flows.
Main activity handler that provides the entry point for processing all Bot Framework activities. Inherits from the Bot class and implements turn-based activity processing with specialized methods for different activity types.
class ActivityHandler:
async def on_turn(self, turn_context: TurnContext):
"""
Main entry point for processing activities.
Args:
turn_context (TurnContext): Context for the current conversation turn
"""
async def on_message_activity(self, turn_context: TurnContext):
"""
Handle message activities from users.
Override this method to process user messages.
Args:
turn_context (TurnContext): Context containing the message activity
"""
async def on_members_added_activity(self, members_added, turn_context: TurnContext):
"""
Handle when members are added to the conversation.
Args:
members_added (list): List of ChannelAccount objects for added members
turn_context (TurnContext): Context for the current turn
"""
async def on_members_removed_activity(self, members_removed, turn_context: TurnContext):
"""
Handle when members are removed from the conversation.
Args:
members_removed (list): List of ChannelAccount objects for removed members
turn_context (TurnContext): Context for the current turn
"""
async def on_invoke_activity(self, turn_context: TurnContext):
"""
Handle invoke activities (typically from cards or skills).
Args:
turn_context (TurnContext): Context containing the invoke activity
Returns:
InvokeResponse or None: Response to the invoke request
"""
async def on_adaptive_card_invoke(self, turn_context: TurnContext, invoke_value):
"""
Handle Adaptive Card invoke activities.
Args:
turn_context (TurnContext): Context for the current turn
invoke_value: Value from the Adaptive Card invoke
Returns:
InvokeResponse: Response to the Adaptive Card action
"""
async def on_signin_invoke(self, turn_context: TurnContext):
"""
Handle sign-in invoke activities.
Args:
turn_context (TurnContext): Context for the current turn
Returns:
InvokeResponse: Response to the sign-in invoke
"""
async def on_token_response_event(self, turn_context: TurnContext):
"""
Handle token response events from OAuth providers.
Args:
turn_context (TurnContext): Context containing the token response
"""
async def on_event_activity(self, turn_context: TurnContext):
"""
Handle event activities.
Args:
turn_context (TurnContext): Context containing the event activity
"""
async def on_unrecognized_activity_type(self, turn_context: TurnContext):
"""
Handle unrecognized activity types.
Args:
turn_context (TurnContext): Context containing the unrecognized activity
"""
async def on_message_reaction_activity(self, turn_context: TurnContext):
"""
Handle message reaction activities.
Args:
turn_context (TurnContext): Context containing the reaction activity
"""
async def on_reactions_added(self, message_reactions, turn_context: TurnContext):
"""
Handle when reactions are added to messages.
Args:
message_reactions (list): List of MessageReaction objects
turn_context (TurnContext): Context for the current turn
"""
async def on_reactions_removed(self, message_reactions, turn_context: TurnContext):
"""
Handle when reactions are removed from messages.
Args:
message_reactions (list): List of MessageReaction objects
turn_context (TurnContext): Context for the current turn
"""
async def on_message_update_activity(self, turn_context: TurnContext):
"""
Handle message update activities.
Args:
turn_context (TurnContext): Context containing the message update activity
"""
async def on_message_delete_activity(self, turn_context: TurnContext):
"""
Handle message delete activities.
Args:
turn_context (TurnContext): Context containing the message delete activity
"""
async def on_conversation_update_activity(self, turn_context: TurnContext):
"""
Handle conversation update activities.
Args:
turn_context (TurnContext): Context containing the conversation update activity
"""
async def on_end_of_conversation_activity(self, turn_context: TurnContext):
"""
Handle end of conversation activities.
Args:
turn_context (TurnContext): Context containing the end of conversation activity
"""
async def on_typing_activity(self, turn_context: TurnContext):
"""
Handle typing activities.
Args:
turn_context (TurnContext): Context containing the typing activity
"""
async def on_installation_update(self, turn_context: TurnContext):
"""
Handle installation update activities.
Args:
turn_context (TurnContext): Context containing the installation update activity
"""Abstract base class that ActivityHandler inherits from, providing the foundation for all bot implementations.
class Bot:
async def on_turn(self, turn_context: TurnContext):
"""
Main turn processing method that must be implemented by derived classes.
Args:
turn_context (TurnContext): Context for the current conversation turn
"""from botbuilder.core import ActivityHandler, TurnContext, MessageFactory
class EchoBot(ActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
# Echo back the user's message
reply_text = f"You said: {turn_context.activity.text}"
await turn_context.send_activity(MessageFactory.text(reply_text))
async def on_members_added_activity(self, members_added, turn_context: TurnContext):
# Welcome new members
for member in members_added:
if member.id != turn_context.activity.recipient.id:
welcome_text = f"Welcome {member.name}!"
await turn_context.send_activity(MessageFactory.text(welcome_text))class MultiActivityBot(ActivityHandler):
async def on_message_activity(self, turn_context: TurnContext):
text = turn_context.activity.text.lower()
if "hello" in text:
await turn_context.send_activity(MessageFactory.text("Hello there!"))
elif "help" in text:
await turn_context.send_activity(MessageFactory.text("How can I help you?"))
else:
await turn_context.send_activity(MessageFactory.text("I didn't understand that."))
async def on_members_added_activity(self, members_added, turn_context: TurnContext):
for member in members_added:
if member.id != turn_context.activity.recipient.id:
await turn_context.send_activity(
MessageFactory.text("Welcome! Type 'help' for assistance.")
)
async def on_invoke_activity(self, turn_context: TurnContext):
# Handle Adaptive Card or other invoke activities
if turn_context.activity.name == "adaptiveCard/action":
return await self.on_adaptive_card_invoke(turn_context, turn_context.activity.value)
return None
async def on_adaptive_card_invoke(self, turn_context: TurnContext, invoke_value):
# Process Adaptive Card action
action_type = invoke_value.get("action", "")
if action_type == "submit":
await turn_context.send_activity(MessageFactory.text("Form submitted!"))
# Return response
from botbuilder.core import InvokeResponse
return InvokeResponse(status=200, body={"type": "continue"})
async def on_token_response_event(self, turn_context: TurnContext):
# Handle OAuth token response
token = turn_context.activity.value.get("token")
if token:
await turn_context.send_activity(MessageFactory.text("Authentication successful!"))
else:
await turn_context.send_activity(MessageFactory.text("Authentication failed."))class InvokeResponse:
"""Response for invoke activities."""
def __init__(self, status: int, body: object = None):
self.status = status
self.body = body
class MessageReaction:
"""Represents a reaction to a message."""
type: str
class ChannelAccount:
"""Represents a channel account (user or bot)."""
id: str
name: strInstall with Tessl CLI
npx tessl i tessl/pypi-botbuilder-core