Ruby has very powerful tools to create random jabber bots with XMPP protocol. In this article i`ll show just a small sample with a few commands available.
I got xmpp4r-simple library from Google Code page – http://code.google.com/p/xmpp4r-simple/. Its kinda old, not updated since 2006, but the sources are really easy to read and understand, and probably modify.
First, we need to install dependencies (gems):
$ gem install xmpp4r
$ gem install xmpp4r-simpleAnd now, the actual ruby code:
#!/usr/bin/ruby require 'rubygems' require 'xmpp4r' require 'xmpp4r-simple' $jabber_login = "YOUR_JABBER_ID_HERE" # test@jabber.org $jabber_password = "YOUR_PASSWORD_HERE" $client = nil # create jabber connection def jabber_connect() begin conn = Jabber::Simple.new($jabber_login,$jabber_password) return conn rescue return nil end end # send message to jabber client def jabber_respond(to, msg) $client.deliver(to,msg,:chat) end # get sender jabber id def jabber_get_jid(str) matches = str.match(/([a-z\d_.\-]{1,32})@([a-z\d.-]{1,32})\//i) return "#{matches[1]}@#{matches[2]}" end # ------------------------------------------------------------------- # # send server time def app_time(jid) jabber_respond(jid, "Server time is: #{Time.now}") end # send some 'help' :) def app_help(jid) jabber_respond(jid, "Nobody can help you. You`re alone.") end # process received message def app_parse_msg(jid, msg) cmd = msg.body.strip begin case cmd when /^help$/i then app_help(jid) when /^time$/i then app_time(jid) when /^jid$/i then jabber_respond(jid, "Your jabber id: #{jid}") else jabber_respond(jid, "Unknown command. Try something different.") end rescue Exception => ex jabber_respond(jid, "SYSTEM_ERROR: #{ex}") end end # ------------------------------------------------------------------- # puts "Connecting to jabber server..." $client = jabber_connect() if $client puts "Connected. Waiting for messages..." loop do $client.received_messages do |message| jid = jabber_get_jid(message.from.to_s) puts "Received message from #{jid}: #{message.body}" app_parse_msg(jid, message) end sleep 0.1 end else puts "Cannot connect. Please try again later." end
I think, the comments in source code are enough to understand what this bot supposed to do. There are 3 commands available: ‘help’, ‘time’, ‘jid’
Download script here – http://files.sosedoff.com/204ab61c/

