Vicidial Customised Spy function

Here is the script you can use to enable spy function to VICIDIAL with the phone. to spy a call conversation you can use code *1 and then the extension number to spy it. If you want to do a normal spy via panel then you can find how-to here: http://omid-mohajerani.blogspot.com/2020/02/vicidial-monitor-and-barge.html

in this dialplan you can restrict access to these functions just to specific phones. (for supervisors)

[AstiosDialSpy]
exten => _*1XXXX,1,NoOP(————- ${CALLERID(num)} is Spying ${EXTEN:2} ————- )
         same => n,AGI(astiosdial/spy.php,${CALLERID(num)},${EXTEN:2})
         same => n,Wait(1)
         same => n,Chanspy(SIP/${EXTEN:2},q)
         same => n,Hangup

exten => _*2XXXX,1,NoOP(————- ${CALLERID(num)} is Whispering ${EXTEN:2} ————- )
         same => n,AGI(astiosdial/spy.php,${CALLERID(num)},${EXTEN:2})
         same => n,Wait(1)
         same => n,Chanspy(SIP/${EXTEN:2},qw)
         same => n,Hangup

exten => _*3XXXX,1,NoOP(————- ${CALLERID(num)} is Barging ${EXTEN:2} ————- )
         same => n,AGI(astiosdial/spy.php,${CALLERID(num)},${EXTEN:2})
         same => n,Wait(1)
         same => n,Chanspy(SIP/${EXTEN:2},qB)
         same => n,Hangup

vim /var/lib/asterisk/agi-bin/astiosdial/spy.php

!/usr/bin/php -q
<?php
error_reporting(E_ALL); // only in development environment
set_time_limit(30);
require(‘LIB/phpagi.php’);
//Database Connection Details
$servername = “127.0.0.1”;
$username   = “mydbusername”;
$password   = “mydbpassword”;
$dbname     = “astiosdial”;
$tablename  = “spy”;
// Asterisk Variables
$now = date(“Y-m-d H:i:s”);
$callerid  = $argv[1];
$exten     = $argv[2];
//
$agi = new AGI();
$agi->answer();

$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die(“Connection failed: ” . $conn->connect_error);
}

$sql = “SELECT * FROM spy WHERE callerid = $callerid”;
$result = $conn->query($sql);
$permission = “NOTOK” ;
if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        $exten_array = explode( “,”, $row[“exten”] );
       foreach($exten_array as $key)
        {
        //  $agi->verbose(” exten is $exten and key is  $key”);//display values
          if ($exten == $key ) { $permission=”OK”; }
        }
    }
    if ($permission == “OK”) { $agi->verbose(“User $callerid have access to spy exten $exten”); return;}
    elseif ($permission == “NOTOK”) {
            $agi->stream_file(“beep”);
            $agi->stream_file(“beep”);
            $agi->stream_file(“beep”);
            $agi->stream_file(“astiosdial-noaccesstomonitorthisextension”);
            $agi->Hangup();
        }
} else {
    $agi->verbose( “User $callerid doesnt have spy permission”);
    $agi->stream_file(“beep”);
    $agi->stream_file(“beep”);
    $agi->stream_file(“beep”);
    $agi->stream_file(“astiosdial-noaccesstothisfeature”);
    $agi->Hangup();
}
$conn->close();
?>

Table structure:

CREATE TABLE `spy` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `callerid` varchar(20) NOT NULL,
  `exten` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
)