Asterisk AmoCRM integration
Currently working: Incoming calls through Asterisk are visible to the manager in AmoCRM (a popup appears and the call is shown in the customer card); outgoing calls from the AmoCRM page are placed (see amocrm.php, item 5); recorded calls play back; CDRs are uploaded to AmoCRM. (This is unclear; I could not find a useful explanation from the vendor.)
Setup
Web server on your host
AmoCRM connects to it. HTTPS is required on the web server for AmoCRM!
Asterisk
/etc/asterisk/manager.conf
[general]
enabled = yes
port = 5038
bindaddr = 127.0.0.1 ; No need to use 0.0.0.0 for a local connection
webenabled = yes ; DO NOT FORGET
httptimeout = 60 ;
debug = on; if necessary
[amocrm]
secret = SECRET
deny = 0.0.0.0/0.0.0.0
permit = 127.0.0.1/255.255.255.0
read = cdr,reporting,originate
write = reporting,originate
Your extension for incoming calls. You need the USER\_HASH from AmoCRM. Find it in AmoCRM under Settings -> API (an extended plan is required).
This is an example from my configuration; adapt it for yours.
# INCOMING CALLS
[ukrtelecom-income]
exten => _0441234567,1,Noop()
same => n,Set(CALLTYPE=in)
; Look up the caller name in AmoCRM
same => n,Set(CALLERID(name)=${SHELL(wget -O - --quiet "https://domain.amocrm.ru/private/acceptors/asterisk_new/?number=${CALLERID(num)}&[email protected]&USER_HASH=555" | cut -d "|"
; Call the responsible manager
same => n,Set(TOEXT=${SHELL(wget -O - --quiet "https://domain.amocrm.ru/private/acceptors/asterisk_new/?redirect=Y&number=${CALLERID(num)}&[email protected]&USER_HASH=555")}).
; Record the conversation
same => n,Gosub(sub-mixmonitor,${EXTEN},1)
same => n,Dial(SIP/${TOEXT},,tTr)
same => n,HangUp()
Verifying the setup
Check the AMI connection
asterisk-test-connection-to-ami-by-telnet
Check external requests to the web service on your host
# Request 1:
https://asterisk.domain.com/amocrm.php?_login=amocrm&_secret=SECRET&_action=test_cdr
# Response:
asterisk_cb({"status":"ok","data":"connection ok"});
# Request 2:
https://asterisk.domain.com/blabla.php?_login=amocrmuser&_secret=SECRET&_action=status
# Response:
asterisk_cb({"status":"ok","action":"status","data":[]});
# The data array may contain values
Troubleshooting
amocrm.php 1. AmoCRM allows a phone number such as +38050\[440А99\*30 to be entered. Fix this.
elseif ($action === 'call') {
//...
$to = strval($_GET['to']);
$to = preg_replace("/[^0-9]/", '', $to);
$params=array(
'action'=>'Originate',
'channel'=>'SIP/'.$from),
'Exten'=> $to,
2. If the user has no internal number configured for the integration, AmoCRM may still place calls from it. Do not pass such requests to Asterisk.
elseif ($action === 'call') {
//...
$from=intval($_GET['from']);
// no from value was supplied; do not send the request to Asterisk
if ($from == 0) {
answer(array('status'=>'error','data'=>array('message'=>'No From Number')));
}
$params=array(
'action'=>'Originate',
'channel'=>'SIP/'.$from,
'Exten'=> $to,
3. Specify your own CONTEXT
elseif ($action === 'call') {
//...
$params=array(
'action'=>'Originate',^M
'channel'=>'SIP/'.intval($_GET['from']),^M
'Exten'=>strval($_GET['to']),^M
'Context'=>'from-internal', .// SPECIFY YOUR VALUE HERE
'priority'=>'2',
'Callerid'=>'"'.strval($_GET['as']).'" <'.intval($_GET['from']).'>',
'Async'=>'Yes',
- When returning CDRs, the script adjusts the event time by the DELTA set at the beginning of the file:
} elseif ($action==='cdr'){ // fetch call history
//...
foreach ($r as $k=>$v) $r[$k]['calldate']=date('Y-m-d H:i:s',strtotime($v['calldate'])-AC_TIME_DELTA*3600);
//...
}
5. Outgoing calls in CDR. The issue: amocrm.php exports the SRC and DST fields to the CDR. When a call is placed through an external SIP provider, SRC contains the external landline number, so the record looks like this:
{"calldate":"2017-05-31 06:54:46","src":"0442901234","dst":"050123456","duration":"16","billsec":"16"
I assume AmoCRM cannot retrieve such a record because it does not recognize the number. In our Asterisk configuration, the CDR contains the actual number of the internal caller. After a small adjustment to amocrm.php, the result is: