Open Source » Facebook Message Notify

Operating System[s]: AnyThis script simply logs into Facebook using the predefined username and password combination. Upon successful login, it begins a loop that checks every X seconds for any new (unseen) messages. If there are no new messages, the script repeats the cycle. If there are new messages, the script plays a sound notification and informs the user as to the total number of new/unseen messages.

<?php
/************************************************
** Facebook Message Notify [proof of concept]  **
** Created on July 14th, 2010 at 6AM CST       **
*************************************************
** Copyright (C) 2010 adam@papsy.net           **
************************************************/
// THE USERNAME [TO LOGIN WITH]
$un "adam@papsy.net";
// THE PASSWORD [FOR THE ABOVE ACCOUNT]
$pw "notmyrealpassword";
// THE DELAY [IN SECONDS] BETWEEN CHECKS
$dl 60;
// DELETE ANY EXISTING COOKIES
@unlink("facebookcookies.txt");
// CREATE THE CURL OBJECT
$ch curl_init();
// USE A LEGITIMATE USER-AGENT
$ua "Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20100625 Firefox/3.6.6";
// SEND USER-AGENT [DEFINED ABOVE]
$cc curl_setopt($chCURLOPT_HTTPHEADER, array("User-Agent: $ua"));
// CAPTURE HTTP RESPONSE HEADERS
$cc curl_setopt($chCURLOPT_HEADER1);
// RETURN THE SERVER RESPONSE [RATHER THAN DISPLAYING IT]
$cc curl_setopt($chCURLOPT_RETURNTRANSFER1);
// IGNORE SSL CERTIFICATE
$cc curl_setopt($chCURLOPT_SSL_VERIFYHOST0);
$cc curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
// SET COOKIE FILE PATH
$cc curl_setopt($chCURLOPT_COOKIEJAR"facebookcookies.txt");
$cc curl_setopt($chCURLOPT_COOKIEFILE"facebookcookies.txt");
// ACCESS THE INDEX PAGE [TO GET COOKIES]
$cc curl_setopt($chCURLOPT_URL"http://www.facebook.com"); 
$cc curl_exec($ch);
// SET THE POST URL [FOR LOGIN]
$purl  "https://login.facebook.com/login.php?login_attempt=1";
// BUILD THE POST DATA [FOR LOGIN]
$pdat  "charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&";
$pdat .= "locale=en_US&email=".URLEncode($un)."&pass=".URLEncode($pw)."&persistent=1&lsd=5wpTb";
// DETERMINE THE CURRENT TIME
$ctime get_time();
// DISPLAY THE STATUS MESSAGE [BEFORE LOGGING IN]
print("[$ctime] Logging in as $un\t: ");
// EXECUTE THE REQUEST [ATTEMPT LOGIN]
$pret  post_contents($purl$pdat);
// CHECK IF THE LOGIN WAS UNSUCCESSFUL
if (!strstr($pret"Location: http://www.facebook.com/home.php")) {
    
// IF SO, DISPLAY AN ERROR MESSAGE AND HALT EXECUTION
    
die("Invalid username or password!\r\n");
} else {
    
// OTHERWISE, DISPLAY SUCCESS MESSAGE AND PROCEED
    
print("Success!\r\n");
}

// BEGIN [INFINITE] LOOP
while (<> 2) {
    
// CHECK NEW COUNT
    
get_new();
    
// WAIT FOR X SECONDS
    
sleep($dl);
}

// DETERMINES HOW MANY MESSAGES ARE "NEW" [AS IN, UNSEEN - NOT UNREAD]
function get_new() {
    
// DETERMINE THE CURRENT TIME
    
$ctime get_time();
    
// DISPLAY STATUS MESSAGE
    
print("[$ctime] Getting new message count\t: ");
    
// DOWNLOAD THE HOME PAGE
    
$html get_contents("http://www.facebook.com/");
    
// EXTRACT THE UNSEEN COUNT
    
preg_match('|<span id="jewelInnerUnseenCount">(.+?)</span>|'$html$unseen);
    
// MAKE SURE A COUNT HAS BEEN DETERMINED
    
if (count($unseen) <> 2) { 
        
// IF NOT, DISPLAY AN ERROR MESSAGE AND HALT EXECUTION
        
die("UNKNOWN!\r\n");
    } 
// OTHERWISE, PROCEED TO GRAB THE COUNT
    
$count $unseen[1];
    
// CHECK IF THE UNSEEN COUNT IS GREATER THAN 0 
    
if ($count 0) {
        
// IF SO, PLAY THE NOTIFY SOUND
        
do_notify();
        
// DISPLAY THE TOTAL NUMBER OF NEW MESSAGES
        
print("$count new message[s]\r\n");
    } else { 
// OTHERWISE, INFORM THE USER THAT NO NEW MESSAGES EXIST
        
print("0 new messages\r\n");
    }    
}

// PLAYS THE "notify" SOUND THAT COMES WITH WINDOWS
function do_notify() {
    
// USES THE sndrec32 COMMAND TO PLAY WAV FILE
    
shell_exec('start /min sndrec32 /play /close "C:\Windows\Media\Notify.wav"');
}

// USES CURL TO SEND A HTTP POST REQUEST
function post_contents($purl$pdat) {
    
// REFERENCE THE CURL OBJECT
    
global $ch;
    
// SPECIFY POST [RATHER THAN THE DEFAULT 'GET']
    
$cc curl_setopt($chCURLOPT_POST1);
    
// SPECIFY THE POST URL
    
$cc curl_setopt($chCURLOPT_URL$purl);
    
// SPECIFY THE POST DATA
    
$cc curl_setopt($chCURLOPT_POSTFIELDS$pdat);
    
// EXECUTE THE REQUEST
    
$rt curl_exec($ch);
    
// DEFAULT BACK TO GET
    
$cc curl_setopt($chCURLOPT_POST0);
    
// RETURN THE RESULT[ING HTML]
    
return($rt);
}

// USES CURL TO SEND A HTTP GET REQUEST
function get_contents($gurl) {
    
// REFERENCE THE CURL OBJECT
    
global $ch;
    
// SPECIFY THE GET URL
    
$cc curl_setopt($chCURLOPT_URL$gurl);
    
// EXECUTE THE REQUEST
    
$rt curl_exec($ch);
    
// RETURN THE RESULT[ING HTML]
    
return($rt);
}

// RETURNS THE CURRENT TIME
function get_time() {
    
// SPECIFY THE FORMAT [hour:secondAM/PM]
    
$format  "g:iA";
    
// FORMAT THE CURRENT TIME
    
$current date($format);
    
// RETURN THE RESULT
    
return($current);
}
?>

Output: