bots
Version:
Build robust networks of bots that can react to events
43 lines (32 loc) • 1.16 kB
text/coffeescript
{EventEmitter} = require 'events'
{puts} = require 'util'
xmpp = require 'node-xmpp'
module.exports = class XMPP extends EventEmitter
constructor: (, ) ->
listen: ->
puts "XMPP connecting as #{@jid}"
= new xmpp.Client
jid:
password:
.on 'online',
.on 'stanza',
.on 'error', (err) -> console.warn(err)
close: (done) ->
.end()
done()
updatePresence: =>
puts "XMPP Connected"
.send new xmpp.Element('presence', {}).c('show').t('chat').up()
.c('status').t('Happily echoing your <message /> stanzas')
handle: (stanza) =>
if stanza.is('message') and stanza.attrs.type isnt 'error'
message =
say: (thing, callback) =>
.send (stanza.attrs.from, thing)
callback?()
for child in stanza.children
message.body = child.children.join('\n') if child.name is 'body'
'message', message if message.body
createReply: (to, text) ->
new xmpp.Element('message', to: to, type: 'chat')
.c('body').t(text)