Connect FreeSWITCH
The install command, sending answered calls to AMD Cop, test calls and rollback.
This guide is for whoever runs the FreeSWITCH server that places your outbound calls. One command installs AMD Cop on it, the same command ViciDial uses, and takes about a minute. Then you send answered calls to AMD Cop, which is one change in your dialer.
Before you start
- FreeSWITCH 1.10 with
mod_lualoaded, which a standard install has, andcurl7.55 or newer (CentOS 7's 7.29 is too old). Tested on FreeSWITCH 1.10.12. - Root on each server that places calls.
- Outbound HTTPS from the server to
api.amdcop.aiandpilot.amdcop.ai, port 443. FreeSWITCH needs nothing else from Network and security: the TCP 8090 stream is for ViciDial only. - The install command. In the portal, open Dialers, then Get my install command.
How it works
- The call is answered and your dialer sends it to
amdcop_detectin theamdcopcontext. - AMD Cop's script records what the called party says for 3.5 seconds, in mono, without your side of the call.
- It sends the recording to AMD Cop over HTTPS and waits up to 2 seconds for the answer.
- It sets
amdcop_resulttoHUMAN,MACHINEorNOTSURE, and the call goes on: a person orNOTSUREto your agents, a machine to your message or a hang-up.
The script never fails a call. No key, no network, a slow answer or a call that hangs up all give NOTSURE, with the reason in amdcop_cause, and NOTSURE goes to an agent. Anything but MACHINE goes to an agent, even no result at all, so a person is never hung up on because AMD Cop could not answer.
The verdict comes about 3.6 to 5.5 seconds after the answer: 3.5 seconds of listening, then the time AMD Cop takes to reply. A person who says "Hello?" waits that long for an agent, so read Ofcom and TCPA notes before you change amdcop_listen_s. Listening for less than 3.5 seconds gives more NOTSURE answers: AMD Cop needs 3 seconds of audio after the first word.
1. Run the install command
On each FreeSWITCH server that places calls:
curl -fsSL https://api.amdcop.ai/connector.sh | sudo bash -s amdcop_live_...It finds FreeSWITCH by itself, says what it found and where each file goes, then:
- saves your key in
amdcop/key.headerin FreeSWITCH's configuration folder (/etc/freeswitchon most packages), readable by the user FreeSWITCH runs as and nobody else; - installs
amdcop.luain FreeSWITCH's scripts folder; - adds one dialplan file,
dialplan/amdcop.xml, with a context of its own calledamdcop, and runsreloadxml; - adds a heartbeat every minute, so Dialers shows the server as connected;
- registers the server's address with AMD Cop.
It changes nothing in your existing dialplan, loads no module and restarts nothing, and no call reaches AMD Cop until you send it there in step 2. It stops and changes nothing when FreeSWITCH is not running, mod_lua is not loaded, curl is too old, or an amdcop.lua or amdcop.xml is already there that AMD Cop did not write, and says what to do.
To see what it would do without changing anything, add AMDCOP_DRY_RUN=1:
curl -fsSL https://api.amdcop.ai/connector.sh | sudo AMDCOP_DRY_RUN=1 bash -s amdcop_live_...Running the command again is safe: it changes nothing that is already right, and is how you change the key or where calls go. On a server with both Asterisk and FreeSWITCH, add AMDCOP_DIALER=freeswitch.
Installed AMD Cop by hand before? Move your own dialplan/amdcop.xml aside first: the installer never writes over a dialplan file it did not write. It replaces the script and the key from a hand install itself.
While it runs, Dialers ticks off each step under the command. The last two, First call heard and First verdict returned, tick when the first call reaches AMD Cop, after step 2.

2. Send answered calls to AMD Cop
Send each answered outbound call to amdcop_detect XML amdcop, and say where a person goes. Either set it once, when you install:
curl -fsSL https://api.amdcop.ai/connector.sh | sudo AMDCOP_AGENT_DEST='3000 XML default' bash -s amdcop_live_...or on each call, with amdcop_agent_dest:
originate {amdcop_b_number=447700900123,amdcop_agent_dest='3000 XML default'}sofia/gateway/your_carrier/447700900123 amdcop_detect XML amdcopReplace 3000 XML default with what your dialer does with a live call today: a callcenter queue's extension, your agents' extension, or a transfer back into your own dialplan. Always give all three parts, extension, dialplan and context: an extension alone would be looked for in the amdcop context and not found. A value on the call wins over the one given at install, when it has all three parts. A dialer that parks answered calls on an extension can transfer them to amdcop_detect XML amdcop instead.
A machine is hung up. To play a message first, send machines to your own extension the same way: AMDCOP_MACHINE_DEST='vmdrop XML default' at install, or amdcop_machine_dest on the call.
The server's row on Dialers shows where to send the calls, and whether the server is connected.

Then test with two calls, as in Test it, before you send a whole campaign.
Tell AMD Cop which call it is
Set these on the outbound leg when you originate it, so the portal's call log shows the number, campaign and list for each call:
originate {amdcop_b_number=447700900123,amdcop_campaign=SPRING,amdcop_list=12,origination_caller_id_number=442000000000}sofia/gateway/your_carrier/447700900123 amdcop_detect XML amdcopAlways set amdcop_b_number to the number you dialled. Without it, the script falls back to the SIP To user, which some carriers rewrite. The caller ID is read from origination_caller_id_number when amdcop_a_number is not set.
A dialplan kept somewhere else
Some FreeSWITCH systems, FusionPBX among them, read the dialplan from a database rather than from files, so a context in dialplan/amdcop.xml may never be used. When FreeSWITCH does not load the file, the installer removes it and says so. On FusionPBX, check with a test call either way. Run the script on the answered call in your own dialplan instead:
<action application="lua" data="amdcop.lua"/>
<action application="transfer" data="amdcop_${amdcop_result} XML your_context"/>and add to your_context an extension for amdcop_MACHINE, and one matching ^amdcop_(HUMAN|NOTSURE)?$ for everything else, so a call is sent to an agent even when the script gave no result. Conditions are checked before the script runs, so route on the result with a transfer like this one rather than a condition in the same extension.
Settings
Set once, at install, in front of bash in the install command:
| Setting | Default | What it does |
|---|---|---|
AMDCOP_AGENT_DEST |
none | Where a person, or NOTSURE, goes when the call does not say: extension, dialplan and context |
AMDCOP_MACHINE_DEST |
hang up | Where a machine goes when the call does not say: extension, dialplan and context |
AMDCOP_DRY_RUN |
0 |
1 prints what would change, and changes nothing |
AMDCOP_DIALER |
found | freeswitch on a server that also has Asterisk |
AMDCOP_FS_CLI |
found | Where fs_cli is, when it is not on the PATH or in /usr/local/freeswitch/bin |
A setting left out when you run the command again keeps the value it had. Set it to '' to clear it.
On each call, as channel variables: set them on the originate, or with set in the dialplan before the call reaches amdcop_detect:
| Variable | Default | What it does |
|---|---|---|
amdcop_agent_dest |
AMDCOP_AGENT_DEST |
Where a person, or NOTSURE, goes. Used only with all three parts |
amdcop_machine_dest |
AMDCOP_MACHINE_DEST |
Where a machine goes. Used only with all three parts |
amdcop_b_number |
the SIP To user | The number you dialled |
amdcop_a_number |
origination_caller_id_number |
The caller ID you dialled from |
amdcop_campaign |
none | A campaign name, shown in the portal |
amdcop_list |
none | A list name, shown in the portal |
amdcop_listen_s |
3.5 |
Seconds of the called party to record after the answer |
amdcop_timeout_s |
2 |
Seconds to wait for AMD Cop's answer before NOTSURE |
amdcop_url |
https://pilot.amdcop.ai |
Where the recording is sent |
amdcop_key_file |
amdcop/key.header in FreeSWITCH's configuration folder |
The file holding your key |
The script sets these on the channel for your dialplan, your CDRs and your own reports:
| Variable | Value |
|---|---|
amdcop_result |
HUMAN, MACHINE or NOTSURE |
amdcop_cause |
OK, or why the result is NOTSURE: see Troubleshooting |
amdcop_p_machine |
The model's score, for your records. Route on amdcop_result, not on this number |
amdcop_ms |
Milliseconds from the start of the script to the result |
Detection in the portal does not change what the script does: the amdcop context, and your settings above, decide what each result does.
Test it
- Call your own mobile and answer with a normal "Hello?". It should reach an agent, and the portal's call log shows Human.
- Call a number you know goes to voicemail. No agent should hear it, and the portal shows Machine.
To watch each result on the server, run fs_cli and type /log info. Each call logs one line, such as AMDCOP 6f1c... result=HUMAN p_machine=... cause=OK ms=3720 listened_ms=3539. Dialers shows the server as connected within a minute of the install.
Troubleshooting
Start with the doctor, as root:
sudo /usr/local/lib/amdcop/freeswitch_doctor.shIt checks FreeSWITCH and mod_lua, the script, curl, the key, the amdcop context and where it sends calls, the heartbeat, that the portal takes the key and that the service can be reached, and counts how the last 20 calls were decided. Each line is OK, WARN or FAIL, with the fix where something is wrong. It changes nothing on the server, and what it prints is safe to send to support: no key, phone number or call. Paste it under Check this dialer on Dialers and the failing checks come first. A server installed before the doctor was added has no such file: run the install command again, which keeps where calls go and adds it.

amdcop_cause |
What happened | What to check |
|---|---|---|
OK |
AMD Cop answered | Nothing |
SERVICE_NOTSURE |
AMD Cop heard the call and could not decide, often because the person said nothing | Nothing: these go to an agent |
NO_KEY_FILE |
The key file is missing or unreadable | Run the install command again |
HTTP_401 |
AMD Cop refused the key | That the key is current under Dialers, then run the install command again with it |
NO_ANSWER_IN_TIME |
No answer within amdcop_timeout_s, or AMD Cop could not be reached |
curl -s -o /dev/null -w '%{http_code}' https://pilot.amdcop.ai/classify from the server: any number but 000 means it is reachable (405 is normal); 000 means your firewall |
HANGUP_WHILE_LISTENING |
The called party hung up within 3.5 seconds | Nothing |
HANGUP_BEFORE_START |
The call had ended before the script ran | That calls reach amdcop_detect once answered, not before |
NO_RECORDING |
FreeSWITCH wrote no recording | That /tmp is writable by FreeSWITCH's user |
SCRIPT_ERROR |
The script failed | The AMDCOP script error line in the FreeSWITCH log |
A person who is hung up on with has no amdcop_agent_dest in the log reached AMD Cop with nowhere to go: set AMDCOP_AGENT_DEST at install, or amdcop_agent_dest with all three parts on the call, as in step 2.
Many NO_ANSWER_IN_TIME results, with ms near 5500 in the log, mean the server is far from AMD Cop's service in Germany: from India a reply takes about 1.8 seconds, close to the 2 second limit. Run the dialer nearer, or allow more time with amdcop_timeout_s, knowing a person waits that much longer for an agent.
If calls arrive but the portal shows nothing, the server's address is not registered: run the install command again and compare the address it prints with the one under Dialers.
Roll back
Point your dialer back at what it sent answered calls to before. That alone stops AMD Cop. To remove everything as well, do that first, then:
sudo /usr/local/lib/amdcop/freeswitch_uninstall.shIt removes the amdcop context, the script, the heartbeat and the doctor, and runs reloadxml. The rest of your dialplan stays as it is. A call still sent to amdcop_detect afterwards finds no such extension and is hung up, which is why the dialer goes first. Your key stays in amdcop/key.header until you remove the folder, and stays valid until you revoke it in the portal.
Install by hand
For a server where you would rather not run our installer. Each step is what the installer does for you.
The key. Use the user and group FreeSWITCH runs as if they are not freeswitch, and FreeSWITCH's configuration folder if it is not /etc/freeswitch (fs_cli -x "global_getvar conf_dir" prints it):
sudo mkdir -p /etc/freeswitch/amdcop
echo "Authorization: Bearer amdcop_live_..." | sudo tee /etc/freeswitch/amdcop/key.header > /dev/null
sudo chown -R freeswitch:freeswitch /etc/freeswitch/amdcop
sudo chmod 700 /etc/freeswitch/amdcop
sudo chmod 600 /etc/freeswitch/amdcop/key.headerThe script. Save this as amdcop.lua in FreeSWITCH's scripts folder, which fs_cli -x "global_getvar script_dir" prints. If FreeSWITCH cannot find curl on its own PATH, put curl's full path in the local CURL line. FreeSWITCH reads the script each time a call runs it, so it needs no restart.
If you installed version 1.0 or 1.1, replace it with this one. 1.0 made calls answered at the same moment wait for each other's verdicts, one after another, so on a busy server they came too late and went to agents as NOTSURE. 1.0 and 1.1 also left two recording settings on the call, so your own recording of the rest of the call held the called party without the agent, and a ; in a campaign name made the call NOTSURE:
-- AMD Cop answering-machine detection for FreeSWITCH, version 1.2.
--
-- Run it on the answered leg of an outbound call, before the call is bridged
-- to an agent:
--
-- <action application="lua" data="amdcop.lua"/>
--
-- It records the first seconds of what the called party says, sends them to
-- AMD Cop, and sets four channel variables:
--
-- amdcop_result HUMAN, MACHINE or NOTSURE
-- amdcop_p_machine how sure the model was that it is a machine, 0 to 1
-- amdcop_cause OK, or why the answer is NOTSURE
-- amdcop_ms milliseconds from the start of this script to the answer
--
-- It never fails the call. Any problem (no key, no network, a slow answer, a
-- bad recording) gives NOTSURE, and your dialplan decides what NOTSURE does.
--
-- Settings, each a channel variable that may be set before this runs:
--
-- amdcop_url default https://pilot.amdcop.ai
-- amdcop_key_file default amdcop/key.header in FreeSWITCH's conf_dir,
-- which is /etc/freeswitch/amdcop/key.header on most
-- packages
-- amdcop_listen_s seconds to listen after answer, default 3.5
-- amdcop_timeout_s seconds to wait for the answer, default 2
-- amdcop_campaign sent with the call, shown in the portal (optional)
-- amdcop_list sent with the call, shown in the portal (optional)
-- amdcop_a_number the caller ID you dialled from (optional; read from
-- the channel when not set)
-- amdcop_b_number the number you dialled (optional; read from the
-- channel when not set)
-- Where the recording is sent when amdcop_url is not set. The installer
-- rewrites this one line when it is told to use another address.
local DEFAULT_URL = "https://pilot.amdcop.ai"
-- The curl that sends it. The installer writes the full path of the curl it
-- checked, which FreeSWITCH's own PATH may not reach.
local CURL = "curl"
local function setting(name, default)
local value = session:getVariable(name)
if value == nil or value == "" then
return default
end
return value
end
-- The first of these channel variables that has a value.
local function first_of(names)
for _, name in ipairs(names) do
local value = session:getVariable(name)
if value ~= nil and value ~= "" then
return value
end
end
return nil
end
local api = freeswitch.API()
local started_ms = api:getTime()
local listened_ms = 0
local function finish(result, p_machine, cause)
local took_ms = api:getTime() - started_ms
session:setVariable("amdcop_result", result)
session:setVariable("amdcop_p_machine", p_machine or "")
session:setVariable("amdcop_cause", cause)
session:setVariable("amdcop_ms", tostring(took_ms))
freeswitch.consoleLog("INFO", string.format(
"AMDCOP %s result=%s p_machine=%s cause=%s ms=%d listened_ms=%d\n",
session:get_uuid(), result, p_machine or "-", cause, took_ms, listened_ms))
end
-- JSON string values are written by us and by our API only, so a strict
-- encoder is enough: escape the two characters that could break the string.
local function json_text(value)
value = tostring(value or "")
value = value:gsub("\\", "\\\\"):gsub('"', '\\"')
return '"' .. value .. '"'
end
-- Single quotes for the shell: the only character that needs care is ' itself.
local function shell_quote(value)
return "'" .. tostring(value):gsub("'", "'\\''") .. "'"
end
local function file_exists(path)
local handle = io.open(path, "r")
if handle then
handle:close()
return true
end
return false
end
-- The whole of a file, removed once read. Empty when there is no file.
local function take_file(path)
local handle = io.open(path, "r")
if not handle then
return ""
end
local text = handle:read("*a") or ""
handle:close()
os.remove(path)
return text
end
-- Sets a channel variable and returns a function that puts it back as it
-- was, unset included.
local function set_for_now(name, value)
local before = session:getVariable(name)
session:setVariable(name, value)
return function()
if before == nil then
session:execute("unset", name)
else
session:setVariable(name, before)
end
end
end
-- Beside FreeSWITCH's own configuration, wherever this server keeps it.
local function default_key_file()
local conf_dir = freeswitch.getGlobalVariable("conf_dir")
if conf_dir == nil or conf_dir == "" then
conf_dir = "/etc/freeswitch"
end
return conf_dir .. "/amdcop/key.header"
end
local function run()
if not session:ready() then
return finish("NOTSURE", nil, "HANGUP_BEFORE_START")
end
local url = setting("amdcop_url", DEFAULT_URL)
local key_file = setting("amdcop_key_file", default_key_file())
local listen_s = tonumber(setting("amdcop_listen_s", "3.5")) or 3.5
local timeout_s = tonumber(setting("amdcop_timeout_s", "2")) or 2
if not file_exists(key_file) then
return finish("NOTSURE", nil, "NO_KEY_FILE")
end
local uuid = session:get_uuid()
local wav = string.format("/tmp/amdcop-%s.wav", uuid)
-- Only what the called party says (RECORD_READ_ONLY), in mono 16-bit,
-- which is what AMD Cop reads. A background recording and a timed sleep
-- end on the clock; the `record` application ran up to a second over.
-- FreeSWITCH reads both settings as a recording starts, so they are put
-- back straight after: left set, the customer's own recording of the rest
-- of the call would hold the called party alone, without the agent.
local restore_read_only = set_for_now("RECORD_READ_ONLY", "true")
local restore_stereo = set_for_now("RECORD_STEREO", "false")
local listen_from = api:getTime()
local started, problem = pcall(session.execute, session, "record_session", wav)
restore_read_only()
restore_stereo()
if not started then
error(problem)
end
session:sleep(math.floor(listen_s * 1000))
session:execute("stop_record_session", wav)
listened_ms = api:getTime() - listen_from
if not session:ready() then
os.remove(wav)
return finish("NOTSURE", nil, "HANGUP_WHILE_LISTENING")
end
if not file_exists(wav) then
return finish("NOTSURE", nil, "NO_RECORDING")
end
local call = "{"
.. '"uniqueid":' .. json_text(uuid)
.. ',"a_number":' .. json_text(first_of({"amdcop_a_number",
"origination_caller_id_number", "effective_caller_id_number"}))
.. ',"b_number":' .. json_text(first_of({"amdcop_b_number",
"sip_to_user", "dialed_user"}))
.. ',"campaign_id":' .. json_text(session:getVariable("amdcop_campaign"))
.. ',"list_id":' .. json_text(session:getVariable("amdcop_list"))
.. "}"
-- The key is read by curl from a file (-H @file), so it never appears in
-- the process list. The call's details go as a plain string
-- (--form-string): with -F, a ";" in a campaign name would end the field.
-- curl writes the answer and its HTTP code to files, and os.execute waits
-- for this call's curl alone. Version 1.0 read curl through io.popen, which
-- made calls answered at the same moment wait for each other's answers,
-- one after another (10 at once waited up to 14.5 s).
local reply_file = string.format("/tmp/amdcop-%s.reply", uuid)
local code_file = reply_file .. ".code"
local command = table.concat({
shell_quote(CURL), "-s --max-time", tostring(timeout_s),
"-H", shell_quote("@" .. key_file),
"-F", shell_quote("audio=@" .. wav .. ";type=audio/wav"),
"--form-string", shell_quote("call=" .. call),
"-o", shell_quote(reply_file),
"-w", shell_quote("%{http_code}"),
shell_quote(url .. "/classify"),
">", shell_quote(code_file),
}, " ")
os.execute(command)
os.remove(wav)
local body = take_file(reply_file)
local code = take_file(code_file):match("^(%d%d%d)$")
if code == nil or code == "000" then
return finish("NOTSURE", nil, "NO_ANSWER_IN_TIME")
end
if code ~= "200" then
return finish("NOTSURE", nil, "HTTP_" .. code)
end
local verdict = body:match('"verdict"%s*:%s*"(%u+)"')
local p_machine = body:match('"p_machine"%s*:%s*([%d%.eE%-]+)')
if verdict ~= "HUMAN" and verdict ~= "MACHINE" then
return finish("NOTSURE", p_machine, "SERVICE_NOTSURE")
end
return finish(verdict, p_machine, "OK")
end
local ok, problem = pcall(run)
if not ok then
freeswitch.consoleLog("ERR", "AMDCOP script error: " .. tostring(problem) .. "\n")
pcall(finish, "NOTSURE", nil, "SCRIPT_ERROR")
endThe context. Save this as dialplan/amdcop.xml in FreeSWITCH's configuration folder, with your own agents in place of 3000 XML default, then run fs_cli -x reloadxml:
<include>
<context name="amdcop">
<extension name="amdcop_detect">
<condition field="destination_number" expression="^amdcop_detect$">
<action application="lua" data="amdcop.lua"/>
<action application="transfer" data="amdcop_${amdcop_result} XML amdcop"/>
</condition>
</extension>
<extension name="amdcop_agent">
<condition field="destination_number" expression="^amdcop_(HUMAN|NOTSURE)?$">
<action application="transfer" data="3000 XML default"/>
</condition>
</extension>
<extension name="amdcop_machine">
<condition field="destination_number" expression="^amdcop_MACHINE$">
<action application="hangup" data="NORMAL_CLEARING"/>
</condition>
</extension>
</context>
</include>Register the server, once, from the server itself. The answer includes the address AMD Cop saw, which is the one it registers. A 409 means that address belongs to another account or your plan has no room for another address; 402 means the plan has no room for another dialer:
curl -s https://api.amdcop.ai/v1/connector/register -H @/etc/freeswitch/amdcop/key.header -H "Content-Type: application/json" -d '{"dialer_type":"freeswitch","description":"FreeSWITCH 1"}'The heartbeat, in the crontab of the user FreeSWITCH runs as:
* * * * * curl -s -m 10 -o /dev/null https://api.amdcop.ai/v1/connector/heartbeat -H @/etc/freeswitch/amdcop/key.header -H "Content-Type: application/json" -d '{}'To roll a hand install back, delete amdcop.lua, dialplan/amdcop.xml and the crontab line, run fs_cli -x reloadxml, and remove the amdcop folder.
Next: Switching from AMDY