API reference / Voice and AI
Replace FreeSWITCH AMD
If your FreeSWITCH dialplan runs mod_amd, mod_com_amd or mod_avmd, you
can switch to Drop Cowboy® Detection by changing the application line. Two Lua
scripts set the same channel variables and fire the same events as the
modules they replace, so the dialplan and event listeners that read them keep
working.
dc_amd.lua |
Replaces mod_amd, or mod_com_amd with the com_amd profile. Blocking: the dialplan waits for the result. |
dc_avmd_start.lua |
Replaces mod_avmd. Background: the dialplan continues at once, and the beep is reported when it's heard. |
| What you change | The module's application line becomes a lua line |
| Where your key lives | /etc/freeswitch/dc-amd.conf, mode 0640, and nowhere else |
Both scripts are tested on FreeSWITCH 1.10.12 and 1.11.4. To stream audio to Detection from your own code instead, see Detection.
Before you start
- A Detection key. Create one in the dashboard or with the API. See
Create a Detection key. If the key has
allowed_cidrs, include the public address FreeSWITCH connects from. mod_lua, loaded.mod_audio_fork, loaded. It is the MIT-licensed module from the drachtio-freeswitch-modules project, and it streams the call audio over a WebSocket.- The
statcommand, which the scripts use to check the config file's permissions. - Outbound access to
detect.dropcowboy.comon port 443.
Install
Download the Lua package and the checksums from the latest release, check them and unpack:
curl -fsSLO https://github.com/Drop-Cowboy/pbx-connect/releases/latest/download/dc-amd-freeswitch.tar.gz curl -fsSLO https://github.com/Drop-Cowboy/pbx-connect/releases/latest/download/SHA256SUMS sha256sum --ignore-missing -c SHA256SUMS tar -xzf dc-amd-freeswitch.tar.gz && cd dc-amd-freeswitchCopy
dc_amd.lua,dc_avmd_start.luaanddc_amd_core.luato the FreeSWITCH script directory. To find it, runfs_cli -x 'global_getvar script_dir'. All three files must sit together.Create the config file from the sample:
install -m 0640 -o root -g freeswitch conf/dc-amd.conf.sample /etc/freeswitch/dc-amd.confUse the group FreeSWITCH runs as:
freeswitchon the Debian packages,www-dataon FusionPBX.Put your key in
api_key.Change the dialplan lines.
conf/dialplan-sample.xmlhas an entry for each case.
No reload is needed. Each call reads the config file again.
Your key
Your key lives only in the config file, which must be mode 0640 and owned by
root and the FreeSWITCH group. If other users have any permission on it, the
scripts refuse the file and the call continues without Detection.
- Never put the key in dialplan arguments, a URL or a command line. The scripts take the key, the URL and the username only from the file.
- The key is sent only as the Basic auth password when the WebSocket opens. It is never in the URL, the session's first message or a log line. Logs show at most its last four characters.
- To hand the key to
mod_audio_fork, the script setsMOD_AUDIO_BASIC_AUTH_USERNAMEandMOD_AUDIO_BASIC_AUTH_PASSWORDon the channel and clears both as soon as the stream starts, so they don't reach CDRs. - Event socket access is key access. For that moment the key is in the
channel's variables, so the
MEDIA_BUG_STARTevent and theAPIevent for the command carry it. Keep the event socket bound to localhost with a strong password.
The config file
The scripts read /etc/freeswitch/dc-amd.conf, then
$${conf_dir}/dc-amd.conf. Lines are key = value, and lines starting with
# or ; are comments. [general] applies to every call, and an [amd],
[com_amd] or [avmd] section overrides it for that profile. The one
exception: mode and total_time_ms in [general] don't apply to [avmd],
which keeps its own defaults unless you set them in [avmd].
| Setting | Values | Default |
|---|---|---|
api_key |
Your Detection key | Required |
url |
Where audio is sent. Must be wss://, with no query string. |
wss://detect.dropcowboy.com/carrier/ws |
username |
The Basic auth username sent with the key | dc |
mode |
Enable, DetectMessageEnd |
Enable, or DetectMessageEnd for [avmd] |
human_verdict |
first, final |
first |
total_time_ms |
1000 to 120000 | 30000, or 60000 for [avmd] |
connect_timeout_ms |
200 to 30000 | 3000 |
sample_rate |
16k, 8k |
16k |
avmd_beep_on |
beep, message_end. Used by [avmd] only. |
beep |
log_level |
INFO logs one line per call, WARNING only failures |
INFO |
mode.Enablereturns as soon as the call is classified.DetectMessageEndwaits for the end of a voicemail greeting before returningMACHINE.human_verdict.firstreturns a person as soon as one is heard.finalwaits until Detection is sure, so a greeting that opens like a person comes back as a machine.total_time_ms. The longest a call waits. The call always continues by then.
Any of mode, human_verdict, total_time_ms, connect_timeout_ms,
sample_rate and avmd_beep_on can also be passed as an argument for one
call, such as dc_amd.lua mode=DetectMessageEnd. Other options are ignored,
and the log names them.
Start it at answer, at 16k
Both scripts must start after the call is answered. Either send the answered
call to an extension that runs the script, or start the script from the
originate with execute_on_answer:
originate {ignore_early_media=true,execute_on_answer='lua dc_amd.lua'}sofia/gateway/carrier/13125550142 &park()
Keep sample_rate = 16k, also on 8 kHz calls. mod_audio_fork holds the
audio while the connection opens. In testing, none of that early audio was
lost at 16k, and some of it was at 8k.
dc_amd.lua
dc_amd.lua runs while the dialplan waits. It returns as soon as the result
arrives, at total_time_ms, or when the caller hangs up, and it never hangs
up the call itself.
<action application="lua" data="dc_amd.lua"/> <!-- mod_amd variables -->
<action application="lua" data="dc_amd.lua com_amd"/> <!-- mod_com_amd variables -->
The amd profile
The default. It sets amd_result and amd_cause, as mod_amd does:
| Result | amd_result |
amd_cause |
|---|---|---|
| A live person | HUMAN |
HUMAN |
| A voicemail greeting | MACHINE |
GREETING |
| The greeting ended with a beep | MACHINE |
BEEP |
| The greeting ended without a beep | MACHINE |
GREETINGEND |
| Nobody spoke after the greeting | MACHINE |
SILENCE |
| A full mailbox | MACHINE |
FULLMAILBOX |
| A mailbox that isn't set up | MACHINE |
NOTSETUP |
| A carrier message | MACHINE |
INTERCEPT |
| A phone menu | MACHINE |
IVR |
| A call screener | MACHINE |
SCREENER |
| Probably a call screener, not confirmed | NOTSURE |
SCREENER |
| A fax machine | MACHINE |
FAX |
| No decision in time | NOTSURE |
TOOLONG |
| Detection couldn't run | NOTSURE |
TOOLONG, and dc_amd_error is set |
The com_amd profile
It sets amd_status and amd_result, as mod_com_amd does:
| Result | amd_status |
amd_result |
|---|---|---|
| A live person | person |
silent-after-intro, or silent-initial when nobody spoke |
| A machine or a fax | machine |
max-count |
| No decision, or Detection couldn't run | unsure |
too-long |
Then it fires DETECTED_SPEECH with the headers Unique-ID,
Channel-Name, Channel-Call-UUID, amd_result and amd_status, and the
body amd_complete. Last, it runs every amd_execute_on_<status> variable on
the channel, such as amd_execute_on_machine. If the caller hangs up first,
it sets amd_wait_status=hangup and amd_wait_result=hangup and no result.
Variables both profiles set
| Variable | Value |
|---|---|
dc_amd_source |
detection, failopen, or hangup when the caller hung up first |
dc_amd_answered_by |
Detection's answered_by: human, machine_start, machine_end_beep, machine_end_silence, machine_end_other, fax or unknown |
dc_amd_cause |
Detection's detail.cause, when there is one |
dc_amd_duration_ms |
How much audio Detection heard before deciding |
dc_amd_beep_end_ms |
When the beep ended, when there was one |
dc_amd_elapsed_ms |
How long the script waited |
dc_amd_error |
Why Detection wasn't used. See Fail open. |
dc_amd_error_detail |
A short description, never containing the key |
dc_amd_truncated |
true if mod_audio_fork reported that audio was dropped |
dc_avmd_start.lua
dc_avmd_start.lua replaces avmd_start. It starts the stream, hands the
wait to a background worker and returns, so the dialplan carries on, for
example by playing a message to a person.
<action application="set" data="execute_on_avmd_beep=transfer leave_message XML default"/>
<action application="lua" data="dc_avmd_start.lua"/>
<action application="playback" data="/var/lib/freeswitch/recordings/live-message.wav"/>
<!-- optional, like avmd_stop -->
<action application="lua" data="dc_avmd_start.lua stop"/>
When the beep is heard, the worker does what mod_avmd does, in the same
order:
- sets
avmd_total_time=[<ms>]; - runs every
execute_on_avmd_beep*variable on the channel; - sets
avmd_detect=TRUE; - fires
avmd::beep.
avmd_beep_on decides what counts as the beep. beep, the default, needs a
beep. message_end counts any end of the greeting, with or without one.
| Event | When | Headers |
|---|---|---|
avmd::start |
Detection starts | Unique-ID, Call-command: avmd, Start-time |
avmd::beep |
The beep is heard | Unique-ID, Call-command: avmd, Beep-Status: DETECTED, Detection-time |
avmd::stop |
Detection ends | Unique-ID, Call-command: avmd, Beep-Status: DETECTED or NOTDETECTED, Total-time |
avmd::beep also carries Frequency, Amplitude, their variances and the
Detector-* headers, all zero. Detection-time is real.
A person, a timeout or a failure ends detection with no beep, and
avmd::stop has Beep-Status: NOTDETECTED. A hangup ends it with no event
and no variables. When it ends, the worker sets the dc_avmd_* variables,
named like the dc_amd_* variables above. After a stop request,
dc_avmd_source is stopped.
Differences from mod_avmd
- Events go to the event system only.
mod_avmdcan also queue its events to the channel. A script can't, soavmd::*reaches event socket and Lua listeners, but not listeners on the channel itself.DETECTED_SPEECHfrom thecom_amdprofile works the same way. execute_on_avmd_beepis queued on the channel. The worker isn't the call's own thread, so each application runs at the call's next chance: duringpark,playbackorsleep. A value containing a single quote is skipped and logged.
Fail open
Every failure leaves the call running. dc_amd.lua sets an unsure result,
dc_amd_source=failopen and dc_amd_error. dc_avmd_start.lua reports no
beep and sets dc_avmd_source=failopen and dc_avmd_error.
| Error | Meaning |
|---|---|
config_missing |
There is no dc-amd.conf in /etc/freeswitch or $${conf_dir}. |
config_permissions |
Other users have permission on the file, or its mode couldn't be read. |
config_invalid |
A setting is out of range, or the URL has a query string. |
no_api_key, key_invalid |
api_key is missing or has unexpected characters. |
fork_start_failed |
mod_audio_fork refused to start. Check that it's loaded. |
worker_start |
The background worker didn't start. dc_avmd_start.lua only. |
connect_timeout |
The connection didn't open in time. A DNS failure ends up here. |
connect_failed |
The connection was refused, or TLS failed. |
unauthorized |
The key was refused. |
disconnected, closed |
The connection ended before a result. |
fork_error, detection_error |
mod_audio_fork or Detection reported an error. |
timeout |
No result within total_time_ms. |
hangup |
The caller hung up first. dc_amd.lua only. |
FusionPBX
FusionPBX's Call Broadcast has a Voicemail Detection toggle. When it's on,
each broadcast call runs avmd start at answer, then the broadcast's
Destination. The application is fixed, so the toggle can't be pointed at a
script. Use the Destination instead:
Turn the toggle off.
In Dialplan Manager, add an entry that runs a script. For a blocking result to route on:
<extension name="broadcast_amd"> <condition field="destination_number" expression="^broadcast_amd$"> <action application="lua" data="dc_amd.lua mode=DetectMessageEnd"/> <action application="transfer" data="broadcast_${amd_result} XML ${domain_name}"/> </condition> </extension>Or, to play a message at the beep in the background:
<extension name="broadcast_avmd"> <condition field="destination_number" expression="^broadcast_avmd$"> <action application="set" data="execute_on_avmd_beep=playback /var/lib/freeswitch/recordings/${domain_name}/message.wav"/> <action application="lua" data="dc_avmd_start.lua"/> <action application="playback" data="/var/lib/freeswitch/recordings/${domain_name}/live-message.wav"/> <action application="hangup"/> </condition> </extension>Set the broadcast's Destination to
broadcast_amdorbroadcast_avmd.
The call reaches the entry only after answer, which is where both scripts
start. If FusionPBX's own avmd listener is running, it hangs up on the
script's avmd::beep as it would on mod_avmd's.
- The key file. FusionPBX runs FreeSWITCH as
www-data, so create the fileroot:www-data, mode 0640. FusionPBX hands/etc/freeswitchtowww-dataeach time FreeSWITCH starts and after upgrades. That changes the owner but not the mode, so the file stays accepted. - The web server can read the key. It also runs as
www-data, and FusionPBX doesn't separate the web and switch users. - The script directory is
/usr/share/freeswitch/scriptson package installs and/usr/local/freeswitch/share/freeswitch/scriptson source installs. Copy the three Lua files to whicheverscript_dirreports.
Switch back
Change each lua dc_amd.lua or lua dc_avmd_start.lua line back to the
module's application, and make sure the module is loaded. On FusionPBX, set
the broadcast's Destination back and turn the toggle on. The Lua files and
the config file can stay; nothing runs them once the dialplan stops calling
them.
Common problems
- Every call is unsure with
fork_start_failed.mod_audio_forkisn't loaded. Load it, and add it tomodules.conf.xmlso it loads at startup. - Every call is unsure with
config_permissions. The file can be read by other users. Runchmod 0640 /etc/freeswitch/dc-amd.conf. - Every call is unsure with
config_missing. FreeSWITCH can't read the file. Check that its group is the one FreeSWITCH runs as. - Every call is unsure with
unauthorized. The key was revoked, or it hasallowed_cidrsand this server's public address isn't in them. - Every call is unsure with
connect_timeout. A firewall blocks outbound port 443 todetect.dropcowboy.com, or DNS fails. execute_on_avmd_beepruns late. Queued applications run at the call's next chance. Keep the call inpark,playbackorsleepwhile waiting for the beep.- FusionPBX broadcast calls drop at answer. The Voicemail Detection
toggle is on and
mod_avmdisn't loaded. FreeSWITCH logsInvalid Application avmdand hangs up withDESTINATION_OUT_OF_ORDER. Turn the toggle off and use the Destination, as above.