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
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:
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.
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); } ?>
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.
Comments
Post a Comment