Mypbx and Vtiger CRM Integration Configuration Guide: Yeastar Information Technology Co. LTD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

MyPBX and Vtiger CRM Integration

Configuration Guide

Version 1.0

Yeastar Information Technology Co. Ltd.


Integration MyPBX and Vtiger CRM

Overview

About MyPBX AMI


MyPBX provides an AMI (Asterisk Manager Interface) that allows a client program to
connect to an Asterisk instance and issue commands or read events over TCP/IP
stream. The default port for MyPBX AMI is TCP 5038.

About Vtiger CRM


The Vtiger CRM provides a module called PBX Manager Module that will allow users
to use Vtiger to create calls and receive notifications on incoming calls using the
Asterisk Call Manager API provided by Asterisk. This document describes
configuration of Vtiger CRM to work with MyPBX.
Vtiger CRM download link:
https://wiki.vtiger.com/index.php/Installation_with_EXE

In this document, we will guide you to achieve the following features:


1. Click to call. Make calls using MyPBX extension by clicking on phone numbers
(links) within Vtiger.
2. Incoming call popup. Provides notification within Vtiger giving details of the caller
when a call comes to the user.
3. View all past calls under PBX Manager module.

Configuring Vtiger CRM


NOTE: in this guide, we shall install Vtiger CRM on a windows host. You can also
install it on a Linux host. Once you are done with Vtiger CRM installation, you will
have to connect with MyPBX.
1. Login Vtiger CRM with the username admin and the password you selected
during the installation.

Figure 1

2/11
Integration MyPBX and Vtiger CRM

2. Go to Settings Icon >CRM Settings >Module Manager.

3. Click on Settings Icon next to PBX Manager module.


4. You will be redirected to Settings page where you have to provide soft phone
configuration details.

Figure 2

• Asterisk server IP : MyPBX IP, in this example: 192.168.4.148


• Asterisk sever port: 5038
• Asterisk username: MyPBX username
• Asterisk password: MyPBX password
• Astersik version: MyPBX uses Asterisk 1.6, here we choose 1.6

5. Click on the button to save the configuration.

Setting User Extensions


Vtiger CRM allows we to create MyPBX extensions for each user , which will let us
call or receive call for that user. The following is steps to set User Extensions in Vtiger
CRM.

1. Login as any user and go to Settings Icon , click on the My Preferences link
on the top right of the page. On this page you will find a block labeled Asterisk
Configuration.
2. Asterisk Extension: MyPBX extension, here take 705 for example.
3. Receive Incoming Calls: check the box, yes for enabling incoming calls.

3/11
Integration MyPBX and Vtiger CRM

Figure 3

Click to Call
Till now, we have finished all the configurations on Vtiger CRM, but it is not over yet.
There are steps we should do to make it work with MyPBX.
1. Edit the file AsteriskClass.php. Since we install Vtiger on Windows, so here we will
find the file in the path:
\Vtigercrm-5.4.0\apache\htdocs\VtigerCRM\modules\PBXManager\utils\
AsteriskClass.php
The file on Linux will be found in:
www/html/Vtigercrm/modules/PBXManager/utils/AsteriskClass.php
In order to find the correct context, we will change the context according to the
contexts in MyPBX. For example, context for extension 705 on MyPBX is
[DLPN_DialPlan705]. Therefore, we will firstly get the extension number using the
sentence “$extensionnumber = split('\/',$from);” Then change the context with”
$context= "DLPN_DialPlan".$extensionnumber[1];”
• Add the two sentences:
$extensionnumber = split('\/',$from);
$context= "DLPN_DialPlan".$extensionnumber[1];
• Comment the section “switch” to disable it.
• Modified section of the file:
$extensionnumber = split('\/',$from);
$context= "DLPN_DialPlan".$extensionnumber[1];
/*
switch($typeCalled){
case "SIP":
$context = " from-internal";
break;
case "PSTN":
$context =" ";//"outbound-dialing";
break;
default:
$context = " ";
}*/

• Save the file.

2. Make an outgoing call to test.


• Choose one number link to click, once clicked, it will display a popup notifying
the outgoing call being made.

4/11
Integration MyPBX and Vtiger CRM

Figure 4

Figure 5
• The extension you saved in User Preferences will start getting a call. In this guide,
705 will ring.
• Pick up the call (705), a call will be made to the number clicked in Vtiger CRM
(here is 702).
• Check the call log in PBX Manager.

Figure 6

Incoming call popup


To receive incoming calls in Vtiger, we need to modify configuration file in Vtiger. The
following is steps to achieve this feature pop-up call.
1. Modify file AsteriskClient.php
• Find the configuration file AsteriskClient.php in the path:
\Vtigercrm-5.4.0\apache\htdocs\VtigerCRM\cron\modules\PBXManager
On Linux, the file will be found in:
/var/www/html/Vtigercrm/cron/modules/PBXManager/AsteriskClient.php
• Open the file and add a function in the file:handleIncomingCalls
• Invoke the function handleIncomingCalls in the function
Asterisk_handleEvents. Please refer to the Appendix.
• In the function asterisk_handleResponse4 , add “else
if($mainresponse['Event'] == 'Dial'&& $mainresponse['SubEvent'] == 'Begin'){}”
behind
“else if($mainresponse['Event'] == 'OriginateResponse'){ }”.For the detail,
please refer to the Appendix.
• Please refer to the modified section of AsteriskClient.php in the Appendix.

5/11
Integration MyPBX and Vtiger CRM

• Save the file.


• Enter the directory where the file AsteriskClient.php in with the CLI or terminal.
Here we will use a Command prompt Window to enter the directory:
\Vtigercrm-5.4.0\apache\htdocs\VtigerCRM\cron\modules\PBXManager
Input the command: phpAsteriskClient.php to execute the file. If it succeeds,
you will see the picture as below:

Figure 7
For Linux System, you need use terminal to do the operation.

2. Make a call to the user extension to test. Here we will call from 702 to 705. Once
the call is set up, the users will get a pop-up notifying the caller name (if the
number is present in Vtiger CRM) and the number.

Figure 8

6/11
Integration MyPBX and Vtiger CRM

[Appendix]

Modified section of AsteriskClient.php


function asterisk_handleEvents($asterisk, $adb, $version="1.4") {
$fnEntryTime = time();
//values of flag for asteriskincomingevents(-1 for stray calls, 0 for incoming calls,
1 for outgoing call)
Do
$mainresponse = $asterisk->getAsteriskResponse();

if(!empty($mainresponse)) {
$state = ($version == "1.6")? "ChannelStateDesc" : "State";

if(asterisk_handleResponse1($mainresponse, $state, $adb)) {


if(asterisk_handleResponse2($mainresponse, $adb, $asterisk,
$state)) {
if(handleIncomingCalls($asterisk, $adb, $version)) {/* Invoke
the function handleIncomingCalls*/
//if(asterisk_handleResponse3($mainresponse, $adb,
$asterisk)){
// Proceed if previous event could not be handled.
}
}
}
} else {
// No more response to consume
break;
}
} while(true);

return false;
}

function asterisk_handleResponse4($mainresponse, $adb, $asterisk, $state) {


$appdata = $mainresponse['AppData'];

$uniqueid = $channel = $callerType = $extension = null;


$parseSuccess = false;

if(
$mainresponse['Event'] == 'Newexten' && (strstr($appdata,
"__DIALED_NUMBER") || strstr($appdata, "EXTTOCALL"))
){

7/11
Integration MyPBX and Vtiger CRM

$uniqueid = $mainresponse['Uniqueid'];

$channel = $mainresponse['Channel'];
$splits = explode('/', $channel);
$callerType = $splits[0];

$splits = explode('=', $appdata);


$extension = $splits[1];

$parseSuccess = true;
} else if($mainresponse['Event'] == 'OriginateResponse'){
//if the event is OriginateResponse then its an outgoing call and set the flag to
1, so that AsteriskClient does not pick up as incoming call
$uniqueid = $mainresponse['Uniqueid'];
$adb->pquery("UPDATE vtiger_asteriskincomingevents set flag = 1 WHERE
uid = ?", array($uniqueid));
}else if($mainresponse['Event'] == 'Dial'&& $mainresponse['SubEvent'] ==
'Begin'){
$uniqueid = $mainresponse['UniqueID'];

$channel = $mainresponse['Channel'];
$splits = explode('/', $channel);
$callerType = $splits[0];
$extension = $mainresponse['Dialstring'];
$parseSuccess = true;
}
/* Add else if($mainresponse['Event'] == 'Dial'&& $mainresponse['SubEvent'] ==
'Begin'){ } */

/*else if($mainresponse['Event'] == 'Hangup'){


$uniqueid = $mainresponse['Uniqueid'];

$channel = $mainresponse['Channel'];
$splits = explode('/', $channel);
$callerType = $splits[0];

$extension = $mainresponse['CallerIDNum'];

$parseSuccess = true;
}
*/

if($parseSuccess) {

8/11
Integration MyPBX and Vtiger CRM

if(checkExtension($extension, $adb)) {

$sql = "UPDATE vtiger_asteriskincomingevents SET to_number=?,


callertype=?, timer=?, flag=? WHERE uid=?";
$adb->pquery($sql, array($extension, $callerType, time(), 0, $uniqueid));
echo "UPDATE vtiger_asteriskincomingevents SET
to_number=$extension, callertype=$callerType, timer=".time().", flag=0 WHERE
uid=$uniqueid\n";
//echo "xxxxxxxxxxxxxxx ".$adb->num_rows($result)."\n";
$callerinfo = $adb->pquery("SELECT from_number,from_name FROM
vtiger_asteriskincomingevents WHERE uid = ?",array($uniqueid));
echo $uniqueid." fffffffffffffffffffff
".$adb->num_rows($callerinfo)."\n";
if($adb->num_rows($callerinfo) > 0){
$callerNumber = $adb->query_result($callerinfo, 0, "from_number");
$callerName = $adb->query_result($callerinfo, 0, "from_name");

if(empty($callerNumber) || $callerNumber == '0') {


// We don't have the information who is calling, could happen in
Asterisk 1.4 (when call is made to Queue)
// Let us defer the popup show for next Event: Link
$sql = "UPDATE vtiger_asteriskincomingevents SET flag=?
WHERE uid=?";
$adb->pquery($sql, array(-1, $uniqueid));
} else {
$query = "INSERT INTO vtiger_asteriskincomingcalls (refuid,
from_number, from_name, to_number, callertype, flag, timer) VALUES(?,?,?,?,?,?,?)";
$adb->pquery($query,array($uniqueid, $callerNumber,
$callerName, $extension, $callerType, 0, time()));

}
}
}

return false;
}

return true;
}

Add function handleIncoming Calls:

function handleIncomingCalls($asterisk, $adb, $version="1.4"){

9/11
Integration MyPBX and Vtiger CRM

$response = $asterisk->getAsteriskResponse();
if(empty($response)){
return false;
}

$callerNumber = "Unknown";
$callerName = "Unknown";
if($version == "1.6"){
$state = "ChannelStateDesc";
}else{
$state = "State";
}

if(($response['Event'] == 'Newstate' || $response['Event'] == 'Newchannel') &&


($response[$state] == 'Ring' || $response[$state] == 'Ringing')){

if(!empty($response['CallerID'])){
$callerNumber = $response['CallerID'];
}elseif(!empty($response['CallerIDNum'])){
$callerNumber = $response['CallerIDNum'];
}
if(!empty($response['CallerIDName'])){
$callerName = $response['CallerIDName'];
}
$unique_id = $response['Uniqueid'];
while(true){
$response = $asterisk->getAsteriskResponse();
if(($response['Event'] == 'Dial') && ($response['CallerIDNum'] ==
$callerNumber) && ($response['UniqueID'] == $unique_id)){

$dest_unique_id = $response['DestUniqueID'];
while (true) {
$response = $asterisk->getAsteriskResponse();
if (($response['Event'] == 'Newstate') &&
($response['ChannelStateDesc'] == 'Ringing') && ($response['Uniqueid'] ==
$dest_unique_id)) {
if(strstr($response['Channel'], $callerNumber)){
$temp = explode("/",$response['Channel']);
$callerType = $temp[0];
}
break;
}
}
break;

10/11
Integration MyPBX and Vtiger CRM

}
}
}else{
return false;
}
}

<The end>

11/11

You might also like