-
Notifications
You must be signed in to change notification settings - Fork 0
/
jabberbot.js
127 lines (105 loc) · 3.35 KB
/
jabberbot.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env node
var xmpp = require('node-xmpp')
var config = require('./config');
var weather = require('./weather');
var lookup = require('./lookup');
var chuck = require('./chuck');
var yomomma = require('./yomomma');
var bikes = require('./dublinbikes');
if(config.username === null || config.password === null) {
console.err("Missing username or password in config.json");
process.exit(1)
}
var client = new xmpp.Client({
jid: config.username,
password: config.password
});
client.on('online', function() {
console.log('[ Jabber bot Online ]')
client.send(new xmpp.Element('presence', { })
.c('show').t('chat').up()
.c('status').t('HAL - Heuristically programmed ALgorithmic computer')
);
// join room (and request no chat history)
client.send(new xmpp.Element('presence', { to: config.room+'/'+config.nick }).
c('x', { xmlns: 'http://jabber.org/protocol/muc' }).
c('history', { maxstanzas: 0, seconds: 1})
);
})
client.on('stanza', function(stanza) {
// always log error stanzas
if (stanza.attrs.type == 'error') {
console.log('[error] ' + stanza);
return;
}
// ignore everything that isn't a room message
if (!stanza.is('message') || !stanza.attrs.type == 'groupchat') {
return;
}
// ignore messages we sent
if (stanza.attrs.from == config.room+'/'+config.nick) {
return;
}
var body = stanza.getChild('body');
// message without body is probably a topic change
if (!body) {
return;
}
var fromNick = "";
if( stanza.attrs && stanza.attrs.from ) {
fromNick = stanza.attrs.from.split('/')[1];
}
var message = body.getText();
if (message.indexOf('!weather') === 0) {
var place = message.replace('!weather','');
if(place.length > 1) {
weather(place, fromNick, function(forecast){
return sendMessage(forecast);
});
}
else {
return sendMessage("Usage: !weather placename");
}
}
if(message.indexOf('!daisy') === 0) {
var daisy = "Daisy, Daisy, give me your answer do. I'm half crazy all for the love of you. It won't be a stylish marriage, I can't afford a carriage. But you'll look sweet upon the seat of a bicycle built for two.";
return sendMessage(daisy);
}
if(message.indexOf('!chuck') === 0) {
chuck(fromNick, function(joke){
return sendMessage(joke);
});
}
if(message.indexOf('!bikes') === 0) {
bikes(fromNick, function(res){
return sendMessage(res);
});
}
if(message.indexOf('!yomomma') === 0) {
yomomma(fromNick, function(joke){
return sendMessage(joke);
});
}
if (message.indexOf('!lookup') === 0) {
var query = message.replace('!lookup','');
if(query.length > 1) {
lookup(query, fromNick, function(answer){
return sendMessage(answer);
});
}
else {
return sendMessage("Usage: !lookup question");
}
}
});
function sendMessage(message) {
var to = config.room;
var ourmessage = new xmpp.Element(
'message',
{ to: to, type: 'groupchat' }
).c('body').t(message);
client.send(ourmessage);
}
client.on('error', function(e) {
console.error(e)
});