phpxmpppidginxmpphp

XMPPHP as live support chat


My idea is to integrate a live support chat on a website. The users text is send with xmpphp to my jabber client with the jabberbot sender id and if I answer, the jabber bot, takes my answer and transfers the text to the user.

There is only one problem. How do I separate different users or different chats? I don't want all users to see the answer, but the user who asks. Is there a kind of unique chat id or another possibility, that I might just missed?

User => Website => Chatbot => me

I want to answer and send it back to the user, but how can I find out the correct user from my answer?


Solution

  • Last time I have to solve this problem I used this architecture:

    enter image description here

    Entlarge image

    The Webserver provides an JavaScript / jQuery or flash chat.

    After chat is started, the client ask the server all 1 Second for new Messages.

    Alternative for 1 Sec Polling

    If that is to slow for you, have a look at websockets.

    http://martinsikora.com/nodejs-and-websocket-simple-chat-tutorial

    http://demo.cheyenne-server.org:8080/chat.html

    But Websockets could no provided by php. There for you need to change php + apchache agaist node.js or java.

    Plain HTTP PHP Methode

    In PHP you will connect to the PsyBnc with is polling the messages from the supporter for you.

    The PsyBnc is an IRC bot.

    The reason why don't directly connect to XMPP or BitlBee is that those protocols don't like the flapping connect, disconnect from PHP. Because you can not keep the session alive, you need something that is made for often and short connects. This is the PsyBnc.

    I would use something like this:

    http://pear.php.net/package/Net_SmartIRC/download

    <?php
    session_start();
    
    $message = $_GET['message'];
    $client_name = $_GET['client_name'];
    
    if (empty($_SESSION['chat_id'])) {
        $_SESSION['chat_id'] = md5(time(). mt_rand(0, 999999));
    }
    
    if (empty($_SESSION['supporter'])) {
        // how do you select the supporter? 
        // only choose a free?
        // We send first message to all supporter and the first who grapped got the chat (where only 3 gues)
    }
    
    $irc_host = "127.0.0.1";
    $irc_port = 6667; // Port of PsyBnc
    $irc_password = "password_from_psy_bnc";
    $irc_user = "username_from_psy_bnc";
    
    include_once('Net/SmartIRC.php');
    
    class message_reader
    {
        private $messages = array();
    
        public function receive_messages(&$irc, &$data)
        {
            // result is send to #smartirc-test (we don't want to spam #test)
            $this->messages[] = array(
                'from' => $data->nick, 
                'message' => $data->message,
            );  
        }
    
        public function get_messages() {
            return $this->messages;
        }
    }
    
    $bot = &new message_reader();
    $irc = &new Net_SmartIRC();
    $irc->setDebug(SMARTIRC_DEBUG_ALL);
    $irc->setUseSockets(TRUE);
    $irc->registerActionhandler(SMARTIRC_TYPE_QUERY|SMARTIRC_TYPE_NOTICE, '^' . $_SESSION['chat_id'], $bot, 'receive_messages');
    $irc->connect($irc_host, $irc_port);
    $irc->login($_SESSION['chat_id'], $client_name, 0, $irc_user, $irc_password);
    $irc->join(array('#bitlbee'));
    $irc->listen();
    $irc->disconnect();
    
    // Send new Message to supporter
    if (!empty($message)) {
        $irc->message(SMARTIRC_TYPE_QUERY, $_SESSION['supporter'], $message);
    }
    
    echo json_encode(array('messages' => $bot->get_messages()));
    

    Connect the support instant messanger to PHP

    We have allready an IRC connection to the PsyBnc, now we need to send messages from IRC to ICQ, XMPP, GOOGLE TALK, MSN, YAHOO, AOI...

    Here for is a nice solution named BitlBee. BitlBee offers an IRC Server with can transfer message from and to nearly all instant messager protocols. By aliasing those accounts. For example you need for your system only 1 Server account at google talk, icq ... and at all your supporter to the buddylist of those accounts. Now BitleBee will provide your boddylist as an irc chat.