Jump to content

PHP Jabber API


Recommended Posts

I know. You all probably look at the term "chat bots" and run and hide. What is more scary than a robot that pretends to be someone else?

 

 

However, by warning people of this danger, we can overcome it:

 

 

 

 

Here's the code for a basic jabber chatbot. You can see it (sometimes) by chatting with fatlotus@gmail.com. Right now, all it does is send things back in a "elmer fudd" language, but this could be far more powerful:

<?php
error_reporting (E_ALL & ~E_NOTICE);
# Jabber Bot Test Script
require ('class.jabber.php');

set_time_limit (0);

define ("INTRO","I am the Elmar-bot. I am not a human. Give me a word or phrase, and I will return what Elmar Fudd would have said. Just begin in with the word elmar and I will do the rest.");

$jabber = new Jabber();
$jabber->server = 'ssl://talk.google.com';
$jabber->host = 'gmail.com';
$jabber->port = 5223;
$jabber->username = 'fatlotus';
$jabber->password = 'PASSWORD REMOVED';
$jabber->resource = 'Evil Bot of Doom v.1.0';
$jabber->use_ssl = true;
$jabber->enable_logging = true;
$jabber->log_filename = 'Log.txt';

$jabber->Connect() or die ('Error Connecting'."\n");
$jabber->SendAuth() or die ('Couldn\'t login.'."\n");
$jabber->SendPresence (NULL,NULL,"In Bot Mode");

$jabber->SendMessage ("doggiearcher@gmail.com","chat",NULL, array("body" => "Hi Kelso!"));

$jabber->CruiseControl();


$jabber->Disconnect();


function Handler_message_normal($message) {
    Handler_message_chat($message);
}
function Handler_message_chat($message) 
{ 
    global $jabber;
    
    
    $from = $jabber->GetInfoFromMessageFrom($message);
    $body = $jabber->GetInfoFromMessageBody($message); 
    if ($jabber->StripJID($from) == "fatlotus@gmail.com") return;

    if ($body == "go2sleep" && preg_match("/^adminaccount@gmail.com/i", $from)) 
    { 
        $jabber->SendMessage ($from, "chat", NULL, array ("body" => 'Good Bye!'));
        $jabber->Disconnect(); 
        exit; 
    } 
    elseif (preg_match("/^elmar /i", $body)) 
    { 
        $phrase = preg_replace("/^elmar /i", "", $body); 
        $translated = str_replace("r", "w", $phrase); 
        $translated = str_replace("R", "W", $translated);
        $translated = str_replace("l","w",$translated);
        $translated = str_replace("L","W",$translated);
        $translated = eregi_replace ("(r)r+","\\1",$translated);
        $translated = eregi_replace ("(w)w+","\\1",$translated);
        
        $jabber->SendMessage($from, "chat", NULL, array("body" => $translated)); 
    } 
    else 
    { 
        $jabber->SendMessage($from, "chat", NULL, array("body" => INTRO)); 
    }
} 

function Handler_presence_available ($message) {
    global $jabber;
    $jid = $jabber->StripJID($jabber->GetInfoFromPresenceFrom($message)); 
    $jabber->SendMessage($jid, "chat", NULL, array("body" => INTRO));
}
?>

 

 

 

If you have any questions, feel free to contact me. Right now, I don't have time to finish this, but I will answer any questions!

Link to comment
Share on other sites

  • 2 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...