Backend & AI Application Engineer Guide: Enterprise Microsoft Teams Bots, Graph API & ChatOps
Enterprise engineering and operations teams live inside Microsoft Teams, yet many internal incident bots and approval workflows are fragile webhook scripts without Entra ID (Azure AD) token verification, Adaptive Card state management, or RBAC audit trails. As a backend engineer and AI application engineer, I build enterprise-grade Microsoft Teams ChatOps bots and Microsoft Graph API integrations with cryptographic JWT validation, interactive Adaptive Cards, and audited action execution.
1. Verifying Bot Framework JWTs and Tenant Allowlists
An HTTP endpoint receiving activities from Microsoft Teams must validate the incoming Bearer token against Microsoft's OpenID Connect metadata keys and restrict execution to your enterprise tenant ID (tid) before parsing any command:
# chatops/teams_auth.py
import jwt
from jwt import PyJWKClient
from django.core.exceptions import PermissionDenied
BOT_OPENID_JWKS_URI = "https://login.botframework.com/v1/.well-known/keys"
jwk_client = PyJWKClient(BOT_OPENID_JWKS_URI, cache_keys=True)
def verify_teams_activity_token(auth_header: str, expected_app_id: str, allowed_tenant_id: str) -> dict:
if not auth_header.startswith("Bearer "):
raise PermissionDenied("Missing Bearer token")
token = auth_header.removeprefix("Bearer ").strip()
signing_key = jwk_client.get_signing_key_from_jwt(token)
claims = jwt.decode(
token,
signing_key.key,
algorithms=["RS256"],
audience=expected_app_id,
issuer="https://api.botframework.com",
)
if claims.get("tid") != allowed_tenant_id:
raise PermissionDenied("Unauthorized Microsoft 365 tenant")
return claims
2. Interactive Adaptive Cards with Idempotent Approval Actions
When an on-call engineer clicks Approve Production Rollback or Grant Temporary DB Read Access on an Adaptive Card (`Action.Execute`), the bot validates the user's Azure AD Object ID (`aadObjectId`) against an RBAC policy, records the approval in PostgreSQL, and updates the existing card in place via `PUT /v3/conversations/{conversationId}/activities/{activityId}` so no second engineer can click the button twice:
def build_completed_approval_card(request_id: str, approver_name: str, status: str) -> dict:
return {
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"type": "AdaptiveCard",
"version": "1.5",
"body": [
{"type": "TextBlock", "text": f"Deployment Request #{request_id}", "weight": "Bolder", "size": "Medium"},
{"type": "TextBlock", "text": f"Status: {status} by {approver_name}", "color": "Good" if status == "APPROVED" else "Attention"},
],
}