The Graph package for a Microsoft Teams agent
Project description
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))
await ctx.sign_in()
return
# Use the user token that's already available in the context
graph = get_graph_client(ctx.user_token)
# Make Graph API calls
me = await graph.me.get()
await ctx.send(f"Hello {me.display_name}!")
# Make Graph API calls
me = await graph.me.get()
await ctx.send(f"Hello {me.display_name}!")
## 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)
```python
# 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)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file microsoft_teams_graph-2.0.0a7.tar.gz.
File metadata
- Download URL: microsoft_teams_graph-2.0.0a7.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c5912a116a233e21cd36a30193a134d0d68043101b77657ab4cbc9a304c0cae
|
|
| MD5 |
287132369389c7d0ab3e3323fea6a462
|
|
| BLAKE2b-256 |
e18e13d6313ba866333a93c3a7b4f6dc808f26ffd14b56e33be56735c1717433
|
File details
Details for the file microsoft_teams_graph-2.0.0a7-py3-none-any.whl.
File metadata
- Download URL: microsoft_teams_graph-2.0.0a7-py3-none-any.whl
- Upload date:
- Size: 5.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: uv/0.9.16 {"installer":{"name":"uv","version":"0.9.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f2d7cc7e20876deebaadea6a498c802112d3c1ee6ae87a2019303cd60789d09
|
|
| MD5 |
3a463b2e08ed703f51c233d39a654dea
|
|
| BLAKE2b-256 |
ed9217f057f72c2f553317ce772817fbaef8d2c418f9196b4bfc46c4e6a41383
|