ActivityHandler Class

  • java.lang.Object
    • com.microsoft.bot.builder.ActivityHandler

Implements

Bot


public class ActivityHandler
implements Bot

An implementation of the Bot interface intended for further subclassing. Derive from this class to plug in code to handle particular Activity types. Pre and post processing of Activities can be plugged in by deriving and calling the base class implementation.

Constructor Summary

Constructor Description
ActivityHandler()

Method Summary

Modifier and Type Method and Description
protected InvokeResponse createInvokeResponse(Object body)

Creates a success InvokeResponse with the specified body.

protected java.util.concurrent.CompletableFuture<AdaptiveCardInvokeResponse> onAdaptiveCardInvoke(TurnContext turnContext, AdaptiveCardInvokeValue invokeValue)

Invoked when the bot is sent an Adaptive Card Action Execute.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onCommandActivity(TurnContext turnContext)

Invoked when a command activity is received when the base behavior of onTurn(TurnContext turnContext) is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onCommandResultActivity(TurnContext turnContext)

Invoked when a CommandResult activity is received when the base behavior of onTurn(TurnContext turnContext) is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onConversationUpdateActivity(TurnContext turnContext)

Invoked when a conversation update activity is received from the channel when the base behavior of onTurn(TurnContext turnContext) is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onEndOfConversationActivity(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.END_OF_CONVERSATION activities.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onEvent(TurnContext turnContext)

Invoked when an event other than tokens/response is received when the base behavior of onEventActivity(TurnContext turnContext) is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onEventActivity(TurnContext turnContext)

Invoked when an event activity is received from the connector when the base behavior of onTurn(TurnContext turnContext) is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onInstallationUpdate(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate activities.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onInstallationUpdateAdd(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate activities with 'action' set to 'add'.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onInstallationUpdateRemove(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate activities with 'action' set to 'remove'.

protected java.util.concurrent.CompletableFuture<InvokeResponse> onInvokeActivity(TurnContext turnContext)

Invoked when an invoke activity is received from the connector when the base behavior of onTurn is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onMembersAdded(List<ChannelAccount> membersAdded, TurnContext turnContext)

Override this in a derived class to provide logic for when members other than the bot join the conversation, such as your bot's welcome logic.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onMembersRemoved(List<ChannelAccount> membersRemoved, TurnContext turnContext)

Override this in a derived class to provide logic for when members other than the bot leave the conversation, such as your bot's good-bye logic.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onMessageActivity(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes#MESSAGE activities, such as the conversational logic.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onMessageReactionActivity(TurnContext turnContext)

Invoked when an event activity is received from the connector when the base behavior of onTurn(TurnContext turnContext) is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onReactionsAdded(List<MessageReaction> messageReactions, TurnContext turnContext)

Override this in a derived class to provide logic for when reactions to a previous activity are added to the conversation.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onReactionsRemoved(List<MessageReaction> messageReactions, TurnContext turnContext)

Override this in a derived class to provide logic for when reactions to a previous activity are removed from the conversation.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onSignInInvoke(TurnContext turnContext)

Invoked when a 'signin/verifyState' or 'signin/tokenExchange' event is received when the base behavior of onInvokeActivity is used.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onTokenResponseEvent(TurnContext turnContext)

Invoked when a "tokens/response" event is received when the base behavior of onEventActivity(TurnContext turnContext) is used.

java.util.concurrent.CompletableFuture<java.lang.Void> onTurn(TurnContext turnContext)

Called by the adapter (for example, a BotFrameworkAdapter) at runtime in order to process an inbound Activity.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onTypingActivity(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.Typing activities.

protected java.util.concurrent.CompletableFuture<java.lang.Void> onUnrecognizedActivityType(TurnContext turnContext)

Invoked when an activity other than a message, conversation update, or event is received when the base behavior of onTurn(TurnContext turnContext) is used.

Methods inherited from java.lang.Object

java.lang.Object.clone java.lang.Object.equals java.lang.Object.finalize java.lang.Object.getClass java.lang.Object.hashCode java.lang.Object.notify java.lang.Object.notifyAll java.lang.Object.toString java.lang.Object.wait java.lang.Object.wait java.lang.Object.wait

Constructor Details

ActivityHandler

public ActivityHandler()

Method Details

createInvokeResponse

protected InvokeResponse createInvokeResponse(Object body)

Creates a success InvokeResponse with the specified body.

Parameters:

body - The body to return in the invoke response.

Returns:

The InvokeResponse object.

onAdaptiveCardInvoke

protected CompletableFuture onAdaptiveCardInvoke(TurnContext turnContext, AdaptiveCardInvokeValue invokeValue)

Invoked when the bot is sent an Adaptive Card Action Execute.

Parameters:

turnContext - A strongly-typed context Object for this turn.
invokeValue - A stringly-typed Object from the incoming activity's Value.

Returns:

A task that represents the work queued to execute. When the {@link OnInvokeActivity(TurnContext(InvokeActivity))} method receives an Invoke with a InvokeActivity.name of adaptiveCard/action, it calls this method.

onCommandActivity

protected CompletableFuture onCommandActivity(TurnContext turnContext)

Invoked when a command activity is received when the base behavior of onTurn(TurnContext turnContext) is used. Commands are requests to perform an action and receivers typically respond with one or more commandResult activities. Receivers are also expected to explicitly reject unsupported command activities.

Parameters:

turnContext - A strongly-typed context Object for this turn.

Returns:

A task that represents the work queued to execute. When the onTurn(TurnContext turnContext) method receives a command activity, it calls this method. In a derived class, override this method to add logic that applies to all comand activities. Add logic to apply before the specific command-handling logic before the call to the base class onCommandActivity(TurnContext turnContext) method. Add logic to apply after the specific command-handling logic after the call to the base class onCommandActivity(TurnContext turnContext) method. Command activities communicate programmatic information from a client or channel to a bot. The meaning of an command activity is defined by the name property, which is meaningful within the scope of a channel.

onCommandResultActivity

protected CompletableFuture onCommandResultActivity(TurnContext turnContext)

Invoked when a CommandResult activity is received when the base behavior of onTurn(TurnContext turnContext) is used. CommandResult activities can be used to communicate the result of a command execution.

Parameters:

turnContext - A strongly-typed context Object for this turn.

Returns:

A task that represents the work queued to execute. When the onTurn(TurnContext turnContext) method receives a CommandResult activity, it calls this method. In a derived class, override this method to add logic that applies to all comand activities. Add logic to apply before the specific CommandResult-handling logic before the call to the base class onCommandResultActivity(TurnContext turnContext) method. Add logic to apply after the specific CommandResult-handling logic after the call to the base class onCommandResultActivity(TurnContext turnContext) method. CommandResult activities communicate programmatic information from a client or channel to a bot. The meaning of an CommandResult activity is defined by the name property, which is meaningful within the scope of a channel.

onConversationUpdateActivity

protected CompletableFuture onConversationUpdateActivity(TurnContext turnContext)

Invoked when a conversation update activity is received from the channel when the base behavior of onTurn(TurnContext turnContext) is used.

Conversation update activities are useful when it comes to responding to users being added to or removed from the conversation.

For example, a bot could respond to a user being added by greeting the user. By default, this method will call onMembersAdded(List<ChannelAccount> membersAdded, TurnContext turnContext) if any users have been added or onMembersRemoved(List<ChannelAccount> membersRemoved, TurnContext turnContext) if any users have been removed. The method checks the member ID so that it only responds to updates regarding members other than the bot itself.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onEndOfConversationActivity

protected CompletableFuture onEndOfConversationActivity(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.END_OF_CONVERSATION activities.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onEvent

protected CompletableFuture onEvent(TurnContext turnContext)

Invoked when an event other than tokens/response is received when the base behavior of onEventActivity(TurnContext turnContext) is used.

This method could optionally be overridden if the bot is meant to handle miscellaneous events.

By default, this method does nothing.

When the onEventActivity(TurnContext turnContext) method receives an event with a Activity#getName() other than `tokens/response`, it calls this method.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onEventActivity

protected CompletableFuture onEventActivity(TurnContext turnContext)

Invoked when an event activity is received from the connector when the base behavior of onTurn(TurnContext turnContext) is used.

Event activities can be used to communicate many different things.

By default, this method will call onTokenResponseEvent(TurnContext turnContext) if the activity's name is "tokens/response" or onEvent(TurnContext turnContext) otherwise. "tokens/response" event can be triggered by an com.microsoft.bot.schema.OAuthCard.

When the onTurn(TurnContext turnContext) method receives an event activity, it calls this method.

If the event Activity#getName is `tokens/response`, it calls onTokenResponseEvent(TurnContext turnContext) otherwise, it calls onEvent(TurnContext turnContext).

In a derived class, override this method to add logic that applies to all event activities. Add logic to apply before the specific event-handling logic before the call to the base class method. Add logic to apply after the specific event-handling logic after the call to the base class method.

Event activities communicate programmatic information from a client or channel to a bot. The meaning of an event activity is defined by the Activity#getName property, which is meaningful within the scope of a channel. A `tokens/response` event can be triggered by an com.microsoft.bot.schema.OAuthCard or an OAuth prompt.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onInstallationUpdate

protected CompletableFuture onInstallationUpdate(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate activities.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onInstallationUpdateAdd

protected CompletableFuture onInstallationUpdateAdd(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate activities with 'action' set to 'add'.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onInstallationUpdateRemove

protected CompletableFuture onInstallationUpdateRemove(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.InstallationUpdate activities with 'action' set to 'remove'.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onInvokeActivity

protected CompletableFuture onInvokeActivity(TurnContext turnContext)

Invoked when an invoke activity is received from the connector when the base behavior of onTurn is used.

Invoke activities can be used to communicate many different things. By default, this method will call onSignInInvokeAsync if the activity's name is 'signin/verifyState' or 'signin/tokenExchange'.

A 'signin/verifyState' or 'signin/tokenExchange' invoke can be triggered by an OAuthCard.

Parameters:

turnContext - The current TurnContext.

Returns:

A task that represents the work queued to execute.

onMembersAdded

protected CompletableFuture onMembersAdded(List membersAdded, TurnContext turnContext)

Override this in a derived class to provide logic for when members other than the bot join the conversation, such as your bot's welcome logic.

When the onConversationUpdateActivity(TurnContext turnContext) method receives a conversation update activity that indicates one or more users other than the bo are joining the conversation, it calls this method.

Parameters:

membersAdded - A list of all the members added to the conversation, as described by the conversation update activity.
turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onMembersRemoved

protected CompletableFuture onMembersRemoved(List membersRemoved, TurnContext turnContext)

Override this in a derived class to provide logic for when members other than the bot leave the conversation, such as your bot's good-bye logic.

When the onConversationUpdateActivity(TurnContext turnContext) method receives a conversation update activity that indicates one or more users other than the bot are leaving the conversation, it calls this method.

Parameters:

membersRemoved - A list of all the members removed from the conversation, as described by the conversation update activity.
turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onMessageActivity

protected CompletableFuture onMessageActivity(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes#MESSAGE activities, such as the conversational logic.

When the onTurn(TurnContext turnContext) method receives a message activity, it calls this method.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onMessageReactionActivity

protected CompletableFuture onMessageReactionActivity(TurnContext turnContext)

Invoked when an event activity is received from the connector when the base behavior of onTurn(TurnContext turnContext) is used.

Message reactions correspond to the user adding a 'like' or 'sad' etc. (often an emoji) to a previously sent activity. Message reactions are only supported by a few channels.

The activity that the message reaction corresponds to is indicated in the replyToId property. The value of this property is the activity id of a previously sent activity given back to the bot as the response from a send call.

When the onTurn(TurnContext turnContext) method receives a message reaction activity, it calls this method. If the message reaction indicates that reactions were added to a message, it calls onReactionsAdded(List<MessageReaction> messageReactions, TurnContext turnContext). If the message reaction indicates that reactions were removed from a message, it calls onReactionsRemoved(List<MessageReaction> messageReactions, TurnContext turnContext).

In a derived class, override this method to add logic that applies to all message reaction activities. Add logic to apply before the reactions added or removed logic before the call to the base class method. Add logic to apply after the reactions added or removed logic after the call to the base class.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onReactionsAdded

protected CompletableFuture onReactionsAdded(List messageReactions, TurnContext turnContext)

Override this in a derived class to provide logic for when reactions to a previous activity are added to the conversation.

Message reactions correspond to the user adding a 'like' or 'sad' etc. (often an emoji) to a previously sent message on the conversation. Message reactions are supported by only a few channels. The activity that the message is in reaction to is identified by the activity's Activity#getReplyToId() property. The value of this property is the activity ID of a previously sent activity. When the bot sends an activity, the channel assigns an ID to it, which is available in the com.microsoft.bot.schema.ResourceResponse#getId of the result.

Parameters:

messageReactions - The list of reactions added.
turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onReactionsRemoved

protected CompletableFuture onReactionsRemoved(List messageReactions, TurnContext turnContext)

Override this in a derived class to provide logic for when reactions to a previous activity are removed from the conversation.

Message reactions correspond to the user adding a 'like' or 'sad' etc. (often an emoji) to a previously sent message on the conversation. Message reactions are supported by only a few channels. The activity that the message is in reaction to is identified by the activity's Activity#getReplyToId() property. The value of this property is the activity ID of a previously sent activity. When the bot sends an activity, the channel assigns an ID to it, which is available in the com.microsoft.bot.schema.ResourceResponse#getId of the result.

Parameters:

messageReactions - The list of reactions removed.
turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onSignInInvoke

protected CompletableFuture onSignInInvoke(TurnContext turnContext)

Invoked when a 'signin/verifyState' or 'signin/tokenExchange' event is received when the base behavior of onInvokeActivity is used.

If using an OAuthPrompt, override this method to forward this Activity to the current dialog. By default, this method does nothing.

When the onInvokeActivity method receives an Invoke with a name of `tokens/response`, it calls this method.

If your bot uses the OAuthPrompt, forward the incoming Activity to the current dialog.

Parameters:

turnContext - The current TurnContext.

Returns:

A task that represents the work queued to execute.

onTokenResponseEvent

protected CompletableFuture onTokenResponseEvent(TurnContext turnContext)

Invoked when a "tokens/response" event is received when the base behavior of onEventActivity(TurnContext turnContext) is used.

If using an OAuthPrompt, override this method to forward this Activity to the current dialog.

By default, this method does nothing.

When the onEventActivity(TurnContext turnContext) method receives an event with a Activity#getName() of `tokens/response`, it calls this method.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onTurn

public CompletableFuture onTurn(TurnContext turnContext)

Called by the adapter (for example, a BotFrameworkAdapter) at runtime in order to process an inbound Activity.

This method calls other methods in this class based on the type of the activity to process, which allows a derived class to provide type-specific logic in a controlled way.

In a derived class, override this method to add logic that applies to all activity types. Add logic to apply before the type-specific logic before the call to the base class onTurn(TurnContext turnContext) method. Add logic to apply after the type-specific logic after the call to the base class onTurn(TurnContext turnContext) method.

Parameters:

turnContext - The context object for this turn. Provides information about the incoming activity, and other data needed to process the activity.

Returns:

A task that represents the work queued to execute.

onTypingActivity

protected CompletableFuture onTypingActivity(TurnContext turnContext)

Override this in a derived class to provide logic specific to ActivityTypes.Typing activities.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

onUnrecognizedActivityType

protected CompletableFuture onUnrecognizedActivityType(TurnContext turnContext)

Invoked when an activity other than a message, conversation update, or event is received when the base behavior of onTurn(TurnContext turnContext) is used.

If overridden, this could potentially respond to any of the other activity types like com.microsoft.bot.schema.ActivityTypes#CONTACT_RELATION_UPDATE or com.microsoft.bot.schema.ActivityTypes#END_OF_CONVERSATION.

By default, this method does nothing.

When the onTurn(TurnContext turnContext) method receives an activity that is not a message, conversation update, message reaction, or event activity, it calls this method.

Parameters:

turnContext - The context object for this turn.

Returns:

A task that represents the work queued to execute.

Applies to