Sunday, October 2, 2011

[Programming] XMPP and Office Guru (Part 2)

Since my last post, I had a really hard time making XMPP work using XMPP severs for Linux. Luckily, there was an easier way of doing things without the messy details of installing and hosting your own XMPP server. From this post, How to Write Your Own IM Bot in Less Than 5 Minutes (http://www.labnol.org/internet/tutorial-create-bot-for-gtalk-yahoo-messenger/4354/) I found out that there is a service called IMified which 'hosts' your bots and calls a specific page to do all the processing. As an overview, below is a simple diagram on how IMified works.


So going back to our previous problem, how do we create a location monitoring application which accepts user input thru IM and then displays it to a screen? So with my IMified bot in place, what I did was to code a simple PHP page which accepts POST parameters from IMified and processes it accordingly. Below is the source code for the page which accepts the values sent from IMified

<?PHP
/* 
IMified Message Handler
Allan Paul "Pogz" Sy Ortile
June 2011

IMified sends information through POST requests 
which in turn is then handled by this page.
For the complete list of POST values sent, you
may consult http://www.imified.com/developers/api
for more details. 
*/

$user = $_POST['user'];
$msg = $_POST['msg'];
// Time stamp to log what time the message was recieved
$time = time();

// Connect to database
$con = mysql_connect("host","username","password");
if (!$con)
	{
	die('Could not connect: ' . mysql_error());
	}
else 
	{
	// Select the database to use
	mysql_select_db("db_name", $con);

	// Ass the message to the table in the database	
	mysql_query("UPDATE tbl_user SET time = '$time', location = '$msg' WHERE ymuser = '$user' ");

	// Bot reply to user
	echo "Hi " . $user . "! Your current location has been logged! ";
	mysql_close($con);
	}

?>

If we would trace what is happening between the user, the IMified service and the webserver you may refer to the steps below:

1.) ymuser sends "currently at the office" to the ymchatbot hosted by IMified
2.) IMified processes the message and then passes the POST values to the page located in our webserver
$_POST['user']; now contains the value ymuser
$_POST['msg']; now contains "currently at the office"
3.) The accepting page now processes the POST values sent which is then saved to a database along with a timestamp generated from the PHP code.
4.) The accepting page replies with "Hi ymuser! Your current location has been logged!"

So now, the values are saved into our MySQL database which is then picked up by another page which is just a regular PHP page. The source code could be found below:

<?PHP
/* 
Location Output Page
Allan Paul "Pogz" Sy Ortile
June 2011
*/

// Connect to database
$con = mysql_connect("host","username","password");
if (!$con)
	{
	die('Could not connect: ' . mysql_error());
	}
else 
	{
	// Use database and table
	mysql_select_db("db_name", $con);

	// Do query and pick only the ones in the SMS team and order it by latest timestamp
	$result = mysql_query("SELECT * FROM tbl_user WHERE team = 'SMS' ORDER BY time DESC ");
	
	// Display unique recent post
	echo "<body bgcolor='#FFFFEE'>
	<div align='center'>
	<table border='1'>
	<tr>
	<th align='center'><b><font size='+2' 'verdana'>USERNAME</b></font></th>
	<th width='250' align='center' ><b><font size='+2' 'verdana'>TIME</b></font></th>
	<th width='550'><b><font size='+2' 'verdana'>LOCATION</b></font></th>
	</tr>";
	
	// Some output mumbojumbo
	while($row = mysql_fetch_array($result))
	{
	$human_time = date('d-m-y H:i',$row['time']);
	echo "<tr>";
	echo "<td align='center'><font size='+2' face='verdana'>" . $row['dlsuuser'] . "</font></td>";
	echo "<td align='center'><font size='+1' face='verdana'><b>" . $human_time . "</b></font></td>";
	echo "<td><font size='+2' face='verdana'>" . $row['location'] . "</font></td>";	
	echo "</tr>";
	}
	echo "</table>
	</div></body>";

	mysql_close($con);
	}

?>

The database schema contains the following fields: uid, ymuser, dlsuuser, time, location, team. All of which are text values save for uid and time which are integer values. So, you might be wondering why I opted having a ymuser field and a dlsuuser field. The two fields just does a mapping of the persons YM id with its corresponding work userid. Its just for formalization.

From our infrastructure monitoring screen, I just added an iframe which just calls the page where the results are displayed.

Its pretty much straight forward.

Posting Source Codes

I have a draft post to the part 2 of my previous post about creating a chatbot, it seems like it has been stuck in draft for quite some time since I was looking for a solution on how to post source code here in Blogger. After trying out some stuff and having failed attempts, I went out for a while and shelved the whole thing.

<? PHP
echo ("Hello World!");
?>

Credit goes out to http://eli.thegreenplace.net/2006/03/20/posting-source-code-to-blogger/ which apparently, I've failed to see previously.

After trying it out, I've seen a  more proper and elegant solution. By enabling the "Show HTML literally" and adding a <pre> </pre> tag on the HTML source of the post one could obtain the formatting above. Hopefully I could finalize my previous post about setting up a chatbot.