Script - Snom phone Action URL to FusionPBX Caller ID

Status
Not open for further replies.

EasyBB

Active Member
Oct 23, 2016
240
33
28
Australia
I use this script to gather caller id information from my colleagues.

This script is written to work with Snom phones but can be modified to suit any phone that can hit an http url when certain events happen on the phone. I use Snom's "Incoming Call/ On Connected" Action URL field to fire this script. Obviously you'll be able to extend this script to do many more things than just sending emails. You'll also need to adjust the URL depending on where you put this script on the server.

Just a word of caution: If you are on a VPS or have a public IP on your PBX, please make sure the script is NOT writable by www-data or the user running the web server.

Please refer to my mods to the Caller ID Lookup also: http://www.fusionpbxforums.com/threads/caller-id-lookup-cidlookup-from-contacts.20/

PHP:
<?php
/**
 * getcontact.php    v17041501    contributed to fusionpbxforums.com by EasyBB
 * This PHP script is indended to be used for getting caller info from a Snom phone
 * and then sending an email requesting the caller name in case we don't have
 * the caller in the FusionPBX/other contacts database.
 * Snom phone's appropriate action URL must be configured to send the request to this script.
 * See example below:
 * Action URL: http://your-web-server/contact/getcontact.php?cname=$display_remote&extension=$user_name1
 * Prerequisites: PHP 5.4+, MTA (postfix, sendmail etc), Linux
 **/
 
 // Set some variables
 $mail_sub = 'Caller Info'; // Email subject
 $from_name = 'IT Admin.(itadmin@example.com)'; // Email body from name
 
 // Define extensions and corresponding user info
 $extensions = [
    '151' => ['Employee1', 'Employee1@example.com'],
    '152' => ['Employee2', 'Employee2@example.com'],
    '153' => ['Employee3', 'Employee3@example.com']
 ];

if (isset($_GET['cname']) && isset($_GET['extension']))
{
    // Get variables from URL
    $callerInfo = htmlspecialchars(trim($_GET['cname']), ENT_QUOTES);
    $calleeNum = htmlspecialchars(trim($_GET['extension']), ENT_QUOTES);
    
    // Check if callee extension number exists in extensions array
    if(array_key_exists($calleeNum, $extensions))
    {
        $calleeName = $extensions[$calleeNum][0];
        $calleeMail = $extensions[$calleeNum][1];
    }
    else
    {
        $calleeName = $calleeNum; // Use extension number in case name not found.
        $calleeMail = 'itadmin@example.com'; // Use generic email as "catch all".
    }
    
    // Generate caller data
    // Caller info from a Snom phone would look like: John Doe (Cell) 0123456789
    $callerInfo = explode(' ', $callerInfo);
    
    // Caller id number is the last array member, so grab and remove it from array
    $callerNum = trim(array_pop($callerInfo));
    
    // Let's put together Caller Name from the remaining array
    $callerName = '';
    foreach ($callerInfo as $cn)
    {
        $callerName .= $cn . ' ';
    }
    
    // Trim any whitespaces after the name
    $callerName = trim($callerName);

    // Set a known value if caller name is empty
    if (empty($callerName))
    {
        $callerName = 'UNKNOWN';
    }

    // Prepare data to append to the contacts file
    // $person = date("Y-m-d H:i:s") . ' -> ' . $callerName . ' : ' . $callerNum . "\n";

    // The contacts file to write to
    // $file = 'contacts.txt';

    // Write data to the file
    // file_put_contents($file, $person, FILE_APPEND | LOCK_EX);

    // Prepare email and execute mail command
    if (($callerName == 'UNKNOWN') || ($callerName == $callerNum))
    {
        $msg = 'Hi ' . $calleeName . ", \n";
        $msg .= 'You have just received a call from: ' . $callerNum . ". \n";
        $msg .= 'Since there is no name attached to the number, could ' ;
        $msg .= 'you please forward this mail to me with a name? Thanks!' ;
        $msg .= "\n\n";
        $msg .= 'Ragards,' . "\n" ;
        $msg .= $from_name . "\n\n";
        $msg .= '=====================' . "\n";
        $msg .= 'This is an automated email.';

        exec("echo '" . $msg . "' | mail -s '". $mail_sub ."' " . $calleeMail ."");
        // echo 'Email sent! <br />';
    }
    else
    {
        // echo 'Email not sent! <br />'; // Caller name is known
    }
}
else
{
    die(''); // Get request does not have caller and callee info
}

?>
 
  • Like
Reactions: yukon
Status
Not open for further replies.