Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Make your own IM bot in Ruby

The following code was copied from the article 'Make your own IM bot in Ruby, and interface it with your Rails app' [rubypond.com].

installation:
   1  
   2  git clone git://github.com/ln/xmpp4r.git xmpp4r
   3  cd xmpp4r
   4  rake gem:install
   5  sudo gem install xmpp4r-simple


connect:
   1  
   2  require 'rubygems'  
   3  require 'xmpp4r-simple'
   4  messenger = Jabber::Simple.new('my-bot@gmail.com', "bot-password")

send:
   1  
   2  messenger.deliver("a-real-person@gmail.com", "Why hello there Mr. Person, your bot is here now!") 

listen:
   1  
   2  while true
   3    messenger.received_messages do |msg|  
   4      puts msg.body  
   5      messenger.deliver("a-real-person@gmail.com", "Got your message, thanks!")  
   6    end  
   7    sleep 2  
   8  end

at your service:
   1  
   2  require 'rubygems'  
   3  require 'xmpp4r-simple'
   4  messenger = Jabber::Simple.new('my-bot@gmail.com', "bot-password")
   5  while true
   6    messenger.received_messages do |msg|
   7      user = User.find_by_im_name(msg.from)
   8      if user
   9        case msg.body
  10        when /^help /i
  11          messenger.deliver(msg.from, "Valid commands are......")  
  12        when /^status /i
  13          user.status = msg.body.sub(/^status /i)
  14          user.save
  15          messenger.deliver(msg.from, "Thanks #{user.first_name}, your status has been updated")  
  16        when /^balance\?/i
  17          messenger.deliver(msg.from, "#{user.first_name}, your current remaining balance is #{user.remaining_balance}")  
  18        else
  19          messenger.deliver(msg.from, "Sorry #{user.first_name}, I didn't understand that. Message me with 'help' for a list of commands")  
  20        end
  21      else
  22        messenger.deliver(msg.from, "Sorry, but we've not got this account registered on our system. Sign-up or update your details at http://www.mysite.com/")  
  23      end    
  24    end  
  25    sleep 2  
  26  end
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS