Infolinks

Sunday, July 15, 2012

Auto Chat Bot with Node.js and XMPP

Want something that respond your chats automatically, without your presence. If you want a system like that here i come for solution for that using Node.js and XMPP Protocol (Used in most chat system like google, facebook). So here we go step by step to implement a system like i have describe above.

First of all you have a system installed node.js, After it for use xmpp with node.js you need a node.js module node-xmpp. To install node-xmpp you just need run a command using npm

npm install node-xmpp

The node-xmpp module is entirely written in Node.js. The GitHub page of the project is located here. Refer other details about node-xmpp from GitHub page. Now we are going to create a chat bot.
First of all you need to include node-xmpp module in your code.

Example of a simple Auto Chat bot system


var xmpp = require('node-xmpp');

//Chat Rules Start from Here
var arr_rules_id = new Array("1","2","3","4");
var arr_rules_text = new Array("Hi","How are you","Where are You","Are you Coming");
var arr_rules_resp_text = new Array("Hi There i am auto chat bot","I am fine","I am online","No never");
//Chat Rules Ends Here

//XMPP Account Details Starts from Here
var client_id = "your_chat_account@server.com"; //Use any XMPP Supported Account like Gmail
var client_pwd = "your_chat_account_password";
var client_host = "XMPP HOST of Your Account";  //talk.google.com for gmail
var client_port = "XMPP port for Host";   //5222 for gmail
var status_msg = "I am Online";   //Set it as you want
//XMPP Account Details End Here

//Now Creating XMPP Client
var cl = new xmpp.Client({ jid: client_id,
password: client_pwd,
host: client_host,
port: client_port
});
//Client Created

//Now Set the Presence of Bot
cl.on('online', function() {
    cl.send(new xmpp.Element('presence', { }).
c('show').t('chat').up().
c('status').t(status_msg)
);
console.log("Connected");
});

//Receiving of Stanza's Starts(Messages are also recieved)
cl.on('rawStanza',
      function(stanza) {
console.log(stanza);
 if (stanza.is('presence') && (stanza.attrs.type == 'subscribe')) {
            cl.send(new xmpp.Element('presence', { to: stanza.attrs.from, type: 'subscribed' }));
        }
if (stanza.is('message') &&
// Important: never reply to errors!
stanza.attrs.type !== 'error') {

// Swap addresses...
var frm = stanza.attrs.from;
var body = stanza.getChild('body');
console.log(stanza);
console.log("\n"+body);
if (!body) {
    return;
  }

var message_recv = body.getText();   //Get Incoming Message
var index = checkarr(arr_rules_text, message_recv);   //Check for incoming message's response text index in array

function checkarr(arr,mes)
{
for(var i=0; i<arr.length; i++) {
        if (arr[i].toLowerCase() == mes.toLowerCase())
        {
        return i;
        }
    }
    return 'notvalid';
}

if(index!='notvalid')     //If message keyword matches then reply back 
{
    var response = arr_rules_resp_text[index];
    cl.send(new xmpp.Element('message', { to: frm, type: 'chat' }).
        c('body').t(response)
    );
}
else
{
    cl.send(new xmpp.Element('message', { to: frm, type: 'chat' }).
        c('body').t("Not Response Found Against Keyword")
    );
}
}
});
cl.on('error', function(err) {
    console.error(err);
});

cl.on('conn-close', function() {
console.log('connection ends');
});

So here we have done all stuff to do chat automatic response bot now run this script and your account goes online. Below i have attach sample screenshot of autochat.


No comments:

Post a Comment