This Ruby code shows how to send an instant message through
XMPP [wikipedia.org] using
XMPP4R [gna.org]. Source code origin:
Ruby and XMPP/Jabber Part 2: Logging in and sending simple messages [famundo.com].
Preparation
- Installed eJabberd on my Gentoo server
- Created 2 user accounts using Pidgin Internet Messenger on my Ubuntu desktop.
- Tested sending messages using Jabber Instant Messaging (Gabber) and Pidgin Internet Messenger.
Installation
gem install xmpp4r
What we will need
require 'rubygems'
require 'xmpp4r'
include Jabber
Logging in
jid = JID::new('yourname@yourdomain.com/Testing')
password = 'yourpassword'
cl = Client::new(jid)
cl.connect
cl.auth(password)
Sending a simple message
to = "user2@yourdomain.com"
subject = "XMPP4R test"
body = "Hi, this is my first try from XMPP4R!!!"
m = Message::new(to, body).set_type(:normal).set_id('1').set_subject(subject)
cl.send m
References:
-
Building a Twitter Agent with Ruby and Rails [rubyinside.com]
- http://gentoo-wiki.com/Ejabberd
* update 15:45 18-Feb-08 *
Here's a simpler example I found from
Liminal Existence: Announcing Jabber::Simple [romeda.org]
sudo gem install xmpp4r-simple
require 'xmpp4r-simple'
jabber = Jabber::Simple.new('yourname@yourdomain.com', 'yourpassword')
jabber.deliver("user1@yourdomain.com", "Hey! I'm thinking of going Vegetarian - Any suggestions?")
see also:
IM Integration With XMPP4r : Part 2 [rubyfleebie.com]