如何订阅O365中全公司范围的MS Teams语音来电事件?
Hey there! I get your frustration—call bots aren't the right fit here, and it's easy to go down the wrong search rabbit hole. Let's walk through the practical, bot-free ways you can trigger backend logic when your employees receive external calls in Teams.
1. Microsoft Graph API: Subscribe to Real-Time Call Events
This is the most flexible, code-first approach. You can use the Graph API to set up webhook subscriptions for Teams call state changes, then have your backend service react to those notifications.
Target Events:
- Use the
communications/callsendpoint to subscribe tocallUpdatedevents—this triggers when a call's state changes (like when an employee answers an incoming call). - Filter for external calls by checking the
participantsarray in the event payload: external callers will have an identity type ofguestorunknown.
- Use the
Setup Steps:
- Register an app in Azure AD with required permissions:
Calls.Read.All(for read-only access) orCalls.ReadWrite.All(if you need to modify call data later)—admin consent is mandatory here. - Build a webhook endpoint in your backend service that can accept POST requests from the Graph API.
- Use the Graph API to create a subscription, specifying the resource (e.g.,
/communications/calls), your webhook URL, and the event types you care about. - In your webhook handler, parse the incoming payload to confirm it's an external answered call, then execute your business logic.
- Register an app in Azure AD with required permissions:
2. Power Automate (Low-Code Alternative)
If you don't want to build a full backend from scratch, Power Automate has pre-built connectors for Teams Phone System that can trigger workflows without writing custom code.
- How to Configure:
- Create a new flow with the trigger "When a call is received (Teams Phone System)".
- Add a condition to filter for external calls: check if the "Caller type" field equals "External".
- Add an action to call your backend API, run a script, or perform any other required task.
- Save and enable the flow—Power Automate will handle event detection and trigger your logic automatically.
3. Azure Event Grid Integration
If you're already using Azure services, Teams Phone System can send call events directly to Azure Event Grid, which your backend can subscribe to for scalable event handling.
- Setup Overview:
- Create an Event Grid Topic in the Azure Portal.
- Link your O365 tenant to Azure and configure Teams Phone System to send call events to this topic.
- Create an Event Grid Subscription pointing to your backend service (e.g., an Azure Function, Logic App, or custom API).
- In your backend, process the incoming Event Grid payload to identify external calls and run your business logic.
Key Considerations
- Permissions: All these methods require admin consent in your O365 tenant, as you're accessing sensitive call data. Stick to the least privileged permissions needed for your use case.
- Event Timing: Verify that your trigger fires when the call is answered, not just when it rings—adjust the event filters in Graph API or Power Automate to match this.
- Testing: Use the Graph API's test tools or Power Automate's test mode to simulate incoming external calls and confirm your setup works as expected.
内容的提问来源于stack exchange,提问作者Frébo

