Custom Greeting with STAT Fallback
Custom Greeting with STAT Fallback
When deploying a PBX, custom audio greetings are rarely ready on day one. This pattern checks whether a custom greeting file exists on disk and plays it if found, otherwise falls back to Asterisk's built-in sound files. This lets you deploy with generic greetings and drop in custom recordings later without any dialplan changes.
The Snippet
[ivr-main]
exten => s,1,Answer()
same => n,Wait(1)
; Check for custom greeting in any common audio format
same => n,Set(GREETING=/var/lib/asterisk/sounds/custom/main-greeting)
same => n,GotoIf($[${STAT(e,${GREETING}.wav)} || ${STAT(e,${GREETING}.ulaw)} || ${STAT(e,${GREETING}.gsm)}]?play_custom)
;
; No custom greeting found - use built-in sounds
same => n,Playback(thank-you-for-calling)
same => n,Goto(menu)
;
same => n(play_custom),Playback(${GREETING})
same => n(menu),Background(press-1&press-2&or&press-3)
same => n,WaitExten(5)
How It Works
STAT(e,filename) returns 1 if the file exists, 0 if it doesn't. The e flag specifically tests for existence. By checking for .wav, .ulaw, and .gsm extensions, the pattern works regardless of which audio format was uploaded.
Note that Playback() doesn't need the file extension. Asterisk automatically finds the best format match. But STAT() checks actual files on disk, so we need to include extensions.
The GREETING variable stores only the base path (no extension). This way, Playback(${GREETING}) lets Asterisk choose the optimal format for the channel's codec.
Where to Place Custom Greetings
/var/lib/asterisk/sounds/custom/
main-greeting.wav # Main IVR greeting
afterhours-greeting.wav # After-hours message
holiday-greeting.wav # Holiday closure message
queue-holdmusic.wav # Custom hold music
Create the directory if it doesn't exist:
mkdir -p /var/lib/asterisk/sounds/custom
chown asterisk:asterisk /var/lib/asterisk/sounds/custom
Recording Format
For best compatibility, record in one of these formats:
- WAV: 8000 Hz, 16-bit, mono PCM (most universal)
- ulaw: 8000 Hz mu-law (native PSTN format, smallest files)
- GSM: Compressed, good for storage-constrained systems
Convert with sox:
# Convert any audio to Asterisk-compatible WAV
sox input.mp3 -r 8000 -c 1 -e signed-integer -b 16 output.wav
# Convert to ulaw
sox input.mp3 -r 8000 -c 1 -t ul output.ulaw
Using with After-Hours
The same pattern works for after-hours greetings, where each department can have its own custom greeting:
; Subroutine: call via Gosub(sub-afterhours,s,1(sales))
[sub-afterhours]
exten => s,1,Answer()
same => n,Wait(1)
; ARG1 = department name (e.g., "sales", "support")
same => n,Set(AH_GREETING=/var/lib/asterisk/sounds/custom/afterhours-${ARG1})
same => n,GotoIf($[${STAT(e,${AH_GREETING}.wav)} || ${STAT(e,${AH_GREETING}.ulaw)}]?play_custom)
; Generic fallback
same => n,Playback(thank-you-for-calling)
same => n,Goto(continue)
same => n(play_custom),Playback(${AH_GREETING})
same => n(continue),Return()
; Example: route to after-hours with department name
[incoming-sales]
exten => s,1,Gosub(sub-afterhours,s,1(sales))
same => n,Hangup()
Call via Gosub() so that ${ARG1} is populated with the department name. Dropping afterhours-sales.wav into the custom sounds directory instantly gives the sales department a personalized greeting.
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.