Calendar events, like those examined in this chapter, are not the only events that may trigger a webbot to run. However, other types of triggers usually require that a scheduled task run periodically to detect if the non-calendar event has occurred. In the previous section, we talked about using email as a means to monitor webbots, but email can also be used to trigger webbots. For example, the script in the following listings uses techniques discussed in Chapter 14 to trigger a webbot to run after receiving an email with the words Run the webbot in the subject line.
First, the webbot initializes itself to read email and establishes the location of the webbot it will run when it receives the triggering email message, as shown in Example 22-3.
Example 22-3. Initializing the webbot that is triggered via email
// Include the POP3 command library include("LIB_pop3.php"); define("SERVER", "your.mailserver.net"); // Your POP3 mail server define("USER", "your@email.com"); // Your POP3 email address define("PASS", "your_password"); // Your POP3 password $webbot_path = "c:\\webbots\\view_competitor.bat";
Once the initialization is complete, this webbot attempts to make a connection to the mail server, as shown in Example 22-4.
Example 22-4. Making a mail server connection
// Connect to POP3 server $connection_array = POP3_connect(SERVER, USER, PASS); $POP3_connection = $connection_array['handle'];
As shown in Example 22-5, once a successful connection to the mail server is made, this webbot looks at each pending message to determine if it contains the trigger phrase Run the webbot. When this phrase is found, the webbot executes in a shell.
Example 22-5. Reading each message and executing a webbot when a specific email is received
if($POP3_connection) { // Create an array of received messages $email_array = POP3_list($POP3_connection); // Examine each message in $email_array for($xx=0; $xx<count($email_array); $xx++) { // Get each email message list($mail_id, $size) = explode(" ", $email_array[$xx]); $message = POP3_retr($POP3_connection, $mail_id); // Run the webbot if email subject contains "Run the webbot" if(stristr($message, "Subject: Run the webbot")) { $output = shell_exec($webbot_path); echo "<pre>$output </pre>"; // Delete message, so we don't trigger another event from this email POP3_delete($POP3_connection, $mail_id); } } }
Once the webbot runs, it deletes the triggering email message so it won’t mistakenly be executed a second time.