Created
February 1, 2012 16:13
-
-
Save bryanhelmig/1717773 to your computer and use it in GitHub Desktop.
Simple Node.js Jabber/AIM message sending API.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", -> | |
jabber.send new xmpp.Element("presence", {}). | |
c("show"). | |
t("chat"). | |
up(). | |
c("status"). | |
t("We are here to serve you.") | |
util.puts "jabber online" | |
jabber.on "error", (e) -> | |
util.puts e | |
### | |
The AIM bot. | |
### | |
aim = new oscar.OscarConnection | |
connection: | |
username: 'username', | |
password: 'password' | |
aim.on "error", (e) -> | |
util.puts e | |
aim.connect (err) -> | |
if not err | |
util.puts "aim online" | |
else | |
util.puts err | |
### | |
A simple webserver. | |
### | |
app = express.createServer() | |
app.use express.bodyParser() | |
app.post '/', (req, res) -> | |
### | |
Simple, hit the root URL with a POST 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment