-
Notifications
You must be signed in to change notification settings - Fork 2
/
twitter.js
47 lines (40 loc) · 1.68 KB
/
twitter.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
var ntwitter = require('ntwitter'),
utils = require('./utils');
module.exports = function (options) {
var twitterClient = null;;
var initialise = function (callback) {
if (twitterClient) {
callback(null);
} else {
twitterClient = new ntwitter({
consumer_key: process.env.TWITTER_API_KEY,
consumer_secret: process.env.TWITTER_API_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
callback(null);
}
}
var listen = function (callback) {
initialise(function (err) {
utils.log("twitter: initialising listening for messages to @" + process.env.TWITTER_USERNAME + "...");
twitterClient.stream('statuses/filter', { track: [ "@" + process.env.TWITTER_USERNAME ] }, function (stream) {
stream.on('error', function(error, code) {
utils.log("twitter: error listening: " + error + ", " + code);
}),
stream.on('data', function (data) {
callback(null, data ? { from: data.user.screen_name, created_at: new Date(data.created_at), message: data.text } : null);
});
});
});
}
var updateStatus = function (status, callback) {
initialise(function (err) {
twitterClient.updateStatus(status, callback || function (err) { if (err) utils.log("twitter: error posting, " + JSON.stringify(err)); });
});
}
return {
'listen': listen,
'updateStatus': updateStatus,
};
};