Replace Asterisk AMD

If your dialplan runs Asterisk's AMD() application, you can switch to Drop Cowboy Detection by changing one line. dc-amd takes the same arguments as AMD() and sets the same variables, AMDSTATUS and AMDCAUSE, so the dialplan that routes on them keeps working.

What you install dc-amd, one static Linux binary (amd64 or arm64) in Asterisk's AGI directory
What you change AMD(...) becomes EAGI(dc-amd,...)
What you get AMDSTATUS, AMDCAUSE, AMDSTATS on VICIdial builds, and DC_AMD_* variables
Where your key lives /etc/dc-amd/dc-amd.conf, mode 0640, and nowhere else

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 your Asterisk server connects from.
  • Outbound access from the Asterisk server to detect.dropcowboy.com on port 443.
  • Root access on the server, to install the binary and the config file.

Install

Download the binary for your server and the checksums from the latest release, check them, then run the installer:

curl -fsSLO https://github.com/Drop-Cowboy/pbx-connect/releases/latest/download/dc-connect-linux-amd64
curl -fsSLO https://github.com/Drop-Cowboy/pbx-connect/releases/latest/download/SHA256SUMS
sha256sum --ignore-missing -c SHA256SUMS
chmod +x dc-connect-linux-amd64
sudo ./dc-connect-linux-amd64 amd install

On an ARM server, use dc-connect-linux-arm64. The installer:

  • copies the binary to /var/lib/asterisk/agi-bin/dc-connect and links dc-amd to it;
  • asks for your Detection key without showing it;
  • writes /etc/dc-amd/dc-amd.conf, mode 0640, owned by root:asterisk;
  • creates /var/log/dc-amd;
  • runs dc-amd check, which opens a short session with Detection to prove the key works.
Option Use it when
--agi-dir DIR Your AGI directory isn't /var/lib/asterisk/agi-bin.
--user NAME, --group NAME Asterisk doesn't run as asterisk. The config file's group must be the one Asterisk runs as.
--key-stdin You install from a script. The key is read from standard input, never from the command line.
--no-check The server can't reach Detection yet. Run dc-amd check later.

Change one line

Replace AMD with EAGI(dc-amd,...) and keep the arguments:

; before
same => n,AMD(2000,2000,1000,5000,120,50,4,256)

; after
same => n,EAGI(dc-amd,2000,2000,1000,5000,120,50,4,256)

Use EAGI, not AGI. EAGI gives the script the call's audio. Under plain AGI, dc-amd hears nothing and fails open with DC_AMD_ERROR=no_audio_fd.

When the channel is wideband, such as a call using G.722 or Opus, add one line before it so dc-amd gets the 16 kHz audio:

same => n,Set(EAGI_AUDIO_FORMAT=slin16)
same => n,EAGI(dc-amd,2000,2000,1000,5000,120,50,4,256)

Leave it out on narrowband calls. Changes to the config file apply from the next call, with no reload.

Arguments

dc-amd reads AMD()'s nine arguments, in the same order:

# Argument Default
1 initialSilence 2500
2 greeting 1500
3 afterGreetingSilence 800
4 totalAnalysisTime 5000
5 minimumWordLength 100
6 betweenWordsSilence 50
7 maximumNumberOfWords 2
8 silenceThreshold 256
9 maximumWordLength 5000
  • An empty or missing argument takes its value from [general] in Asterisk's amd.conf, then from the default above, as AMD() does.
  • totalAnalysisTime is the time limit. dc-amd always returns within it, including the time spent connecting. With wait-for-beep on, the limit is beep_timeout_sec instead.
  • profile=NAME, anywhere in the list, applies the [NAME] section of the config file. Any other name=value is ignored, with a warning in the log.
  • Every argument is passed to AMD() whenever AMD() runs: in Shadow and Off mode, and with fail_open = native.

The config file

The file is /etc/dc-amd/dc-amd.conf. To use another path, set the DC_AMD_CONFIG environment variable for Asterisk.

Your key lives only in this file, which must be mode 0640 and owned by root:asterisk. If other users have any permission on it, dc-amd refuses the file and fails open. Never put the key in dialplan arguments, a URL or a command line. dc-amd sends it only as a bearer token on the connection to Detection, and logs and dc-amd check show only its last four characters.

Setting Values Default
api_key Your Detection key Required
mode replace, shadow, off replace
fail_open notsure, native notsure
connect_timeout_ms 100 to 60000, capped by totalAnalysisTime 2000
wait_for_beep yes, no no
beep_timeout_sec 3 to 59 30
human_verdict first, final first
silence_status MACHINE, HUMAN, NOTSURE MACHINE
screener_status MACHINE, HUMAN, NOTSURE NOTSURE
cause_names detailed, stock detailed
amd_stats auto, yes, no auto
log_file A path, or none /var/log/dc-amd/dc-amd.log
shadow_dir A directory /dev/shm/dc-amd
results_file A path /var/log/dc-amd/results.jsonl

Lines are name = value. Lines starting with # or ; are comments. Settings outside any section apply to every call. A [name] section overrides them for calls that pass profile=name, and a [general] header goes back to the settings for every call. api_key, url, amd_stats, log_file, shadow_dir and results_file can't be set in a section. The installer writes the [beep] section below.

api_key = paste-your-detection-key-here
mode = replace

[beep]
wait_for_beep = yes

Modes

Mode What happens on the call Use it to
Replace Detection decides, and dc-amd sets the variables. Run on Drop Cowboy® Detection.
Shadow AMD() decides, as before. dc-amd records the same audio, and after the call returns to the dialplan, a background process sends it to Detection and writes both answers to results_file. Compare before you switch.
Off AMD() decides. Nothing goes to Detection. Turn Detection off without editing the dialplan.

In Shadow mode the recording is kept in shadow_dir only until it has been sent. results_file holds one JSON line per call, with the Asterisk unique ID, both answers and no key. To see how often the two agree, and how fast each decided, run:

/var/lib/asterisk/agi-bin/dc-amd stats --since 24h

Switch to Replace when you're satisfied, by changing mode in the config file.

What gets set

AMDSTATUS is always one of AMD()'s values: HUMAN, MACHINE, NOTSURE or HANGUP. With cause_names = detailed, the default, AMDCAUSE names what was heard:

Result AMDSTATUS AMDCAUSE
A live person HUMAN HUMAN-<ms>-0
A voicemail greeting MACHINE GREETING-<ms>-0
The greeting ended with a beep MACHINE BEEP-<ms>-<beep end ms>
The greeting ended without a beep MACHINE GREETINGEND-<ms>-0
A full mailbox MACHINE FULLMAILBOX-<ms>-0
A mailbox that isn't set up MACHINE NOTSETUP-<ms>-0
A carrier message MACHINE INTERCEPT-<ms>-0
A phone menu MACHINE IVR-<ms>-0
A fax machine MACHINE FAX-<ms>-0
A call screener screener_status CALLSCREEN-<ms>-0
Nobody spoke silence_status INITIALSILENCE-<ms>-<initialSilence>
No decision in time NOTSURE TOOLONG-<ms>
The caller hung up HANGUP Empty

<ms> is the time to the decision in milliseconds. A call that sent no audio at all comes back as NOTSURE and TOOLONG-<ms>, not as silence. AMDCAUSE is never longer than 30 characters; trailing numbers are dropped first.

Stock cause names

Some dialers match on the cause names AMD() itself produces. Set cause_names = stock and AMDCAUSE uses only those:

AMDSTATUS AMDCAUSE
HUMAN HUMAN-<afterGreetingSilence>-<afterGreetingSilence>
MACHINE, nobody spoke INITIALSILENCE-<initialSilence>-<initialSilence>
MACHINE, a fax machine LONGGREETING-<greeting>-<greeting>
MACHINE, anything else MAXWORDS-<maximumNumberOfWords + 1>-<maximumNumberOfWords>, or MAXWORDS-<maximumNumberOfWords>-<maximumNumberOfWords> on Asterisk 13 and earlier
NOTSURE TOOLONG-<ms>
HANGUP Empty

AMDSTATS

On VICIdial builds of Asterisk, where AMD() sets AMDSTATS, dc-amd sets it too, in the same nine fields: <ms>-0-<initialSilence>-<afterGreetingSilence>-0-0-<greeting>-0-<maximumNumberOfWords>. On other builds it isn't set. With amd_stats = auto, the default, dc-amd recognizes a VICIdial build from its version string. Some VICIdial builds report a plain version, so set amd_stats = yes on those; the VICIdial installer does this for you.

DC_AMD_* variables

Variable Value
DC_AMD_TYPE Detection's answered_by: human, machine_start, machine_end_beep, machine_end_silence, machine_end_other, fax or unknown. Empty when Detection didn't decide.
DC_AMD_BEEP 1 when the greeting ended with a beep, otherwise 0.
DC_AMD_BEEP_MS When the beep ended, in milliseconds. Empty when there was none.
DC_AMD_SCREENER 1 for a call screener, otherwise 0.
DC_AMD_FAX 1 for a fax machine, otherwise 0.
DC_AMD_MS The time to the result, in milliseconds.
DC_AMD_SESSION The Detection session ID, for support. Empty when Detection didn't decide.
DC_AMD_SOURCE detection, native when AMD() decided, or failopen.
DC_AMD_ERROR Why Detection wasn't used. Empty otherwise. See Fail open.

When HUMAN is decided

Some voicemail greetings start like a person picking up. human_verdict chooses how they're handled:

  • first (the default) returns HUMAN as soon as a person is heard. It is the fastest, and a greeting that opens like a person can come back as HUMAN.
  • final waits until Detection is sure. Those greetings come back as MACHINE, and a real person a little later than with first.

Wait for the beep

To leave a voicemail message, start it when the greeting ends. Turn on wait-for-beep in a profile, then pass the profile on the calls that leave messages:

[beep]
wait_for_beep = yes
same => n,EAGI(dc-amd,2000,2000,1000,5000,120,50,4,256,profile=beep)
same => n,GotoIf($["${AMDSTATUS}" = "MACHINE"]?message)
same => n,Goto(agents,s,1)
same => n(message),Playback(custom/voicemail-message)
same => n,Hangup()

With wait-for-beep on, MACHINE comes back at the end of the greeting: at the beep, or when the greeting ends without one. A person still comes back as HUMAN as soon as they're heard. The limit is beep_timeout_sec, 30 seconds by default, in place of totalAnalysisTime. A full mailbox or one that isn't set up also comes back as MACHINE, with AMDCAUSE starting FULLMAILBOX or NOTSETUP; hang up on those instead of playing the message.

Fail open

dc-amd never hangs up a call and never holds the dialplan past its time limit. If Detection can't answer, it sets AMDSTATUS=NOTSURE, AMDCAUSE=TOOLONG-<ms>, DC_AMD_SOURCE=failopen and DC_AMD_ERROR to the reason, and the call continues.

With fail_open = native, dc-amd runs AMD() itself instead, for the time that remains. It does this only when no audio has been read yet and at least one second is left, and DC_AMD_SOURCE is then native.

DC_AMD_ERROR Meaning
config_missing, config_unreadable The config file is missing, or Asterisk can't read it.
config_insecure Other users have permission on the config file.
config_invalid A setting is out of range. The log names the line number.
unknown_profile The call passed profile=NAME and the file has no [NAME] section.
no_api_key api_key isn't set.
no_audio_fd The script was started with AGI instead of EAGI.
audio_format EAGI_AUDIO_FORMAT is set to something other than slin or slin16.
agi_failed Asterisk's AGI conversation failed.
connect_timeout, dns, connect_refused, tls, connect_failed The connection to Detection didn't open.
http_401, http_403 The key was refused, or this server's address is outside the key's allowed ranges.
http_402 The account has no funds.
http_429 The key reached its daily minute cap.
http_503 Detection is at capacity or briefly unavailable.
closed, no_result, send_failed, welcome_failed The session ended before a result.
deadline No result within the time limit.
overrun The audio fell behind.
native_unavailable fail_open = native, or Off or Shadow mode, needed AMD(), and it couldn't run.

If the caller hangs up, dc-amd sets AMDSTATUS=HANGUP with an empty AMDCAUSE and exits within about a second.

Commands

Run these as root, from the AGI directory:

Command What it does
dc-amd check Checks the config file and opens a short session with Detection.
dc-amd test call.wav Sends a 16-bit mono WAV file, 8 or 16 kHz, through the same path a call takes and prints the variables it would set. --args takes AMD() arguments and --profile a profile.
dc-amd stats Summarizes Shadow mode results. --since 24h limits the window.
dc-amd uninstall Removes the binary and the link. The config file and logs stay unless you add --purge.
dc-amd version Prints the version.

Switch back

  • Right away, with no dialplan edit: set mode = off in the config file. The next call runs AMD() with the same arguments.
  • For good: change each EAGI(dc-amd,...) line back to AMD(...), reload the dialplan, then run dc-amd uninstall. Uninstall first and those lines fail, leaving AMDSTATUS unset.

WombatDialer

WombatDialer runs AMD() from a dialplan context you write for it, called [amddetect] in its examples. Change the AMD() line in that context to EAGI(dc-amd,...) with the same arguments. The lines after it, which read AMDSTATUS, stay as they are.

XCALLY Motion

XCALLY Motion writes its own dialplan. Where your version lets you override the context that runs AMD(), change that line to EAGI(dc-amd,...) as above. Where it doesn't, keep its built-in answering machine detection.

Common problems

  • AMDSTATUS is empty after a hangup. AGIEXITONHANGUP is set on the channel. It ends dc-amd at the hangup, before it can set HANGUP. Don't set it on these calls; the log warns when it is.
  • Every call is NOTSURE with config_insecure. The config file can be read by other users. Run chmod 0640 /etc/dc-amd/dc-amd.conf and chown root:asterisk /etc/dc-amd/dc-amd.conf.
  • Every call is NOTSURE with config_unreadable. Asterisk runs as a user outside the file's group. Change the group to the one Asterisk runs as.
  • Every call is NOTSURE with no_audio_fd. The line says AGI(dc-amd,...). Change it to EAGI.
  • Calls with profile=beep are NOTSURE with unknown_profile. Add the [beep] section to the config file. A file written by an earlier install doesn't have it.
  • Every call is NOTSURE with http_403. The key has allowed_cidrs and this server's public address isn't in them, or the key was revoked.
  • Every call is NOTSURE with connect_timeout. A firewall blocks outbound port 443 to detect.dropcowboy.com. Run dc-amd check to confirm.
  • Messages start over the greeting. The call doesn't pass a profile with wait_for_beep = yes. See Wait for the beep.

Code samples

The requests from this page, ready to copy. Set DC_KEY and DC_SECRET to your API key pair first.

Install
curl -fsSLO https://github.com/Drop-Cowboy/pbx-connect/releases/latest/download/dc-connect-linux-amd64
curl -fsSLO https://github.com/Drop-Cowboy/pbx-connect/releases/latest/download/SHA256SUMS
sha256sum --ignore-missing -c SHA256SUMS
chmod +x dc-connect-linux-amd64
sudo ./dc-connect-linux-amd64 amd install
Modes
/var/lib/asterisk/agi-bin/dc-amd stats --since 24h