BotAdapter Class

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

public abstract class BotAdapter

Represents a bot adapter that can connect a bot to a service endpoint. This class is abstract.

The bot adapter encapsulates authentication processes and sends activities to and receives activities from the Bot Connector Service. When your bot receives an activity, the adapter creates a context object, passes it to your bot's application logic, and sends responses back to the user's channel.

Use use(Middleware middleware) to add Middleware objects to your adapter\u2019s middleware collection. The adapter processes and directs incoming activities in through the bot middleware pipeline to your bot\u2019s logic and then back out again. As each activity flows in and out of the bot, each piece of middleware can inspect or act upon the activity, both before and after the bot logic runs.

TurnContext Activity Bot Middleware

Field Summary

Modifier and Type Field and Description
static final java.lang.String BOT_IDENTITY_KEY

Key to store bot claims identity.

static final java.lang.String OAUTH_CLIENT_KEY

Key to store bot oauth client.

static final java.lang.String OAUTH_SCOPE_KEY

Key to store bot oauth scope.

Constructor Summary

Constructor Description
BotAdapter()

Method Summary

Modifier and Type Method and Description
java.util.concurrent.CompletableFuture<java.lang.Void> continueConversation(ClaimsIdentity claimsIdentity, Activity continuationActivity, BotCallbackHandler callback)

Sends a proactive message to a conversation.

java.util.concurrent.CompletableFuture<java.lang.Void> continueConversation(ClaimsIdentity claimsIdentity, Activity continuationActivity, String audience, BotCallbackHandler callback)

Sends a proactive message to a conversation.

java.util.concurrent.CompletableFuture<java.lang.Void> continueConversation(ClaimsIdentity claimsIdentity, ConversationReference reference, BotCallbackHandler callback)

Sends a proactive message to a conversation.

java.util.concurrent.CompletableFuture<java.lang.Void> continueConversation(ClaimsIdentity claimsIdentity, ConversationReference reference, String audience, BotCallbackHandler callback)

Sends a proactive message to a conversation.

java.util.concurrent.CompletableFuture<java.lang.Void> continueConversation(String botId, Activity continuationActivity, BotCallbackHandler callback)

Sends a proactive message to a conversation.

java.util.concurrent.CompletableFuture<java.lang.Void> continueConversation(String botAppId, ConversationReference reference, BotCallbackHandler callback)

Sends a proactive message to a conversation.

abstract java.util.concurrent.CompletableFuture<java.lang.Void> deleteActivity(TurnContext context, ConversationReference reference)

When overridden in a derived class, deletes an existing activity in the conversation.

protected MiddlewareSet getMiddlewareSet()

Gets the collection of middleware in the adapter's pipeline.

OnTurnErrorHandler getOnTurnError()

Gets the error handler that can catch exceptions in the middleware or application.

protected java.util.concurrent.CompletableFuture<java.lang.Void> runPipeline(TurnContext context, BotCallbackHandler callback)

Starts activity processing for the current bot turn.

abstract java.util.concurrent.CompletableFuture<ResourceResponse[]> sendActivities(TurnContext context, List<Activity> activities)

When overridden in a derived class, sends activities to the conversation.

void setOnTurnError(OnTurnErrorHandler withTurnError)

Sets the error handler that can catch exceptions in the middleware or application.

abstract java.util.concurrent.CompletableFuture<ResourceResponse> updateActivity(TurnContext context, Activity activity)

When overridden in a derived class, replaces an existing activity in the conversation.

BotAdapter use(Middleware middleware)

Adds middleware to the adapter's pipeline.

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

Field Details

BOT_IDENTITY_KEY

public static final String BOT_IDENTITY_KEY

Key to store bot claims identity.

OAUTH_CLIENT_KEY

public static final String OAUTH_CLIENT_KEY

Key to store bot oauth client.

OAUTH_SCOPE_KEY

public static final String OAUTH_SCOPE_KEY

Key to store bot oauth scope.

Constructor Details

BotAdapter

public BotAdapter()

Method Details

continueConversation

public CompletableFuture continueConversation(ClaimsIdentity claimsIdentity, Activity continuationActivity, BotCallbackHandler callback)

Sends a proactive message to a conversation.

Call this method to proactively send a message to a conversation. Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user.

Parameters:

claimsIdentity - A ClaimsIdentity for the conversation.
continuationActivity - An Activity with the appropriate ConversationReference with which to continue the conversation.
callback - The method to call for the result bot turn.

Returns:

A task that represents the work queued to execute.

continueConversation

public CompletableFuture continueConversation(ClaimsIdentity claimsIdentity, Activity continuationActivity, String audience, BotCallbackHandler callback)

Sends a proactive message to a conversation.

Call this method to proactively send a message to a conversation. Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user.

Parameters:

claimsIdentity - A ClaimsIdentity for the conversation.
continuationActivity - An Activity with the appropriate ConversationReference with which to continue the conversation.
audience - A value signifying the recipient of the proactive message.
callback - The method to call for the result bot turn.

Returns:

A task that represents the work queued to execute.

continueConversation

public CompletableFuture continueConversation(ClaimsIdentity claimsIdentity, ConversationReference reference, BotCallbackHandler callback)

Sends a proactive message to a conversation.

Call this method to proactively send a message to a conversation. Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user.

Parameters:

claimsIdentity - A ClaimsIdentity reference for the conversation.
reference - A reference to the conversation to continue.
callback - The method to call for the result bot turn.

Returns:

A task that represents the work queued to execute.

continueConversation

public CompletableFuture continueConversation(ClaimsIdentity claimsIdentity, ConversationReference reference, String audience, BotCallbackHandler callback)

Sends a proactive message to a conversation.

Call this method to proactively send a message to a conversation. Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user.

Parameters:

claimsIdentity - A ClaimsIdentity reference for the conversation.
reference - A reference to the conversation to continue.
audience - A value signifying the recipient of the proactive message.
callback - The method to call for the result bot turn.

Returns:

A task that represents the work queued to execute.

continueConversation

public CompletableFuture continueConversation(String botId, Activity continuationActivity, BotCallbackHandler callback)

Sends a proactive message to a conversation.

Call this method to proactively send a message to a conversation. Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user.

Parameters:

botId - The application ID of the bot. This parameter is ignored in single tenant the Adapters (Console, Test, etc) but is critical to the BotFrameworkAdapter which is multi-tenant aware.
continuationActivity - An Activity with the appropriate ConversationReference with which to continue the conversation.
callback - The method to call for the result bot turn.

Returns:

A task that represents the work queued to execute.

continueConversation

public CompletableFuture continueConversation(String botAppId, ConversationReference reference, BotCallbackHandler callback)

Sends a proactive message to a conversation.

Parameters:

botAppId - The application ID of the bot. This parameter is ignored in single tenant the Adapters (Console, Test, etc) but is critical to the BotFrameworkAdapter which is multi-tenant aware.
reference - A reference to the conversation to continue.
callback - The method to call for the resulting bot turn.

Returns:

A task that represents the work queued to execute. Call this method to proactively send a message to a conversation. Most channels require a user to initiate a conversation with a bot before the bot can send activities to the user.
     <xref uid="com.microsoft.bot.builder.BotAdapter.runPipeline(com.microsoft.bot.builder.TurnContext,com.microsoft.bot.builder.BotCallbackHandler)" data-throw-if-not-resolved="false" data-raw-source="#runPipeline(TurnContext, BotCallbackHandler)"></xref>

deleteActivity

public abstract CompletableFuture deleteActivity(TurnContext context, ConversationReference reference)

When overridden in a derived class, deletes an existing activity in the conversation.

Parameters:

context - The context object for the turn.
reference - Conversation reference for the activity to delete.

Returns:

A task that represents the work queued to execute. The ConversationReference#getActivityId of the conversation reference identifies the activity to delete. onDeleteActivity(DeleteActivityHandler handler)

getMiddlewareSet

protected MiddlewareSet getMiddlewareSet()

Gets the collection of middleware in the adapter's pipeline.

Returns:

The middleware collection for the pipeline.

getOnTurnError

public OnTurnErrorHandler getOnTurnError()

Gets the error handler that can catch exceptions in the middleware or application.

Returns:

An error handler that can catch exceptions in the middleware or application.

runPipeline

protected CompletableFuture runPipeline(TurnContext context, BotCallbackHandler callback)

Starts activity processing for the current bot turn. The adapter calls middleware in the order in which you added it. The adapter passes in the context object for the turn and a next delegate, and the middleware calls the delegate to pass control to the next middleware in the pipeline. Once control reaches the end of the pipeline, the adapter calls the callback method. If a middleware component does not call the next delegate, the adapter does not call any of the subsequent middleware\u2019s onTurn(TurnContext turnContext, NextDelegate next) methods or the callback method, and the pipeline short circuits.

When the turn is initiated by a user activity (reactive messaging), the callback method will be a reference to the bot's onTurn(TurnContext turnContext) method. When the turn is initiated by a call to continueConversation(String botAppId, ConversationReference reference, BotCallbackHandler callback) (proactive messaging), the callback method is the callback method that was provided in the call.

Parameters:

context - The turn's context object.
callback - A callback method to run at the end of the pipeline.

Returns:

A task that represents the work queued to execute.

sendActivities

public abstract CompletableFuture sendActivities(TurnContext context, List activities)

When overridden in a derived class, sends activities to the conversation.

Parameters:

context - The context object for the turn.
activities - The activities to send.

Returns:

A task that represents the work queued to execute. If the activities are successfully sent, the task result contains an array of ResourceResponse objects containing the IDs that the receiving channel assigned to the activities. onSendActivities(SendActivitiesHandler handler)

setOnTurnError

public void setOnTurnError(OnTurnErrorHandler withTurnError)

Sets the error handler that can catch exceptions in the middleware or application.

Parameters:

withTurnError - An error handler that can catch exceptions in the middleware or application.

updateActivity

public abstract CompletableFuture updateActivity(TurnContext context, Activity activity)

When overridden in a derived class, replaces an existing activity in the conversation.

Parameters:

context - The context object for the turn.
activity - New replacement activity.

Returns:

A task that represents the work queued to execute. If the activity is successfully sent, the task result contains a ResourceResponse object containing the ID that the receiving channel assigned to the activity.

Before calling this, set the ID of the replacement activity to the ID of the activity to replace. onUpdateActivity(UpdateActivityHandler handler)

use

public BotAdapter use(Middleware middleware)

Adds middleware to the adapter's pipeline.

Parameters:

middleware - The middleware to add.

Returns:

The updated adapter object. Middleware is added to the adapter at initialization time. For each turn, the adapter calls middleware in the order in which you added it.

Applies to