2009
Sending an IM via Gtalk in Clojure
Email has its place, but when it comes to short notifications — of the status of a job, for example — IMs work great.
If you think of your software as your co-worker — someone you work with who helps you do your job — all kinds of functionality just make sense. Would you rather your co-worker emailed you or IMed you? Then the software should do that.
This code snippet sends an IM using Smack 3.1.0, a Jabber client library (Apache 2.0 license).
Download the Smack jar.
Browse Smack source.
Note: you must invite the username to chat and confirm in Gmail before messages will get sent.
user> (def conf (org.jivesoftware.smack.ConnectionConfiguration. "talk.google.com" 5222 "gmail.com"))
#'user/conf
user> (def con (org.jivesoftware.smack.XMPPConnection. conf))
#'user/con
user> (.connect con)
nil
user> (org.jivesoftware.smack.SASLAuthentication/supportSASLMechanism "PLAIN" 0)
nil
user> (.login con "my.username@gmail.com" "mypassword" "resource")
nil
user> (.isAuthenticated con) ;; check that we are authenticated
true
user> (def chat (.createChat (.getChatManager con) "send.to.username@gmail.com" nil)) ;; this nil is the listener -- put something here to receive IMs!
#'user/chat
user> ;; Invite to chat in Gmail -- sending messages won't work w/o doing this!
nil
user> (.sendMessage chat "it works!")
nil
user> (.disconnect con)
nil
Update: I created a project that uses this along with log4j, letting you pull a logger out of nowhere and send out status IMs.