-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
147 lines (139 loc) Β· 6.63 KB
/
index.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
Bunch of var
*/
var Twitter = require('twitter'),
fs = require("fs"),
secrets = JSON.parse(fs.readFileSync('data/secrets.json', 'utf8')),
funicularData = JSON.parse(fs.readFileSync('data/funicular_data.json', 'utf8')),
cron = require('node-cron'),
TelegramBot = require('node-telegram-bot-api'), // https://github.com/yagop/node-telegram-bot-api
token = secrets['telegram_token'],
bot = new TelegramBot(token, {polling: true}),
client = new Twitter({
consumer_key: secrets['twitter_consumer_key'],
consumer_secret: secrets['twitter_consumer_secret'],
access_token_key: secrets['twitter_access_token_key'],
access_token_secret: secrets['twitter_access_token_secret']
});
/*
Save JSON to funicular_data.json
TODO: https://www.npmjs.com/package/jsonfile
*/
function writeToData(myData) {
var outputFilename = 'data/funicular_data.json';
fs.writeFile(outputFilename, JSON.stringify(myData, null, 2), function (err) {
if (err) {
console.log(err);
} else {
console.log("JSON saved to " + outputFilename);
console.dir(myData);
}
});
}
/*
Create the data file - we just need the latest tweet id when launching the script
*/
funicularData.twitterUsersToListenTo.forEach(function (screen_name) {
client.get('statuses/user_timeline', {screen_name: screen_name, count: 2}, function (error, tweets, response) {
if (!error) {
debug = false;
if (debug) { // Take previous last tweet to fake a new tweet (test purpose)
console.dir(tweets[1].created_at + " " + tweets[1].id);
funicularData.twitterUsersData[screen_name] = {};
funicularData.twitterUsersData[screen_name].lastTweetID = tweets[1].id;
funicularData.twitterUsersData[screen_name].lastTweetIDstr = tweets[1].id_str;
funicularData.twitterUsersData[screen_name].lastTweetCreateAt = tweets[1].created_at;
} else {
funicularData.twitterUsersData[screen_name] = {};
funicularData.twitterUsersData[screen_name].lastTweetID = tweets[0].id;
funicularData.twitterUsersData[screen_name].lastTweetIDstr = tweets[0].id_str;
funicularData.twitterUsersData[screen_name].lastTweetCreateAt = tweets[0].created_at;
}
writeToData(funicularData);
}
});
});
/*
Telegram start function
*/
bot.onText(/start/, function (msg) {
var txt = "Fluffy Funicular will push you new tweets from some pre-selected twitter account. Use /subscribe to activate the bot and /quit to unsubscribe. Have fun π";
bot.sendMessage(msg.from.id, txt);
});
/*
Telegram subscribe function
*/
bot.onText(/[Ss]ubscribe/, function (msg, match) {
var twitterUsersLinks = funicularData.twitterUsersToListenTo.map(function (usr) {
return "https://twitter.com/" + usr.substring(1);
});
// TODO: check if user present and avoid to redo something already done
console.log(msg.from);
funicularData.telegramUsersToUpdate[msg.from.id] = msg.from;
writeToData(funicularData);
console.log(" - log: #" + msg.from.id + " (" + msg.from.first_name + " " + msg.from.last_name + " aka " + msg.from.username + ") subscribed");
// send msg to myself to keep trace of subscription
newSubscriber = "π @fluffy_funicular_bot has a new subscriber\n";
newSubscriber += "#newsubscriber: id=" + msg.from.id + "(" + msg.from.first_name + " " + msg.from.last_name + " aka " + msg.from.username + ")";
bot.sendMessage(9917772, newSubscriber);
bot.sendMessage(msg.from.id, "Thanks, you've subscribed to " + twitterUsersLinks.join(", "));
});
/*
Telegram unsubscribe (quit) function
*/
bot.onText(/[Qq]uit/, function (msg, match) {
delete funicularData.telegramUsersToUpdate[msg.from.id];
writeToData(funicularData);
console.log(" - log: #" + msg.from.id + " (" + msg.from.first_name + " " + msg.from.last_name + " aka " + msg.from.username + ") unsubscribed");
// send msg to myself to keep trace of subscription
unsubscriber = "π Someone leave @fluffy_funicular_bot\n";
unsubscriber += "#unsubscriber: id=" + msg.from.id + "(" + msg.from.first_name + " " + msg.from.last_name + " aka " + msg.from.username + ")";
bot.sendMessage(9917772, unsubscriber);
bot.sendMessage(msg.from.id, "Bye");
});
function sendNewTweetToTelegram(data) {
// TODO: find a better way to reload the date (fs-watcher?)
reloadedFunicularData = JSON.parse(fs.readFileSync('data/funicular_data.json', 'utf8'));
console.log(data);
tweet = "π This is @" + data.user.screen_name + "'s new tweet:";
tweet += "\n\n";
tweet += data.text;
tweet += "\n\n";
tweet += "\tβ read tweet online: https://twitter.com/" + data.user.screen_name + "/status/" + data.id_str;
for (var i in reloadedFunicularData.telegramUsersToUpdate) {
bot.sendMessage(i, tweet);
}
}
/*
X minutes the cron.schedule will fetch new statuses and push the to telegram
*/
cron.schedule('* * * * *', function () {
console.log(" - log: fetching new tweets");
funicularData.twitterUsersToListenTo.forEach(function (screen_name) {
//console.log(" - log: " + funicularData.twitterUsersData[screen_name].lastTweetID + " " + funicularData.twitterUsersData[screen_name].lastTweetIDstr);
console.log(" β for " + screen_name);
// https://dev.twitter.com/rest/reference/get/statuses/user_timeline
client.get('statuses/user_timeline', {
screen_name: screen_name,
since_id: funicularData.twitterUsersData[screen_name].lastTweetIDstr
}, function (error, tweets, response) {
if (!error) {
if (tweets.length) {
// great! fishing has been good βΊ
// TODO (!!!) Update last tweet information in funicularData
tweets.map(function (tweet) {
//console.dir(tweet.text);
funicularData.twitterUsersData[screen_name] = {};
funicularData.twitterUsersData[screen_name].lastTweetID = tweet.id;
funicularData.twitterUsersData[screen_name].lastTweetIDstr = tweet.id_str;
funicularData.twitterUsersData[screen_name].lastTweetCreateAt = tweet.created_at;
//writeToData(funicularData); // ?? Is cache enough for that ?
sendNewTweetToTelegram(tweet);
});
} else {
// console.log(" β no new status found for " + screen_name);
}
}
});
});
});