Skip to content

Instantly share code, notes, and snippets.

@bryanhelmig
Created February 2, 2012 23:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save bryanhelmig/1726605 to your computer and use it in GitHub Desktop.
Save bryanhelmig/1726605 to your computer and use it in GitHub Desktop.
Simple Node.js Jabber/AIM REST API.
util = require "util"
xmpp = require "node-xmpp"
oscar = require 'oscar'
express = require "express"
###
The XMPP bot.
###
jabber = new xmpp.Client
jid: "name@gmail.com"
password: "password"
host: "talk.google.com"
port: 5222
jabber.on "online", ->
util.puts "jabber online"
jabber.on "error", (e) ->
util.puts e
###
The AIM bot.
###
aim = new oscar.OscarConnection
connection:
username: 'username',
password: 'password'
aim.connect (err) ->
if not err
util.puts "aim online"
else
util.puts err
aim.on "error", (e) ->
util.puts e
###
A simple webserver.
###
app = express.createServer()
app.use express.bodyParser()
app.put '/message', (req, res) ->
###
Simple, hit the /message URL with a PUT and the params:
key (a unique token you set)
to (the username or email to send to)
message (the message)
network (either aim or jabber)
###
key = req.param 'key', null
if key != 'a_big_long_key'
res.json
message: 'Needs proper `key`.'
401
return
# get parameters
network = req.param 'network', null
to = req.param 'to', null
message = req.param 'message', null
# send the message
if network in ['aim', 'jabber'] and to and message
switch network
when 'aim'
aim.sendIM to, message
when 'jabber'
jabber.send(new xmpp.Element('message',
to: to,
type: 'chat'
).
c('body').
t(message))
res.json
message: 'Message sent.'
200
return
res.json
message: 'Needs `network` (aim|jabber), `to` and `message` params.'
400
app.listen 3000
express = require "express"
app = express.createServer()
app.put '/message', (req, res) ->
console.log 'hello!'
app.listen 3000
express = require "express"
app = express.createServer()
app.use express.bodyParser()
app.put '/message', (req, res) ->
console.log req.param 'network', null
console.log req.param 'to', null
console.log req.param 'message', null
app.listen 3000
xmpp = require "node-xmpp"
jabber = new xmpp.Client
jid: "name@gmail.com"
password: "password"
host: "talk.google.com"
port: 5222
cl.on "stanza", (stanza) ->
if stanza.is("message") and stanza.attrs.type isnt "error"
stanza.attrs.to = stanza.attrs.from
delete stanza.attrs.from
cl.send stanza
jabber.on "online", ->
console.log "jabber online"
aim = new oscar.OscarConnection
connection:
username: 'username',
password: 'password'
aim.im (text, sender, flags, when) ->
@sendIM sender, text
aim.connect (err) ->
if not err
util.puts "aim online"
else
util.puts err
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment