Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

104 lines (60 sloc) 4.132 kB

Babylon

This is a pretty straightforward port of Skates the ruby xmpp bot framework.

Description

As with most frameworks the goal is seperation of concerns. You can think of Babylon as being a bit like Rails or Django for developing XMPP bots or components. A request comes in either from the XML Stream (XMPP) or as a user event, and it routed to controller action pairs. Controllers and actions generate XML that they publish back onto the XML Stream. Everything else is just icing.

Thanks

A big thanks to the people at Jiva Technology who funded the development of this library. Julien Genestoux from Superfeedr for dealing with all my questions about Skates, and many others.

Building your App

We start with a simple container object

HTH = {};

Routes

One of the first things you'll want to do when designing an XMPP Bot or component is to respond to incoming XMPP stanzas. Routes provide you the means to match against stanzas and direct them to appropriate actions.

To specify your routes first create a new Router object for your App.

HTH.Router = new Babylon.Router();

Then declare the routes using the query and to functions.

MyApp.Router.draw(function(bind){
    bind.query('message[type="chat"]').to(HTH.MessageController, "love");
    bind.query('presence').to(HTH.PresenceController, "presence");
});

Controllers and Actions

Controllers help construct the context by which you render views. Every incoming request instantiates a new Controller and that instance is passed along to the view.

To create a new Controller we'll inject the Babylon controllers prototype into our own object. The only restriction is that your class must take a stanza argument as an initial value.

MyApp.MessageController = function(stanza) { this.stanza = stanza; }
MyApp.MessageController.prototype = new Babylon.Controller();

now we can add new actions to our controller by assigning new functions to the prototype.

MyApp.MessageController.prototype.love = function(){
    this.from = "juliet@capulet.com";
    this.to = "romeo@montague.com";
    this.message = "damn you shakespeare";
};

This will by default call the love view, if no other view is specified.

Views

Views render XMPP stanzas. As such they get put on the xml stream and sent out to the server once rendered.

To create a view, just attach a new function on to the Babylon.Views object.

Babylon.Views.love = function(bind) { $msg({type: "chat", to: bind.to, from: bind.from}).c("body").t(bind.message)};

This will construct an xmpp message as follows

<message type="chat", from="juliet@capulet.come", to="romeo@montague.com">
    <body>damn you shakespeare</body>
</body>

Models

In babylon as with rails or django, your models provide a tool for managing state, persistence, and logic functionality.

User Input

User input is handled similar to how incoming stanzas are. To catch ui events you'll add another entry to your routers draw function.

MyApp.Router.draw(function(bind){
    bind.event('message').to(HTH.Message, "send_message");
});

Then you can use the raise function on the runner, to push events up into the system.

runner.raise("message", {to: "frank@example.org", body: "Hi Frank"});

This will instantiate the HTH.Message controller with the passed in object, and call the send_message action.

Finishing

Lastly you'll need to be able to run your app. This includes passing in a config and starting the Runner.

var config = { "jid": "juliet@capulet"
             , "password": "romeo"
             , "host": "http://capulet.com/http-bind/"
             };
var runner = new Babylon.Runner(MyApp.Router);
runner.run(config);

And your bot should now connect to the running Bosh server and respond to messages.

Requires

Jump to Line
Something went wrong with that request. Please try again.