TR EN DE
GitHub Repository mhrgl.com
Startseite / Integrationen / Microsoft Teams Direct Routing
💼 Enterprise Hybrid Telephony · Microsoft 365

💼 Microsoft Teams Direct Routing & SBC-Gateway-Integration

Verbinden Sie Ihre AiPBX Asterisk 22-Infrastruktur direkt mit dem Microsoft 365 Teams Phone System. Behalten Sie bestehende lokale SIP-Trunks ohne teure Microsoft Calling Plans und integrieren Sie Teams-Clients nahtlos in Nebenstellen und Warteschlangen.

1. Übersicht & Warum Direct Routing?

Microsoft Teams ist die führende Kollaborationsplattform, aber Microsoft Calling Plans sind teuer und in vielen Ländern stark reglementiert.

Mit dem AiPBX Direct Routing Gateway fungiert AiPBX als Session Border Controller (SBC) zwischen Microsoft 365 und Ihren lokalen Telekommunikationstrunks.

  • Freie Anbieterwahl: Behalten Sie bestehende SIP-Trunk-Verträge zu gewohnt günstigen Konditionen.
  • Bis zu 70% Kostensenkung: Keine teuren monatlichen Microsoft-Calling-Plan-Gebühren pro Benutzer.
  • Hybride Nebenstellen: Kostenlose interne Kurzwahlen zwischen Teams-Nutzern, Tischtelefonen, WebRTC und Mobil-Apps.
  • Vollwertige PBX-Funktionen: IVR-Sprachmenüs, ACD-Warteschlangen, Gesprächsaufzeichnung und digitaler Faxserver.
Funktionsmatrix Microsoft Native Calling Plan AiPBX Direct Routing (Empfohlen)
Monatliche Telekom-Kosten 12€ - 24€ pro Benutzer / Monat 0€ Extra (Nutzt bestehende SIP-Tarife)
Tischtelefone & Gegensprechanlage Nur teure Teams-zertifizierte Telefone Alle Standard-SIP-Telefone + WebRTC
Callcenter & Pausenerfassung Einfache Warteschlangen, keine Pausencodes Vollwertiges ACD, *22/*23 Pausencodes, Live-Wallboard & *90 Mithören
Faxempfang/-versand (T.38) Nicht unterstützt Integrierter T.38 & SpanDSP Faxserver

2. Architektur & Topologie

Microsoft Teams Direct Routing erfordert SIP über TLS (Port 5061) für die Signalisierung und SRTP (SDES-Verschlüsselung) für Audio. AiPBX fungiert als sicherer SBC-Edge-Knoten:

Direct Routing Netzwerk-Topologie TLS 1.2 · SRTP · Port 5061
┌─────────────────────────────────────────────────────────────────────────────┐
│                    MICROSOFT 365 CLOUD INFRASTRUCTURE                       │
│                                                                             │
│   [ Teams Desktop ]       [ Teams Mobile (iOS/Android) ]      [ Teams Web ]  │
│           │                               │                        │        │
│           └───────────────────────┬───────┴────────────────────────┘        │
│                                   ▼                                         │
│                 Microsoft Teams Phone System Core                           │
│                                   │                                         │
│                sip.pstnhub.microsoft.com (EU / Primary)                     │
│                sip2.pstnhub.microsoft.com (US / Secondary)                  │
│                sip3.pstnhub.microsoft.com (APAC / Tertiary)                 │
└───────────────────────────────────┬─────────────────────────────────────────┘
                                    │
                                    │  SIP over TLS (Port 5061)
                                    │  SRTP Media (UDP 10000-20000)
                                    │  Trusted CA SSL (Let's Encrypt / DigiCert)
                                    ▼
┌─────────────────────────────────────────────────────────────────────────────┐
│                    AiPBX SESSION BORDER CONTROLLER (SBC)                    │
│                                                                             │
│   FQDN: sbc.yourdomain.com / IP: 212.147.x.x                                │
│   ┌─────────────────────────────────────────────────────────────────────┐   │
│   │ Asterisk 22 PJSIP Signaling & Media Engine                          │   │
│   │  • TLS Transport (Port 5061) - Strict Cipher Suites (AES256/GCM)    │   │
│   │  • SDES-SRTP Audio Transcoding (SILK, Opus, G.711a, G.711u)         │   │
│   │  • Bi-directional SIP OPTIONS Heartbeat Monitoring (60s Keepalive)  │   │
│   │  • E.164 Inbound/Outbound Route Normalization & Dialplan            │   │
│   └──────────────────────────────────┬──────────────────────────────────┘   │
└──────────────────────────────────────┼──────────────────────────────────────┘
                                       │
           ┌───────────────────────────┼───────────────────────────┐
           ▼                           ▼                           ▼
┌─────────────────────┐     ┌─────────────────────┐     ┌─────────────────────┐
│  OFFICE EXTENSIONS  │     │  CALL CENTER & IVR  │     │   CARRIER TRUNKS    │
│  • Yealink/Fanvil   │     │  • ACD Queues       │     │  • Türk Telekom     │
│  • AiPBX WebRTC Web │     │  • *22/*23 Breaks   │     │  • Turkcell / Voda  │
│  • Android/iOS Apps │     │  • Recording & SLA  │     │  • Any SIP Provider │
└─────────────────────┘     └─────────────────────┘     └─────────────────────┘
💡
SIP OPTIONS Heartbeat-Mechanismus: Microsoft Teams und AiPBX senden alle 60 Sekunden SIP OPTIONS-Pings. Mit qualify_frequency=60 in Asterisk PJSIP wechselt der Status im Teams Admin Center auf "Aktiv".

3. Voraussetzungen & Lizenzen

Folgende Voraussetzungen müssen erfüllt sein:

  1. Microsoft 365 Lizenzen: Teams Phone Standard Zusatzlizenz oder Microsoft 365 E5 für jeden telefonierenden Benutzer.
  2. Verifizierter FQDN: Eine Subdomain (z.B. sbc.ihrefirma.de) unter einer im M365-Tenant verifizierten Domain.
  3. Öffentliches SSL-Zertifikat: Ausgestellt von einer vertrauenswürdigen Zertifizierungsstelle (DigiCert, Sectigo oder Let's Encrypt).
  4. Statische öffentliche IPv4-Adresse: Feste öffentliche IP-Adresse für AiPBX.

4. Netzwerk- & Firewall-Regeln

Öffnen Sie folgende Firewall-Ports für den Datenverkehr zwischen AiPBX und Microsoft 365:

Protokoll & Port Quelle / Ziel Verkehrstyp & Zweck
TCP 5061 52.114.0.0/16, 52.112.0.0/14 SIP über TLS: Signalisierung und OPTIONS-Keepalive mit MS PSTN Hub.
UDP 10000:20000 52.112.0.0/14, 52.120.0.0/14 SRTP-Audioströme: Verschlüsselte Sprachpakete zwischen Teams und PBX.
TCP 443 Genel / Any HTTPS-Webhooks & SSL: Certbot-Erneuerung und ausgehende Teams-Webhooks.
bash — Ubuntu-UFW-Firewall-Regeln
# 1. Microsoft Teams SIP TLS (port 5061) - Microsoft subnets
sudo ufw allow proto tcp from 52.114.0.0/16 to any port 5061 comment 'MS Teams SIP TLS Primary'
sudo ufw allow proto tcp from 52.112.0.0/14 to any port 5061 comment 'MS Teams SIP TLS Secondary'

# 2. Microsoft Teams SRTP voice media (UDP 10000-20000)
sudo ufw allow proto udp from 52.112.0.0/14 to any port 10000:20000 comment 'MS Teams SRTP Media'
sudo ufw allow proto udp from 52.120.0.0/14 to any port 10000:20000 comment 'MS Teams Media Relays'

# 3. Reload the firewall and review its status
sudo ufw reload
sudo ufw status numbered

5. Asterisk 22 & PJSIP Konfiguration

Asterisk 22 PJSIP erfüllt alle Vorgaben für Microsoft Teams Direct Routing (TLS 1.2+, SDES-SRTP und E.164). Fügen Sie folgende Abschnitte zu Ihrer Konfiguration hinzu:

A. /etc/asterisk/pjsip.conf — Trunk & Transport-Setup

ini — /etc/asterisk/pjsip.conf (Teams Direct Routing)
; ====================================================================
; 1. TLS TRANSPORT (PORT 5061 - Microsoft Teams SBC)
; ====================================================================
[transport-tls-msteams]
type=transport
protocol=tls
bind=0.0.0.0:5061
cert_file=/etc/letsencrypt/live/sbc.yourdomain.com/fullchain.pem
priv_key_file=/etc/letsencrypt/live/sbc.yourdomain.com/privkey.pem
method=tlsv1_2
cipher=ECDHE-RSA-AES256-GCM-SHA384,ECDHE-RSA-AES128-GCM-SHA256,ECDHE-RSA-AES256-SHA384
verify_client=no
verify_server=yes
require_client_cert=no

; ====================================================================
; 2. MS TEAMS ENDPOINT
; ====================================================================
[msteams-trunk]
type=endpoint
transport=transport-tls-msteams
context=from-msteams
disallow=all
allow=alaw,ulaw,opus,g722
direct_media=no
media_encryption=sdes
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
trust_id_inbound=yes
send_rpid=yes
send_pai=yes
timers=yes
timers_sess_expires=1800
aors=msteams-aor

; ====================================================================
; 3. MS TEAMS AOR & QUALIFY (OPTIONS heartbeat)
; ====================================================================
[msteams-aor]
type=aor
contact=sip:sip.pstnhub.microsoft.com:5061;transport=tls
qualify_frequency=60
qualify_timeout=3.0

; ====================================================================
; 4. IDENTIFY (match inbound calls to Microsoft IPs)
; ====================================================================
[msteams-identify]
type=identify
endpoint=msteams-trunk
match=52.114.148.0, 52.114.132.74, 52.114.75.24, 52.114.76.76, 52.114.7.24, 52.114.14.70, 52.114.16.74, 52.114.20.29, sip.pstnhub.microsoft.com, sip2.pstnhub.microsoft.com, sip3.pstnhub.microsoft.com

B. /etc/asterisk/extensions.conf — Wählplan-Routing

ini — /etc/asterisk/extensions.conf (from-msteams & to-msteams)
; ====================================================================
; 1. CALLS COMING FROM MICROSOFT TEAMS (from-msteams)
; ====================================================================
[from-msteams]
; Teams user dials a 3-4 digit extension (e.g. 105)
exten => _[1-9]XX,1,NoOp(Teams -> Extension: ${CALLERID(num)} -> ${EXTEN})
 same => n,Dial(PJSIP/${EXTEN},30,tTkK)
 same => n,Hangup()

exten => _[1-9]XXX,1,NoOp(Teams -> 4-digit Extension: ${CALLERID(num)} -> ${EXTEN})
 same => n,Dial(PJSIP/${EXTEN},30,tTkK)
 same => n,Hangup()

; Teams user calls the call-center queue (e.g. 800)
exten => 800,1,NoOp(Teams -> Support Queue)
 same => n,Answer()
 same => n,Queue(support_queue,tTkK,,,180)
 same => n,Hangup()

; Teams user dials an external PSTN number (05xx / 02xx / +90...)
exten => _0[2-5]XXXXXXXXX,1,NoOp(Teams -> Carrier Trunk Outbound: ${EXTEN})
 same => n,Dial(PJSIP/${EXTEN}@carrier-trunk,60,tTkK)
 same => n,Hangup()

; ====================================================================
; 2. CALLS FROM AIPBX EXTENSIONS TO TEAMS (to-msteams)
; Routes to the E.164 numbers assigned to Teams users (+90212XXXXXXX)
; ====================================================================
[to-msteams]
exten => _+.,1,NoOp(AiPBX -> Teams Direct Routing: ${EXTEN})
 same => n,Set(CALLERID(num)=+902129990000) ; your company main number
 same => n,Dial(PJSIP/${EXTEN}@msteams-trunk,60,tTkK)
 same => n,Hangup()

; Short code to reach a Teams user (e.g. 7105 -> +902129997105)
exten => _7XXX,1,NoOp(Extension -> Teams User 7${EXTEN:1})
 same => n,Dial(PJSIP/+90212999${EXTEN}@msteams-trunk,45,tTkK)
 same => n,Hangup()
bash — Asterisk-Konfiguration live neu laden
# Reload the Asterisk modules live
sudo asterisk -rx "pjsip reload"
sudo asterisk -rx "dialplan reload"

# Verify the Teams trunk status and OPTIONS heartbeat
sudo asterisk -rx "pjsip show endpoints"
sudo asterisk -rx "pjsip show aor msteams-aor"

6. Microsoft 365 PowerShell Konfiguration

Die Konfiguration in Microsoft 365 erfolgt über das offizielle MicrosoftTeams PowerShell-Modul mit Administratorrechten:

powershell — Microsoft Teams PowerShell-Konfiguration
# ====================================================================
# STEP 1: Install the module and connect to your Microsoft 365 tenant
# ====================================================================
Install-Module -Name MicrosoftTeams -Force -AllowClobber
Connect-MicrosoftTeams

# ====================================================================
# STEP 2: Register the AiPBX SBC gateway
# ====================================================================
New-CsOnlinePSTNGateway `
    -Fqdn "sbc.yourdomain.com" `
    -SipSignalingPort 5061 `
    -CodecPriorityList SILK,G711A,G711U `
    -MaxConcurrentSessions 100 `
    -Enabled $true

# ====================================================================
# STEP 3: Create the PSTN usage and voice route
# ====================================================================
# Define a PSTN usage tag
Set-CsOnlinePstnUsage -Identity Global -Usage @{Add="AiPBX-Local"}

# Voice route sending all outbound calls to the AiPBX SBC
New-CsOnlineVoiceRoute `
    -Name "AiPBX-PSTN-Route" `
    -NumberPattern ".*" `
    -OnlinePstnGatewayList "sbc.yourdomain.com" `
    -Priority 1 `
    -OnlinePstnUsages "AiPBX-Local"

# Create the voice routing policy
New-CsOnlineVoiceRoutingPolicy `
    -Identity "AiPBX-Voice-Policy" `
    -OnlinePstnUsages "AiPBX-Local" `
    -Description "AiPBX Direct Routing policy"

# ====================================================================
# STEP 4: Assign a number to the user and apply the policy
# ====================================================================
# Assign an E.164 phone number and enable Enterprise Voice
Set-CsPhoneNumberAssignment `
    -Identity "ahmet.yilmaz@yourdomain.com" `
    -PhoneNumber "+902129991001" `
    -PhoneNumberType DirectRouting

# Attach the AiPBX voice policy to the user
Grant-CsOnlineVoiceRoutingPolicy `
    -Identity "ahmet.yilmaz@yourdomain.com" `
    -PolicyName "AiPBX-Voice-Policy"
✅
Wähltastatur-Aktivierung: Die Synchronisation in Microsoft 365 dauert ca. 15-30 Minuten. Danach erscheint die Wähltastatur automatisch in der Teams-App unter "Anrufe".

7. Anrufszenarien & Enterprise-Workflows

Nach der Bereitstellung unterstützt AiPBX nahtlose hybride Telefonieszenarien:

1. Teams ➔ Tischtelefon / Nebenstelle

Die Wahl von 105 in Teams leitet den Anruf über TLS 5061 an AiPBX weiter und klingelt Tischtelefon und Mobile-App gleichzeitig.

2. PSTN eingehend ➔ IVR ➔ Teams

Anrufer wählt die Firmennummer, hört das IVR-Menü und wird direkt zum Mitarbeiter in Microsoft Teams weitergeleitet.

3. Teams ➔ Außenwelt (PSTN-Anrufe)

Teams-Benutzer ruft eine Mobilnummer an; der Anruf wird über den bestehenden Firmentrunk zu lokalen Tarifen geroutet.

4. Teams-Benutzer als Warteschlangen-Agenten

Teams-Benutzer können als Agenten in AiPBX-Warteschlangen aufgenommen werden und Kundenanrufe mit Supervisor-Unterstützung empfangen.

8. Teams-Webhooks & Kanal-Benachrichtigungen

Zusätzlich zum Voice-Routing sendet AiPBX formatierte Adaptive Cards über Webhooks in Microsoft Teams-Kanäle:

bash — Teams-Webhook für verpasste Anrufe (cURL & Adaptive Card)
curl -X POST -H "Content-Type: application/json" -d '{
  "type": "message",
  "attachments": [
    {
      "contentType": "application/vnd.microsoft.card.adaptive",
      "content": {
        "$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
        "type": "AdaptiveCard",
        "version": "1.4",
        "body": [
          {
            "type": "TextBlock",
            "text": "📞 AiPBX — Missed Call Alert",
            "weight": "Bolder",
            "size": "Medium",
            "color": "Attention"
          },
          {
            "type": "FactSet",
            "facts": [
              { "title": "Caller:", "value": "+90 532 123 45 67" },
              { "title": "Target Queue:", "value": "Support Queue (*800)" },
              { "title": "Wait Time:", "value": "48 seconds" },
              { "title": "Time:", "value": "2026-09-18 11:42:15" }
            ]
          }
        ],
        "actions": [
          {
            "type": "Action.OpenUrl",
            "title": "Open in AiPBX",
            "url": "https://aipbx.bid/cdr-reports"
          }
        ]
      }
    }
  ]
}' "https://yourtenant.webhook.office.com/webhookb2/..."

9. Fehlerbehebung & Diagnose

Häufige Probleme bei der Direct Routing-Einrichtung und deren Behebung:

Fehler / Symptom Ursache Lösung
SIP OPTIONS unbeantwortet / SBC down Port 5061 blockiert oder SSL-Zertifikatsname stimmt nicht überein. UFW-Regeln prüfen und FQDN im SSL-Zertifikat validieren.
488 Not Acceptable Here Codec-Konflikt oder fehlende SRTP-Verschlüsselung. media_encryption=sdes und G.711/Opus in PJSIP sicherstellen.
403 Forbidden Fehlende Voice Policy oder Rufnummernzuweisung in M365. Benutzerstatus mit Get-CsOnlineUser prüfen.
Einseitiges Audio UDP-Mediaports blockiert oder NAT-Adresse fehlt. external_media_address in PJSIP definieren und UDP-Ports öffnen.
bash — Live-SIP-Verkehr mitschneiden (Debug)
# Attach to the Asterisk console and enable the PJSIP SIP packet logger
sudo asterisk -rvvv
pjsip set logger on

# To capture only Microsoft Teams traffic, in a shell:
sudo tcpdump -n -i any port 5061 -vv