» tagged pages
» logout
Ruby
Return to Code Snippets: ruby

Post a message to identi.ca using XMPP via HTTP

Tags Applied to this Entry

1 person has tagged this page:
This Ruby code posts a message to identi.ca using XMPP which uses a simple web server.


#!/usr/bin/ruby

require 'rubygems'
require 'socket'
require 'open-uri'
require 'xmpp4r-simple'

server = TCPServer.new('127.0.0.1', 9090)
messenger = Jabber::Simple.new('my-bot@gmail.com', "bot-password")

while (session = server.accept)
request = session.gets
project = request[/(\w+)\?msg=(.*) HTTP\/1.1/,1]
msg = URI.unescape($2)

puts request
session.print "HTTP/1.1 200/OK\rContent-type: text/html\r\n\r\n"
session.print "Response from Jeeves\r\n"
session.print ""

if project != '' and msg != '' then
case project
when 'identica'
messenger.deliver("update@identi.ca", msg)
else
puts "Project #{project} wasn't found on this server"
end
end
session.print URI.unescape(request)

session.print ""
session.close
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
Username:
Password:
(or Cancel)