Documentation - v1.6.0-beta.24
    Preparing search index...

    Class Proactive<TState>

    Provides methods for storing, retrieving, and managing conversation references to enable proactive messaging scenarios. Supports sending activities and continuing conversations outside the standard request/response flow using stored conversation references.

    Use the Proactive class to implement scenarios where an agent needs to initiate conversations or send messages to users without an incoming activity, such as notifications or scheduled alerts. Some operations require that conversation references be stored using storeConversation before they can be used.

    Access via app.proactive after configuring proactive in AgentApplicationOptions.

    Type Parameters

    Index

    Constructors

    Properties

    ContinueConversationValueType: "application/vnd.microsoft.activity.continueconversation+json" = 'application/vnd.microsoft.activity.continueconversation+json'

    activity.valueType that indicates additional key/values for the ContinueConversation event.

    Methods

    • Continues a stored conversation by executing the given handler within the context of that conversation, looking it up by ID.

      See the Conversation overload for full details.

      Parameters

      • adapter: BaseAdapter

        The channel adapter used to continue the conversation.

      • conversationId: string

        The ID of a conversation previously stored via storeConversation.

      • handler: RouteHandler<TState>

        The route handler to execute within the continued conversation context.

      • OptionalautoSignInHandlers: string[]

        Optional list of OAuth connection names whose tokens should be acquired before invoking the handler.

      • OptionalcontinuationActivity: Partial<Activity>

        Optional activity fields merged into the continuation activity, making them available on ctx.activity inside the handler (e.g. value, valueType).

      Returns Promise<void>

      Error if no conversation reference is found for the specified ID.

      // Scheduled job: send a daily digest to all stored conversations
      for (const convId of storedIds) {
      await app.proactive.continueConversation(adapter, convId, async (ctx, state) => {
      await ctx.sendActivity('Here is your daily digest...')
      })
      }
    • Continues an existing conversation by executing the given handler within the context of the provided Conversation reference. The handler receives a TurnContext and a freshly loaded TurnState scoped to the original conversation, enabling the agent to respond as if replying to an incoming activity.

      Parameters

      • adapter: BaseAdapter

        The channel adapter used to continue the conversation.

      • conversation: Conversation

        A Conversation instance created via its constructor or ConversationBuilder.

      • handler: RouteHandler<TState>

        The route handler to execute within the continued conversation context.

      • OptionalautoSignInHandlers: string[]

        Optional list of OAuth connection names whose tokens should be acquired before invoking the handler.

      • OptionalcontinuationActivity: Partial<Activity>

        Optional activity fields merged into the continuation activity, making them available on ctx.activity inside the handler (e.g. value, valueType).

      Returns Promise<void>

      Exceptions thrown inside the handler are captured and re-thrown after the adapter callback completes, since the adapter would otherwise silently swallow them.

      If autoSignInHandlers are supplied and the application has user authorization configured, tokens are acquired before the handler is called. If not all tokens are available and proactiveOptions.failOnUnsignedInConnections is not false, an error is thrown.

      // Continue a conversation with a custom value payload
      const conv = await app.proactive.getConversationOrThrow(convId)
      await app.proactive.continueConversation(
      adapter,
      conv,
      async (ctx, state) => {
      const payload = ctx.activity.value as { alertType: string }
      await ctx.sendActivity(`Alert triggered: ${payload.alertType}`)
      },
      undefined,
      { value: { alertType: 'threshold-exceeded' }, valueType: Proactive.ContinueConversationValueType }
      )
    • Creates a new conversation using the specified channel adapter and conversation options.

      Parameters

      • adapter: BaseAdapter

        The channel adapter used to create the conversation. Must implement createConversationAsync() (i.e. a CloudAdapter instance).

      • createOptions: CreateConversationOptions

        Details required to create the conversation, including identity, channel, service URL, OAuth scope, and ConversationParameters. Build with CreateConversationOptionsBuilder.

      • Optionalhandler: RouteHandler<TState>

        Optional route handler executed immediately after the conversation is created.

      Returns Promise<Conversation>

      The newly created Conversation.

      This wraps CloudAdapter.createConversationAsync(), which requires real network connectivity and authentication. The provided adapter must implement createConversationAsync(); a TypeError is thrown if it does not.

      If createOptions.storeConversation is true, the resulting Conversation is automatically stored via storeConversation so it can be retrieved by ID later.

      If a handler is provided it is executed within the newly created conversation, giving the agent a chance to send an initial message or load state.

      TypeError if the adapter does not implement createConversationAsync().

      // Initiate a new 1:1 conversation with a Teams user and send a welcome message
      const opts = CreateConversationOptionsBuilder
      .create(process.env.APP_ID!, 'msteams')
      .withUser('user-aad-object-id')
      .withTenantId('tenant-id')
      .storeConversation(true)
      .build()

      const conv = await app.proactive.createConversation(adapter, opts, async (ctx, state) => {
      await ctx.sendActivity('Hi! I have an update for you.')
      })
      console.log('New conversation ID:', conv.reference.conversation.id)
    • Deletes the stored conversation reference for the given conversation ID.

      Parameters

      • conversationId: string

        The unique identifier of the conversation whose reference should be deleted.

      Returns Promise<void>

      If no record exists for the given ID, no action is taken.

      // Clean up after a conversation has ended
      app.onActivity('endOfConversation', async (ctx, state) => {
      await app.proactive.deleteConversation(ctx.activity.conversation.id)
      })
    • Retrieves the stored Conversation associated with the given conversation ID.

      Parameters

      • conversationId: string

        The unique identifier of the conversation to retrieve.

      Returns Promise<Conversation | undefined>

      The stored Conversation, or undefined if no record exists for that ID.

      const conv = await app.proactive.getConversation(convId)
      if (conv) {
      await app.proactive.sendActivity(adapter, conv, { text: 'Hello!' })
      }
    • Retrieves the stored Conversation for the given ID, throwing if no record is found.

      Parameters

      • conversationId: string

        The unique identifier of the conversation to retrieve.

      Returns Promise<Conversation>

      The stored Conversation.

      Error if no conversation reference is found for the specified ID.

      // Use when absence of the conversation should be treated as an error
      const conv = await app.proactive.getConversationOrThrow(convId)
      await app.proactive.sendActivity(adapter, conv, { text: 'Alert: your report is ready.' })
    • Sends an activity to a stored conversation, looking it up by ID.

      Parameters

      • adapter: BaseAdapter

        The channel adapter used to send the activity.

      • conversationId: string

        The ID of a conversation previously stored via storeConversation.

      • activity: Partial<Activity>

        The activity to send. If type is not set it defaults to 'message'.

      Returns Promise<ResourceResponse>

      A ResourceResponse with the ID of the sent activity.

      Error if no conversation reference is found for the specified ID.

      // Send a notification using a previously stored conversation ID
      await app.proactive.sendActivity(adapter, storedConvId, { text: 'Your order has shipped!' })
    • Sends an activity to an existing conversation using the provided Conversation reference.

      Parameters

      • adapter: BaseAdapter

        The channel adapter used to send the activity.

      • conversation: Conversation

        A Conversation instance created via its constructor or ConversationBuilder.

      • activity: Partial<Activity>

        The activity to send. If type is not set it defaults to 'message'.

      Returns Promise<ResourceResponse>

      A ResourceResponse with the ID of the sent activity.

      // Build a Conversation from a stored reference and send a message
      const conv = await app.proactive.getConversationOrThrow(convId)
      const response = await app.proactive.sendActivity(adapter, conv, { text: 'Hello from the agent!' })
      console.log('Sent activity ID:', response.id)
    • Stores the current conversation reference from a live TurnContext in proactive storage.

      Parameters

      • context: TurnContext

        The context object for the current turn, containing activity and conversation information.

      Returns Promise<string>

      The conversation ID that can be used to retrieve the reference in future operations.

      // Inside an onMessage handler — save the conversation so we can message later
      app.onActivity('message', async (ctx, state) => {
      const convId = await app.proactive.storeConversation(ctx)
      await ctx.sendActivity(`Conversation stored. ID: ${convId}`)
      })
    • Stores an explicit Conversation object in proactive storage.

      Parameters

      • conversation: Conversation

        The conversation reference record to store.

      Returns Promise<string>

      The conversation ID that can be used to retrieve the reference in future operations.

      If the conversation fails validation (missing conversation.id, serviceUrl, or claims.aud).

      // Build a Conversation manually and store it
      const conv = ConversationBuilder
      .create('my-app-id', 'msteams')
      .withUser('user-aad-id')
      .withConversationId('19:existing-thread-id@thread.tacv2')
      .build()
      const convId = await app.proactive.storeConversation(conv)