Slack Webhook Notifications
Slack Webhook Notifications
Send a Slack message whenever an inbound call arrives, showing the caller ID and which DID was dialed. Uses a Gosub subroutine so any inbound context 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 queue 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
No notes yet. Be the first to contribute a tip or example.
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.