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-2 of 2 total  RSS 

Post a message to identi.ca using XMPP via HTTP

This Ruby code posts a message to identi.ca using XMPP which uses a simple web server.

   1  
   2  #!/usr/bin/ruby
   3  
   4  require 'rubygems'  
   5  require 'socket'
   6  require 'open-uri'
   7  require 'xmpp4r-simple'
   8  
   9  server = TCPServer.new('127.0.0.1', 9090)
  10  messenger = Jabber::Simple.new('my-bot@gmail.com', "bot-password")
  11  
  12  while (session = server.accept)
  13    request = session.gets
  14    project = request[/(\w+)\?msg=(.*) HTTP\/1.1/,1]
  15    msg = URI.unescape($2)
  16  
  17    puts request
  18    session.print "HTTP/1.1 200/OK\rContent-type: text/html\r\n\r\n"
  19    session.print "<html><head><title>Response from Jeeves</title></head>\r\n"
  20    session.print "<body>"
  21  
  22    if project != '' and msg != '' then
  23      case project
  24        when 'identica'
  25          messenger.deliver("update@identi.ca", msg)
  26      else
  27        puts "Project #{project} wasn't found on this server"
  28      end
  29    end
  30    session.print URI.unescape(request)
  31  
  32    session.print "</body></html>"
  33    session.close
  34  end


To post the message you would type your message within the browser URL e.g. http://127.0.0.1:9090/identica?msg=just%20testing%20xmpp4r-simple%20from%20tcpserver1

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-2 of 2 total  RSS