Skip to content

Add Teams message history API#491

Draft
heyitsaamir wants to merge 13 commits into
microsoft:mainfrom
heyitsaamir:heyitsaamir-add-history-api
Draft

Add Teams message history API#491
heyitsaamir wants to merge 13 commits into
microsoft:mainfrom
heyitsaamir:heyitsaamir-add-history-api

Conversation

@heyitsaamir

@heyitsaamir heyitsaamir commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Summary

Adds a Microsoft Graph-backed Teams message history API for context-level and app-level callers, plus a focused example app for manual testing. History now returns both the messages and a serializable source that can be reused for future reads.

API

Reactive context API

history = await ctx.get_history(10)
messages = history.messages
source = history.source

ctx.get_history(n) infers the current scope from the inbound activity and returns MessageHistory(messages, source):

  • 1:1 chat / group chat: calls Graph GET /chats/{chat-id}/messages
  • Team channel: calls Graph GET /teams/{team-aad-group-id}/channels/{channel-id}/messages
  • Channel thread: detects reply_to_id or ;messageid=<root> and calls .../messages/{root-message-id}/replies

For channel history, the SDK requires the Teams activity to include team.aad_group_id; this is the Graph team resource ID used by RSC.

App-level API

history = await app.get_history(n=10, source=source)

app.get_history intentionally takes a HistorySource instead of individual IDs. Callers can pass history.source from a previous ctx.get_history(...) result or construct one of the exported source models directly:

source = ChannelHistorySource(
    team_aad_group_id=team_aad_group_id,
    channel_id=channel_id,
    thread_id=message_id,
)
history = await app.get_history(n=10, source=source)

Available source models:

  • OneOnOneHistorySource(chat_id=...)
  • GroupChatHistorySource(chat_id=...)
  • ChannelHistorySource(team_aad_group_id=..., channel_id=..., thread_id=None)

HistorySource is a Pydantic discriminated union, so sources can be persisted with source.model_dump(by_alias=True) and restored with TypeAdapter(HistorySource).validate_python(payload).

The helper returns the latest n messages for the selected source:

  • Chat/group chat requests use Graph $orderby=createdDateTime desc with $top.
  • Channel root messages use Graph's channel ordering, which is by last modified date of the entire reply chain.
  • Channel thread replies only support $top; the SDK pages through replies and returns the last n by created_date_time when timestamps are available.

Graph errors propagate unchanged.

Caveats

  • 1:1 chat history depends on service-side support that is expected to be picked up very soon by the service team.

Example

Adds examples/history with commands to test:

  • history / history ctx <n>
  • history chat <chat-id> [n]
  • history channel <team-aad-group-id> <channel-id> [n]
  • history thread <team-aad-group-id> <channel-id> <thread-id> [n]

heyitsaamir and others added 13 commits June 30, 2026 16:25
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant