PHP Classes

File: ex_chatter.php

Recommend this page to a friend!
  Classes of sk89q   BlueTOC   ex_chatter.php   Download  
File: ex_chatter.php
Role: Example script
Content type: text/plain
Description: Chatroom bot example
Class: BlueTOC
AIM client using TOC instant messaging protocol
Author: By
Last change: v2.3.000
Date: 17 years ago
Size: 7,299 bytes
 

Contents

Class file image Download
<?php
/*
 * This bot is basically a very boring simple chatroom
 * bot that sits in a chatroom and responds to three
 * commands which are:
 *
 * !story
 * !random
 * !time
 *
 * To get it working:
 * (1) Configure username and password
 * (2) Start up script
 * (3) Talk to bot(s) and say join <chatroom> - example: join bluetoctest
 * (4) Try bot in chatroom
 */
 
require_once "bluetoc/EventHandlers/ObjectBased.php";
require_once
"bluetoc/TocProtocol.php";
require_once
"bluetoc/AimClient.php";

class
ChatterBot extends AimClient
{
    var
$user;
   
   
// We have to keep a track of the chatrooms we are
    // in since AIM requires us to identify chatrooms
    // that we've joined as ID numbers that AIM
    // gives you
   
var $chatrooms = array();
   
    function
ChatterBot($user, $pass)
    {
       
$this->debug_mode = FALSE;
       
       
$this->aim_user = $user;
       
$this->aim_pass = $pass;
       
       
$this->user = $user;
       
       
$this->connect();
    }
   
   
// Handle once we've signed on
   
function event_sign_on($args)
    {
        echo
"{$this->user}: Yay! I've signed in!\n";
       
$this->set_info("<font face=Georgia>I am a chat bot! YAY!<br><br>I am powered by BlueTOC (AIM connection class for PHP)</font>");
    }
   
   
// Handle when we join a chat
   
function event_chat_joined($args)
    {
        echo
"{$this->user}: I have joined chat <b>{$args['name']}</b>\n";
       
       
// Add chatrooms to list of rooms we are in
       
$this->chatrooms[$args['chat_id']] = $args['name'];
    }
   
   
// Handle when we get a chat message
   
function event_chat_message($args)
    {
       
// Remember that AIM chat messages usually have HTML
        // so we must strip it so that we can
        // easily parse it
       
$message = strip_tags($args['message']);
       
       
// Just echo all the chat messages that we
        // receive while in the chatroom
       
echo "{$this->user}: [{$this->chatrooms[$args['chat_id']]}] <{$args['user']}> {$message}\n";
       
       
// See if we can match !command
       
if(preg_match('`^!([A-Za-z0-9]+)`', $message, $msg))
        {
            switch(
$msg[1])
            {
                case
'story':
                   
$this->chat_send($args['chat_id'], "<font face=Georgia>There was once a dog. It ate people.</font>");
                    break;
                case
'random':
                   
$this->chat_send($args['chat_id'], "<font face=Georgia>" . rand(0, 1000000) . "</font>");
                    break;
                case
'time':
                   
$this->chat_send($args['chat_id'], "<font face=Georgia>" . date('r') . "</font>");
                    break;
            }
        }
       
       
// Commands available:
        // !story
        // !random
        // !time
   
}
   
   
// Handle when we get an instant message
   
function event_im($args)
    {
        echo
"{$this->user}: {$args['user']} IMed me!\n";
       
       
// Remember that AIM IMs usually have HTML
        // so we must strip it so that we can
        // easily parse it
       
$message = strip_tags($args['message']);
       
       
// Let's check if they're telling us to
        // join a certain chatroom
        // The following responds to anyone! If you were to
        // restrict to only some screen names, you would do
        // a check on $args['user']
       
if(preg_match("`^join ([A-Za-z ]+)$`", $message, $m))
        {
           
// We can only be in three chatrooms at once
           
if(count($this->chatrooms) >= 3)
            {
               
$this->send_im($args['user'], "<font face=Georgia>I am already in three chatrooms</font>", FALSE);
            }
            else
            {
               
$this->chat_join($m[1]);
               
$this->send_im($args['user'], "<font face=Georgia>Trying to join <b>{$m[1]}</b></font>", FALSE);
            }
        }
    }
   
    function
event_error($args)
    {
       
// These are a list of errors in English
        // Most, if not all, errors will return an error number
        // and not the error description
       
$connection_errors = array(
           
100 => 'Data unable to be sent',
           
200 => 'Flapon',
           
201 => 'Data not received from server after FLAPON packet',
           
202 => 'Invalid FLAP SIGNON response from the server',
           
203 => 'Invalid response from the server');
       
       
$aim_errors = array (
           
0 => 'Success',
           
1 => 'AOLIM Error: Unknown Error',
           
2 => 'AOLIM Error: Incorrect Arguments',
           
3 => 'AOLIM Error: Exceeded Max Packet Length (1024)',
           
4 => 'AOLIM Error: Reading from server',
           
5 => 'AOLIM Error: Sending to server',
           
6 => 'AOLIM Error: Login timeout',
           
901 => 'General Error: %s not currently available',
           
902 => 'General Error: Warning of %s not currently available',
           
903 => 'General Error: A message has been dropped, you are exceeding the server speed limit',
           
950 => 'Chat Error: Chat in %s is unavailable',
           
960 => 'IM and Info Error: You are sending messages too fast to %s',
           
961 => 'IM and Info Error: You missed an IM from %s because it was too big',
           
962 => 'IM and Info Error: You missed an IM from %s because it was sent too fast',
           
970 => 'Dir Error: Failure',
           
971 => 'Dir Error: Too many matches',
           
972 => 'Dir Error: Need more qualifiers',
           
973 => 'Dir Error: Dir service temporarily unavailble',
           
974 => 'Dir Error: Email lookup restricted',
           
975 => 'Dir Error: Keyword ignored',
           
976 => 'Dir Error: No keywords',
           
977 => 'Dir Error: Language not supported',
           
978 => 'Dir Error: Country not supported',
           
979 => 'Dir Error: Failure unknown %s',
           
980 => 'Auth Error: Incorrect nickname or password',
           
981 => 'Auth Error: The service is temporarily unavailable',
           
982 => 'Auth Error: Your warning level is too high to sign on',
           
983 => 'Auth Error: You have been connecting and disconnecting too frequently. Wait 10 minutes and try again. If you continue to try, you will need to wait even longer.',
           
989 => 'Auth Error: An unknown signon error has occurred %s');
           
       
// Let's see what kind of error we are faced with
       
switch($args['type'])
        {
           
// Connection error
           
case ERROR_CONNECTION:
                echo
"* Connection error: {$connection_errors[$args['number']]} ({$args['number']})\n";
                break;
           
// AIM is giving us an error
           
case ERROR_AIM:
                echo
"* AIM error: {$aim_errors[$args['number']]} ({$args['number']})\n";
                break;
        }
    }
}

// Create a new instance of the bot
$client = new ChatterBot('username1', 'password1');

// Listen to the bot infinitely
while(true)
{
   
$client->listen();
   
usleep(150);
}
?>