Skip to main content

Microsoft Teams Graph Integration

This package provides seamless access to Microsoft Graph APIs from Teams bots and agents built with the Microsoft Teams SDK for Python.

Key Features

  • Token Integration: Unified token handling using the Token type from microsoft-teams-common
  • Flexible Token Sources: Supports strings, StringLike objects, callables, async callables, or None
  • Automatic Token Resolution: Leverages common resolve_token utility for consistent handling

Requirements

  • Teams SDK for Python
  • Microsoft Graph SDK for Python (msgraph-sdk)
  • Azure Core library (azure-core)
  • Microsoft Teams Common library (microsoft-teams-common)

Quick Start

Basic Usage with Teams Bot

from microsoft_teams.graph import get_graph_client
from microsoft_teams.apps import App, ActivityContext
from microsoft_teams.api import MessageActivity

app = App()

@app.on_message
async def handle_message(ctx: ActivityContext[MessageActivity]):
    if not ctx.is_signed_in:
        await ctx.sign_in()
        return

    # Create Graph client using user's token
    graph = get_graph_client(ctx.user_token)

    # Get user profile
    me = await graph.me.get()
    await ctx.send(f"Hello {me.display_name}!")

    # Get user's Teams
    teams = await graph.me.joined_teams.get()
    if teams and teams.value:
        team_names = [team.display_name for team in teams.value]
        await ctx.send(f"You're in {len(team_names)} teams: {', '.join(team_names)}")

Token Integration

from microsoft_teams.common.http.client_token import Token

def create_token_callable(ctx: ActivityContext) -> Token:
    """Create a callable token that refreshes automatically."""
    def get_fresh_token():
        # This is called on each Graph API request
        return ctx.user_token  # Always returns current valid token

    return get_fresh_token

# Use with Graph client
graph = get_graph_client(create_token_callable(ctx))

# Or, use the user token that's already available in the context
graph = get_graph_client(ctx.user_token)

Token Type Usage

The package uses the Token type from microsoft-teams-common for flexible token handling. You can provide tokens in several formats:

String Token (Simplest)

# Direct string token
graph = get_graph_client("eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIs...")

Callable Token (Dynamic)

def get_token():
    """Callable that returns a string token."""
    # Get your access token from wherever (Teams API, cache, etc.)
    return get_access_token_from_somewhere()

# Use the callable with get_graph_client
graph = get_graph_client(get_token)

Async Callable Token

async def get_token_async():
    """Async callable that returns a string token."""
    # Fetch token asynchronously
    token_response = await some_api_call()
    return token_response.access_token

graph = get_graph_client(get_token_async)

Dynamic Token Retrieval

def get_fresh_token():
    """Callable that fetches a fresh token on each invocation."""
    # This will be called each time the Graph client needs a token
    fresh_token = fetch_latest_token_from_api()
    return fresh_token

graph = get_graph_client(get_fresh_token)

Authentication

The package uses Token-based authentication with automatic resolution through the common library. Teams tokens are pre-authorized through the OAuth connection configured in your Azure Bot registration.

API Usage Examples

# Get user profile
me = await graph.me.get()

# Get recent emails with specific fields
from msgraph.generated.users.item.messages.messages_request_builder import MessagesRequestBuilder

query_params = MessagesRequestBuilder.MessagesRequestBuilderGetQueryParameters(
    select=["subject", "from", "receivedDateTime"],
    top=5
)
request_config = MessagesRequestBuilder.MessagesRequestBuilderGetRequestConfiguration(
    query_parameters=query_params
)
messages = await graph.me.messages.get(request_configuration=request_config)

Metadata

Release files for microsoft-teams-graph 2.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for microsoft-teams-graph 2.2.0
File Size Uploaded Provenance Actions
microsoft_teams_graph-2.2.0.tar.gz 5.2 kB Without provenance Details for microsoft_teams_graph-2.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for microsoft-teams-graph 2.2.0
File Interpreter ABI Platform Actions
microsoft_teams_graph-2.2.0-py3-none-any.whl Python 3 none any Details for microsoft_teams_graph-2.2.0-py3-none-any.whl

Total release size: 11.1 kB

Release files / microsoft_teams_graph-2.2.0.tar.gz

Download URL microsoft_teams_graph-2.2.0.tar.gz
Size 5.2 kB
Tags Source
SHA-256 checksum
How to use checksums
cdb238eb6507eec0bd62ac489fe24e9412a56ba625e7309cdd80a02147dfccc8
BLAKE2b-256 checksum
How to use checksums
968a3a0700e96a4a9de8e67e3efa82c9dc0c2063506253e9294ad00a08da544b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Release files / microsoft_teams_graph-2.2.0-py3-none-any.whl

Download URL microsoft_teams_graph-2.2.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d5917a2b81c797a16d813712503a9923059f7f05bbff7d5fbf94e2041c8e9c62
BLAKE2b-256 checksum
How to use checksums
b833a8cafd3d4a3a46276208af4d334844c122451588fabfd6af620e415005b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via RestSharp/106.13.0.0

Security for microsoft-teams-graph 2.2.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.2.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page