What the recipe does
- A call comes in from your SIP provider.
- If it is a closed day (Christmas, Easter, holidays), it goes straight to the AI agent.
- During opening hours it rings the staff for 20 seconds.
- If nobody answers, or it is after closing, a fixed AI disclosure is played and the call is sent to the agent over SIP.
- If the agent can’t be reached, the caller hears a message and reaches voicemail.
- If the agent wants to transfer to a human, it sends the call back to the phone system.
It is the same basic idea we use in our own system, LynBro PBX, which runs on Asterisk 22 and FreePBX 17. Our own connection to the speech model goes over a websocket and a Python service, though, not over SIP as here. The recipe is the generic version that works with most voice-agent platforms that accept SIP. Which platform to send the call on to is compared in voice agent platforms.
extensions.conf
[globals]
TZ_DK=Europe/Copenhagen
; Your own number at the SIP provider. Many providers silently reject
; outbound calls if the sender is not a number on your account.
EGET_NUMMER=SKRIV_JERES_NUMMER_HER
[fra-udbyder]
; All inbound calls from the SIP trunk land here
exten => _X.,1,NoOp(Indgaaende opkald fra ${CALLERID(num)})
same => n,Set(__TRANSFER_CONTEXT=fra-ai-agent)
same => n,Set(IDAG=${STRFTIME(${EPOCH},${TZ_DK},%Y%m%d)})
same => n,GotoIf(${DB_EXISTS(lukket/${IDAG})}?lukket,1)
same => n,GotoIfTime(08:00-16:00,mon-thu,*,*,${TZ_DK}?aabent,1)
same => n,GotoIfTime(08:00-15:30,fri,*,*,${TZ_DK}?aabent,1)
same => n,Goto(lukket,1)
exten => aabent,1,NoOp(Aabningstid: personalet foerst)
same => n,Dial(PJSIP/reception&PJSIP/kontor,20)
same => n,Goto(lukket,1)
exten => lukket,1,NoOp(Lukket eller ingen svarede: AI-agenten)
same => n,StopMixMonitor()
same => n,Answer()
same => n,Playback(custom/ai-oplysning)
same => n,Dial(PJSIP/ai-agent,30,g)
same => n,GotoIf($["${DIALSTATUS}"="ANSWER"]?slut)
same => n,Playback(custom/ai-utilgaengelig)
same => n,VoiceMail(100@kontor,u)
same => n(slut),Hangup()
[fra-ai-agent]
; The agent transfers (SIP REFER) to the extension "menneske" (human)
exten => menneske,1,NoOp(AI-agenten overdrager til et menneske)
same => n,Dial(PJSIP/reception&PJSIP/kontor,30)
same => n,VoiceMail(100@kontor,u)
same => n,Hangup()
[ud-via-udbyder]
; Danish 8-digit numbers only. No 90-numbers, no short numbers.
exten => _90XXXXXX,1,Hangup(21)
exten => _1XX,1,Hangup(21)
exten => _[2-9]XXXXXXX,1,Set(CALLERID(num)=${EGET_NUMMER})
same => n,Dial(PJSIP/${EXTEN}@udbyder,60)
same => n,Hangup()
The context and extension names are Danish: aabent = open, lukket = closed, udbyder = provider, kontor = office. Closed days go into Asterisk’s internal database from the command line, so you don’t have to reload the dialplan every Christmas:
asterisk -rx "database put lukket 20261224 1"
asterisk -rx "database put lukket 20261231 1"
asterisk -rx "database show lukket"
asterisk -rx "dialplan reload"
pjsip.conf: the connection to the AI agent
[ai-agent]
type=endpoint
transport=transport-udp
context=fra-ai-agent
disallow=all
allow=ulaw,alaw
aors=ai-agent
outbound_auth=ai-agent-auth
direct_media=no
rtp_symmetric=yes
force_rport=yes
rewrite_contact=yes
allow_transfer=yes
; If calls drop at a fixed time (e.g. after 5 min.), look here:
timers=yes
timers_sess_expires=1800
[ai-agent]
type=aor
contact=sip:sip.stemmeagent.example:5060
qualify_frequency=60
[ai-agent-auth]
type=auth
auth_type=userpass
username=SKIFT_MIG
password=SKIFT_MIG
The four choices that matter
The AI disclosure is an audio file, not a line of dialogue. Playback(custom/ai-oplysning) plays before the agent is even connected. So it always says the same thing, whatever the model and prompt. We do the same: audio files such as greetings and menus are generated in advance with a text-to-speech model, and the AI disclosure on our outbound calls is a fixed greeting, not something the model phrases itself. The file goes in sounds/custom/, for example as ai-oplysning.wav in 8 kHz mono. The rules behind it are on call recording and GDPR.
Silence is the worst failure. If the agent doesn’t answer, Dial(...,g) continues in the dialplan, and the caller hears a message and reaches voicemail. We learned this the hard way: before the fix, a call could fall silent after 3.5 seconds if the speech model couldn’t be reached. Now the caller hears a message in their own language, and the business is told to call back.
Session timers and the sender number. In our system calls dropped after about 5 minutes, and it was SIP session timers, not the audio. Outbound calls failed silently until we sent our own number as the sender. Both are covered in the configuration above.
Outbound traffic is locked down. [ud-via-udbyder] only allows Danish 8-digit numbers and rejects 90-numbers and short numbers. Our own system has the same lock and a daily cap on outbound calls per plan. A stolen SIP password is expensive; use long passwords and fail2ban.
Transfer to a human
When the agent decides a human should take over, it sends a SIP REFER to the extension menneske. Asterisk looks it up in TRANSFER_CONTEXT, which we set on the call with two underscores so the agent’s channel inherits it. That requires the platform to support REFER. Not all do, and not all carriers pass it on: we couldn’t transfer live from OpenAI’s own SIP connection with our provider and had to build a bridge in Asterisk. Test it before you promise callers they can “reach a person”.
Delay is the other thing to test. Above roughly 1.2 seconds from when the caller stops speaking until the agent answers, people talk over it. Place the agent in an EU data centre close to your phone system, and avoid unnecessary audio transcoding: ulaw all the way is the simplest.
Recording only with a key press
If you want to record the calls your staff take, for training, the caller must be able to say no. Datatilsynet (the Danish Data Protection Agency) gives a key press as an example of active consent. A simple version in the aabent extension, before Dial:
same => n,Read(SVAR,custom/optagelse-tryk-1,1,,1,4)
same => n,ExecIf($["${SVAR}"="1"]?MixMonitor(${UNIQUEID}.wav))
StopMixMonitor() in lukket makes sure recording stops before the AI agent is connected. And a clean-up job that deletes recordings after 3 months, which is the example deletion period Datatilsynet gives:
# /etc/cron.daily/slet-optagelser
find /var/spool/asterisk/monitor -name '*.wav' -mtime +90 -delete
Sources
- Datatilsynet – Optagelse af telefonsamtaler (April 2024) — key press as consent, deletion period e.g. 3 months, read 19.11.2026
Questions about Asterisk and AI agents
Does this work with FreePBX?
Yes. In FreePBX your own dialplan goes in extensions_custom.conf, and the endpoint is created as a trunk. Our own system runs FreePBX 17 on top of Asterisk 22.
Which voice agents accept SIP?
Several of the big platforms accept calls over SIP. Retell, for example, states that your own SIP trunk costs nothing extra per minute. See the prices on the AI receptionist page.
Why do our calls drop after exactly 5 minutes?
Almost always SIP session timers. Check timers on both sides, and whether the provider answers the re-negotiation.
Should we run our own Asterisk system?
Only if you already have one, or have someone who knows SIP. Otherwise hosted call routing with an AI receptionist as forwarding is simpler. See business VoIP in Denmark.
Fixed price, no commitment
Get a fixed price for your telephony task
Tell us briefly what you use today and what you want to achieve. A person answers by e-mail with a proposal and a fixed price before any work starts.
- A fixed price before we start
- An answer by e-mail, no calls
- Your details are used only to answer you