Share via


ActivityHandler class

Handles incoming activities from channels and dispatches them to the appropriate handlers.

Remarks

This class is provided to simplify the migration from Bot Framework SDK v4 to the Agents Hosting framework.

The ActivityHandler serves as the central hub for processing incoming activities in conversational AI applications. It provides a comprehensive framework for handling various activity types including messages, conversation updates, message reactions, typing indicators, installation updates, and invoke operations such as adaptive cards and search.

Key Features:

  • Activity Routing: Automatically routes activities to appropriate handlers based on activity type
  • Handler Registration: Provides fluent API methods (onMessage, onConversationUpdate, etc.) for registering event handlers
  • Invoke Support: Built-in handling for adaptive card actions and search invoke operations
  • Error Handling: Robust error handling with proper HTTP status codes for invoke operations
  • Extensibility: Designed for inheritance to allow custom behavior and specialized handlers

Usage:

const handler = new ActivityHandler()
  .onMessage(async (context, next) => {
    await context.sendActivity('Hello!');
    await next();
  })
  .onMembersAdded(async (context, next) => {
    // Welcome new members
    await next();
  });

Developers can extend this class to implement domain-specific logic, override default behaviors, or add support for custom activity types and invoke operations.

Methods

onConversationUpdate(AgentHandler)

Registers a handler for the ConversationUpdate activity type. This is called when the conversation is updated, such as when members are added or removed.

onDialog(AgentHandler)

Registers an activity event handler for the dialog event, emitted as the last event for an incoming activity. This handler is called after all other handlers have been processed.

onEndOfConversation(AgentHandler)

Registers a handler for the EndOfConversation activity type. This is called when the conversation ends.

onInstallationUpdate(AgentHandler)

Registers a handler for the InstallationUpdate activity type. This is called when an agent is installed or uninstalled.

onInstallationUpdateAdd(AgentHandler)

Registers a handler for the InstallationUpdateAdd activity type. This is called when an agent is installed or upgraded.

onInstallationUpdateRemove(AgentHandler)

Registers a handler for the InstallationUpdateRemove activity type. This is called when an agent is uninstalled or downgraded.

onMembersAdded(AgentHandler)

Registers a handler for the MembersAdded activity type. This is called when new members are added to the conversation.

onMembersRemoved(AgentHandler)

Registers a handler for the MembersRemoved activity type. This is called when members are removed from the conversation.

onMessage(AgentHandler)

Registers a handler for the Message activity type. This is called when a message is received from the user.

onMessageDelete(AgentHandler)

Registers a handler for the MessageDelete activity type. This is called when a message is deleted.

onMessageReaction(AgentHandler)

Registers a handler for the MessageReaction activity type. This is called when reactions are added or removed from messages.

onMessageUpdate(AgentHandler)

Registers a handler for the MessageUpdate activity type. This is called when a message is updated.

onReactionsAdded(AgentHandler)

Registers a handler for the ReactionsAdded activity type. This is called when reactions are added to messages.

onReactionsRemoved(AgentHandler)

Registers a handler for the ReactionsRemoved activity type. This is called when reactions are removed from messages.

onSignInInvoke(AgentHandler)

Registers a handler for the SignInInvoke activity type. This is called when a sign-in is requested.

onTurn(AgentHandler)

Registers a handler for the Turn activity type. This is called for all activities regardless of type.

onTyping(AgentHandler)

Registers a handler for the Typing activity type. This is called when a typing indicator is received.

onUnrecognizedActivityType(AgentHandler)

Registers a handler for unrecognized activity types. This is called when an activity type is not recognized.

run(TurnContext)

Runs the activity handler pipeline. This method is called to process an incoming activity through the registered handlers.

Method Details

onConversationUpdate(AgentHandler)

Registers a handler for the ConversationUpdate activity type. This is called when the conversation is updated, such as when members are added or removed.

function onConversationUpdate(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onDialog(AgentHandler)

Registers an activity event handler for the dialog event, emitted as the last event for an incoming activity. This handler is called after all other handlers have been processed.

function onDialog(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onEndOfConversation(AgentHandler)

Registers a handler for the EndOfConversation activity type. This is called when the conversation ends.

function onEndOfConversation(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onInstallationUpdate(AgentHandler)

Registers a handler for the InstallationUpdate activity type. This is called when an agent is installed or uninstalled.

function onInstallationUpdate(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onInstallationUpdateAdd(AgentHandler)

Registers a handler for the InstallationUpdateAdd activity type. This is called when an agent is installed or upgraded.

function onInstallationUpdateAdd(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onInstallationUpdateRemove(AgentHandler)

Registers a handler for the InstallationUpdateRemove activity type. This is called when an agent is uninstalled or downgraded.

function onInstallationUpdateRemove(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onMembersAdded(AgentHandler)

Registers a handler for the MembersAdded activity type. This is called when new members are added to the conversation.

function onMembersAdded(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onMembersRemoved(AgentHandler)

Registers a handler for the MembersRemoved activity type. This is called when members are removed from the conversation.

function onMembersRemoved(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onMessage(AgentHandler)

Registers a handler for the Message activity type. This is called when a message is received from the user.

function onMessage(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onMessageDelete(AgentHandler)

Registers a handler for the MessageDelete activity type. This is called when a message is deleted.

function onMessageDelete(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onMessageReaction(AgentHandler)

Registers a handler for the MessageReaction activity type. This is called when reactions are added or removed from messages.

function onMessageReaction(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onMessageUpdate(AgentHandler)

Registers a handler for the MessageUpdate activity type. This is called when a message is updated.

function onMessageUpdate(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onReactionsAdded(AgentHandler)

Registers a handler for the ReactionsAdded activity type. This is called when reactions are added to messages.

function onReactionsAdded(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onReactionsRemoved(AgentHandler)

Registers a handler for the ReactionsRemoved activity type. This is called when reactions are removed from messages.

function onReactionsRemoved(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onSignInInvoke(AgentHandler)

Registers a handler for the SignInInvoke activity type. This is called when a sign-in is requested.

function onSignInInvoke(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onTurn(AgentHandler)

Registers a handler for the Turn activity type. This is called for all activities regardless of type.

function onTurn(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onTyping(AgentHandler)

Registers a handler for the Typing activity type. This is called when a typing indicator is received.

function onTyping(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

onUnrecognizedActivityType(AgentHandler)

Registers a handler for unrecognized activity types. This is called when an activity type is not recognized.

function onUnrecognizedActivityType(handler: AgentHandler): ActivityHandler

Parameters

handler
AgentHandler

The handler to register

Returns

The current instance for method chaining

run(TurnContext)

Runs the activity handler pipeline. This method is called to process an incoming activity through the registered handlers.

function run(context: TurnContext): Promise<void>

Parameters

context
TurnContext

The turn context for the current turn of conversation

Returns

Promise<void>