Giter VIP home page Giter VIP logo

wobot's Introduction

wobot

A plugin-based HipChat bot written in Node.js.

Installation

The easiest way to obtain Wobot is through npm:

npm install wobot

Keep in mind this module depends on node-xmpp which cannot be built without:

  • libexpat1-dev: apt-get install libexpat1-dev
  • libicu-dev: apt-get install libicu-dev

Your First Bot

Instantiate the wobot.Bot class by passing it a hash containing:

  • jid: Jabber ID followed by /bot
  • password: The account's password
  • Optional caps_ver: Name and version of the bot. Defaults to Wobot:x.x.
  • Optional debug: When set to true, XMPP traffic will be printed.
  • Optional host: The hostname of the server. Defaults to jid host.
var wobot = require('wobot');

var bot = new wobot.Bot({
  jid: '[email protected]/bot',
  password: '??????'
});

bot.connect();

Events

The following events can be binded to:

onConnect(callback)

Emitted whenever the bot connects to the server.

onMessage(condition, callback)

Emitted whenever a message is sent to a channel the bot is in.

  • condition is either a RegExp or a string which must match the message for the callback to be triggered.
  • callback in the form of function(channel, from, message[, matches]).

condition can also be omitted i.e. onMessage(callback).

onPrivateMessage(condition, callback)

Emitted whenever a message is sent privately to the bot.

  • condition is either a RegExp or a string which must match the message for the callback to be triggered.
  • callback in the form of function(from, message[, matches]).

condition can also be omitted i.e. onPrivateMessage(callback).

onInvite(callback)

Emitted whenever invited to a room.

  • callback in the form of function(roomJid, fromJid, reason).

onPing(callback)

Emitted everytime the bot pings the server (roughly every 30 seconds.)

onError(callback)

Emitted whenever an error occurs. disconnect will be emitted afterwards.

  • callback in the form of function(condition, text, stanza)
    • condition is a string containing the XMPP stream error condition.
    • text is a string containing a human-readable error message.
    • stanza is an instance of xmpp.Element, when available.

onDisconnect(callback)

Emitted whenever the bot disconnects from the server.

Public API

Instances of wobot.Bot have the following methods:

join(roomJid, historyStanzas)

Join a channel.

  • roomJid is in the following format: [email protected].
  • historyStanzas: Max number of history entries to request (default=0).

part(roomJid)

Part a channel.

message(targetJid, message)

Send a message to either a channel or a user.

getRoster(callback)

Fetches the roster (buddy list).

  • callback in the form of function(err, roster, stanza)
    • err is a string representation of the error, if any.
    • roster is an array of objects containing user data.
    • stanza is the full response stanza, an xmpp.Element.

Example return value for roster:

[
  { name: 'Christian Joudrey', jid: '[email protected]', mention_name: 'ChristianJoudrey' },
  { name: 'The Bot', jid: '[email protected]', mention_name: 'TheBot' }
]

getRooms(callback)

Fetches the rooms available to the bot user. This is equivalent to what would show up in the HipChat lobby.

  • callback: Function to be triggered: function (err, items, stanza)
    • err: Error condition (string) if any
    • rooms: Array of objects containing room data
    • stanza: Full response stanza, an xmpp.Element

connect()

Connect to the server.

disconnect()

Disconnect from the server.

loadPlugin(identifier, plugin, options)

Load a plugin.

  • identifier: A unique string that identifies the plugin. This will be used to unload it.
  • plugin: Object with a load function as so: function load (bot).
  • options: Will be passed as the second argument to load.

sendIq(stanza, callback)

Sends an IQ stanza and stores a callback to be called when its response is received.

  • stanza is the xmpp.Element to send.
  • callback in the form of function (err, stanza).
    • err is a string representation of the error, if any.
    • stanza is the full response stanza, an xmpp.Element.

Legal stuff

Copyright (c) 2011 Christian Joudrey. See LICENSE for details.

Node.js is an official trademark of Joyent. This module is not formally related to or endorsed by the official Joyent Node.js open source or commercial project.

wobot's People

Contributors

cjoudrey avatar codyro avatar powdahound avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

wobot's Issues

'from' should be a user object from roster

the onMessage callback gets passed a channel that can be used to reply to the message, but does not provide a way to uniquely identify a user, or get their info, like their mention_name and jid. (If you're wondering how I might need these, there are a number of cases, including needing to send the jid to another app, "mentioning" a user in a response to get their attention, and replying privately to a user to send a command in a chat room.)

I suggest that the callback receive the same user object that exists in the roster array.

Add a better API for message events

The current way of setting message callbacks isn't very friendly since you always need to validate the message before continuing:

bot.on('message', function(channel, from, message) {
  if (message !== '!chuck') {
    return false;
  }
  // !chuck stuff..
}));

In addition, EventEmitters don't give us the flexibility of adding RegExp and complex conditions to our binding.

New API proposal

bot.onConnect

Triggered when the bot successfully connects.

bot.onConnect(function() {

});

bot.onMessage

Triggered by channel messages.

bot.onMessage('!chuck', function(channel, from, message) {

}));

The first argument can be either a String or a RegExp.

bot.onMessage(/^\!weather ([0-9]+)$/, function(channel, from, message, matches) {

}));

The result of the .match are returned as the 4th argument.

bot.onPrivateMessage

Triggered by private messages.

bot.onPrivateMessage('!chuck', function(from, message) {

}));

The first argument can be either a String or a RegExp.

bot.onPrivateMessage(/^\!weather ([0-9]+)$/, function(from, message, matches) {

}));

The result of the .match are returned as the 4th argument.

bot.onPing

Triggered whenever the bot pings HipChat (roughly every 30 seconds).

bot.onPing(function() {

});

bot.onError

Triggered whenever a XMPP error occurs.
This will automatically trigger onDisconnect.

bot.onError(function(errMessage, stanza) {

});

bot.onDisconnect

Triggered whenever the bot disconnects.

bot.onDisconnect(function() {

});

bot.message(targetJid, message)

The bot.pm will be removed and combined into bot.message.

The function will auto-detect the target-type and take the appropriate action of sending the message to the channel or via private message to the user.

bot.reply(message)

Within a callback you can use this.reply which will send the message to either the room or the person depending on the type of event.

For instance, if the callback is triggered by onMessage, this.reply('test') will send the message to the room.

If the callback is triggered by onPrivate, this.reply('test') will send the message to the person.

Node 0.8

Can wobot be updated to allow installation with Node >= v0.8.0? The series is pretty far along now.

Stanza doesn't have a root. How can we keep the connection alive?

./node_modules/node-xmpp/lib/xmpp/session.js:129
    this.connection.send(stanza.root());
                                ^
TypeError: Object   has no method 'root'
    at Client.Session.send (./node_modules/node-xmpp/lib/xmpp/session.js:129:30)
    at Timer.<anonymous> (./node_modules/wobot/lib/bot.js:66:19)
    at Timer.exports.setInterval.timer.ontimeout (timers.js:234:14)

Removed from npm?

I just tried installing wobot on to my Raspberry Pi today to play around with a new bot I've been tinkering with and I can't seem to install wobot at all. I run: npm install wobot and it says Error: failed to fetch from registry: wobot.

Here is the whole command:

pi@raspberrypi ~/Code/HipChatBot $ npm install wobot
npm http GET https://registry.npmjs.org/wobot

npm ERR! Error: failed to fetch from registry: wobot
npm ERR!     at /usr/share/npm/lib/utils/npm-registry-client/get.js:139:12
npm ERR!     at cb (/usr/share/npm/lib/utils/npm-registry-client/request.js:31:9)
npm ERR!     at Request._callback (/usr/share/npm/lib/utils/npm-registry-client/request.js:136:18)
npm ERR!     at Request.callback (/usr/lib/nodejs/request/main.js:119:22)
npm ERR!     at Request.<anonymous> (/usr/lib/nodejs/request/main.js:212:58)
npm ERR!     at Request.emit (events.js:88:20)
npm ERR!     at ClientRequest.<anonymous> (/usr/lib/nodejs/request/main.js:412:12)
npm ERR!     at ClientRequest.g (events.js:156:14)
npm ERR!     at ClientRequest.emit (events.js:67:17)
npm ERR!     at HTTPParser.parserOnIncomingClient [as onIncoming] (http.js:1256:7)
npm ERR! You may report this log at:
npm ERR!     <http://bugs.debian.org/npm>
npm ERR! or use
npm ERR!     reportbug --attach /home/pi/Code/HipChatBot/npm-debug.log npm
npm ERR!
npm ERR! System Linux 3.18.11+
npm ERR! command "/usr/bin/nodejs" "/usr/bin/npm" "install" "wobot"
npm ERR! cwd /home/pi/Code/HipChatBot
npm ERR! node -v v0.6.19
npm ERR! npm -v 1.1.4
npm ERR! message failed to fetch from registry: wobot
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     /home/pi/Code/HipChatBot/npm-debug.log
npm not ok

Anyone else seeing this or am I just crazy?

Compatibility with Node v0.5.3

Hey,

I'm attempting to use an https.method call in my code (to get Airbrake exception information), which doesn't work with v0.4.6 of node, and requires v0.5.3

However, wobot v0.3.0 explicitly states it needs < 0.5.0, is there a gigantic dragon I will need to slay in order to get the thing to work?

Thanks,
Luke

Add `bot.reply`

Within a callback you can use this.reply which will send the message to either the room or the person depending on the type of event.

For instance, if the callback is triggered by onMessage, this.reply('test') will send the message to the room.

If the callback is triggered by onPrivateMessage, this.reply('test') will send the message to the person.

Chatbot process dies frequently to error from node-xmpp

Looks to be a result of the node-xmpp module:

/Users/dbrito/skynetwork/chat-bot/node_modules/wobot/node_modules/node-xmpp/lib/xmpp/session.js:127
  this.connection.send(stanza.root());
                              ^
TypeError: Object   has no method 'root'
    at Client.Session.send (/Users/dbrito/skynetwork/chat-bot/node_modules/wobot/node_modules/node-xmpp/lib/xmpp/session.js:127:30)
    at null.<anonymous> (/Users/dbrito/skynetwork/chat-bot/node_modules/wobot/lib/bot.js:66:19)
    at wrapper [as _onTimeout] (timers.js:252:14)
    at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)

Cannot receive messages

Here is my code:

bot.onConnect(() => {
    console.log('Connected');
    bot.join('[email protected]');

    repl.start('slackbot> ').context.b = new BotApi(bot);
});

bot.onMessage(/test/, (channel, from, message, matches) => {
    console.log("message:", from, matches);
});

bot.onPrivateMessage(/hello/, (from, message, matches) => {
    console.log('pm:', message);
});

A private message fires the onPrivateMessage callback, but a normal message does not. I am able to connect and authenticate just fine. what am i doing wrong?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.