Slack Webhook Notifications
Slack Webhook Notifications
Send a Slack message whenever an inbound call arrives, showing the caller IDThe calling party number (and optionally name) presented on an outbound call, set from the endpoint or manipulated in the dialplan. and which DID was dialed. Uses a Gosub subroutine so any inbound contextA named section of the dialplan that groups extensions. Calls enter a specific context and can only reach extensions visible from it, making contexts the basic unit of call routing and security. can call it with one line.
Requirements
- A Slack incoming webhook URL (create one at Slack API)
curlinstalled on the Asterisk serverfunc_filter.somodule loaded (for FILTER())
Global Variables
[globals]
SLACK_WEBHOOK = https://hooks.slack.com/services/XXXXXXXXX/YYYYYYYYYYY/ZZZZZZZZZZZZZZZZZZZZZZZZ
Subroutine
[slack-notify]
; Sends incoming call notification to Slack
; ARG1 = destination name (e.g., "Sales", "Support")
; ARG2 = DID number (e.g., "508-555-1234")
;
; Caller ID is sanitized before passing to System() to prevent
; shell injection and JSON breakage from crafted caller ID data.
exten => s,1,Set(SAFE_NAME=${FILTER(A-Za-z0-9 .,${CALLERID(name)})})
same => n,Set(SAFE_NUM=${FILTER(0-9+,${CALLERID(num)})})
same => n,Set(SLACK_MSG={"text":"Incoming call to *${ARG1}* (${ARG2})\nFrom: ${SAFE_NAME} <${SAFE_NUM}>"})
same => n,System(curl -s -X POST -H 'Content-type: application/json' --data '${SLACK_MSG}' ${SLACK_WEBHOOK} &)
same => n,Return()
Usage in Inbound Context
[from-trunk]
exten => 15085551234,1,NoOp(Inbound to Sales DID)
same => n,Gosub(slack-notify,s,1(Sales,508-555-1234))
same => n,Goto(ring-sales,s,1)
How it works
- FILTER() for security: Caller ID name and number are attacker-controlled data. A crafted caller ID like
"; rm -rf /; "would break the JSON and could execute arbitrary commands viaSystem().FILTER(A-Za-z0-9 .,...)strips everything except alphanumerics, spaces, and periods.FILTER(0-9+,...)keeps only digits and+. - System() with background: The trailing
&runs curl in the background so the call is not delayed waiting for the HTTP request to complete. - Gosub/Return pattern: Unlike
Macro()(deprecated),Gosub()is the recommended way to create reusable subroutines. Arguments are accessed as${ARG1},${ARG2}, etc.Return()passes control back to the calling context. - JSON construction: The Slack message uses Slack's
mrkdwnformat.*bold*for the DID name and\nfor line breaks.
Tips
- For Microsoft Teams, replace the webhook URL and adjust the JSON payload to use Adaptive Cards.
- Add a 911 alert variant that uses a more urgent message format for emergency calls.
- If Slack rate-limits you, consider writing to a local queueAn Asterisk call queue (app_queue) that parks incoming calls and distributes them to logged-in agents according to a chosen ring strategy. file and having a cron job batch-send notifications.
- Test with
curl -X POST -H 'Content-type: application/json' --data '{"text":"test"}' YOUR_WEBHOOK_URLfrom the Asterisk server first.
User Notes
Know a tip or gotcha for this topic? Share it below and help others.
Contribute a note
Share a tip, gotcha, or practical example. Keep it under 2000 characters. No questions (use the Asterisk community forums for support). Wrap code in backticks.