-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
230 lines (190 loc) · 5.8 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
var Bot = require('node-telegram-bot');
config = require('./config/config.js'),
CronJob = require('cron').CronJob,
mongoose = require('./config/mongoose'),
Twitter = require('twitter');
db = mongoose();
console.log('Starting app');
var twitter = new Twitter(config.twitter);
var SteamChat = db.model('SteamChat');
var getLatestSales = function(sinceId, maxId, latestTweetId, sales, onFinished){
var initialRun = false;
var twitterParams = { "count": config.tweetCount, "screen_name": 'steam_games', "exclude_replies": true, "trim_user": true, "include_rts": false};
if(sinceId !== null)
{
twitterParams.since_id = sinceId;
} else
{
initialRun = true;
twitterParams.count = 5;
}
if(maxId !== null)
{
twitterParams.max_id = maxId;
}
twitter.get('statuses/user_timeline', twitterParams, function(error, tweets, response){
if (!error)
{
var minId = Infinity;
saleTweets = tweets.filter(function(tweet){
if(tweet.id < minId)
{
minId = tweet.id;
}
if(tweet.id > latestTweetId)
{
latestTweetId = tweet.id;
}
if(tweet.text.indexOf('sale') !== -1 ||
tweet.text.indexOf('Sale') !== -1 ||
tweet.text.indexOf('SALE') !== -1 ||
tweet.text.indexOf('% off') !== -1 ||
tweet.text.indexOf('% Off') !== -1 ||
tweet.text.indexOf('% OFF') !== -1)
{
return true;
} else
{
return false;
}
});
if(saleTweets.length === 0 || minId === maxId)
{
onFinished(sales);
} else
{
//add tweets and get the next batch
saleTweets.forEach(function(tweet,ind,arr){
sales.tweets.push(tweet);
});
if(initialRun)
{
onFinished(sales);
} else
{
getLatestSales(sinceId,minId-1,latestTweetId,sales,onFinished);
}
}
} else {
console.log(error);
//TODO: retry a number of times before giving up
}
});
};
var checkSteamJob = new CronJob('00 00 */2 * * *', function(){
console.log('entering job');
SteamChat.find({}, function(err,docs){
var latestTweetId = 0;
var sinceId = Infinity;
docs.forEach(function(steamChat,ind,arr){
if(steamChat.latestTweetId < sinceId && steamChat.latestTweetId > 0)
{
sinceId = steamChat.latestTweetId;
}
});
maxId = null;
latestTweetId = sinceId;
console.log(sinceId);
var sales = {
tweets: []
};
getLatestSales(sinceId,maxId,latestTweetId,sales,function(sales){
console.log(sales);
SteamChat.find({}, function(err,docs){
docs.forEach(function(steamChat,ind,arr){
console.log("Sending sales to:");
console.log(steamChat);
userTweets = sales.tweets.filter(function(tweet){
if(tweet.id > steamChat.latestTweetId) return true;
});
var userSales = sales;
userSales.tweets = userTweets;
userSales.tweets.forEach(function(tweet,ind,arr){
bot.sendMessage({"chat_id" : steamChat.id , "text" : tweet.text},function(nodifiedPromise){});
SteamChat.update({"id": steamChat.id}, {$set: {"latestTweetId": tweet.id}}, {"upsert": true}, function(err,result){});
});
});
});
});
});
}
,function(){
console.log('job done');
}
,true);
var bot = new Bot({
token: config.telegram.token
})
.on('message', function (message) {
console.log(message);
if(message.hasOwnProperty("text")){
splitStr = message.text.split(" ");
if(splitStr[0] === "/steamsale")
{
if(splitStr.length === 2)
{
if(splitStr[1] === "start")
{
var startCallback = function(err,steamChat){
if(err)
{
console.log(err)
return
}
if(steamChat === null)
{
SteamChat.update({"id": message.chat.id}, {$set: {"id": message.chat.id, "latestTweetId": 0}}, {"upsert": true, "new": true}, function(err,result){
bot.sendMessage({"chat_id" : message.chat.id , "text" : "Posting Steam sales to this chat"},function(nodifiedPromise){});
sinceId = null;
maxId = null;
latestTweetId = 0;
var sales = {
tweets: [],
message: message
};
getLatestSales(sinceId,maxId,latestTweetId,sales,function(salesResult){
var maxId = 0;
salesResult.tweets.forEach(function(tweet,ind,arr){
bot.sendMessage({"chat_id" : salesResult.message.chat.id , "text" : tweet.text},function(nodifiedPromise){});
if(tweet.id > maxId){
maxId = tweet.id;
}
});
SteamChat.update({"id": salesResult.message.chat.id}, {$set: {"latestTweetId": maxId}}, {"upsert": true}, function(err,result){});
});
});
} else
{
bot.sendMessage({"chat_id" : message.chat.id , "text" : "This chat is already receiving Steam sales"},function(nodifiedPromise){});
}
};
startCallback.message = message;
SteamChat.findOne({"id" : message.chat.id},startCallback);
} else if (splitStr[1] === "stop")
{
var stopCallback = function(err,steamChat){
if(err)
{
console.log(err)
return
}
if(steamChat === null)
{
bot.sendMessage({"chat_id" : message.chat.id , "text" : "This chat does not receive Steam sales"},function(nodifiedPromise){});
} else
{
bot.sendMessage({"chat_id" : message.chat.id , "text" : "This chat is no longer receiving Steam sales"},function(nodifiedPromise){});
}
};
stopCallback.message = message;
SteamChat.findOneAndRemove({"id" : message.chat.id},stopCallback);
}
}
}
if(splitStr[0] === "/start")
{
bot.sendMessage({"chat_id" : message.chat.id , "text" : "This bot keeps you posted on latest Steam sales. You can use it individually or add to a group chat.\nUsage:\n/steamsale start : The user or group chat will start receiving Steam sale news. The bot will post some recent sales to kick things off\n/steamsale stop : The user or group chat will stop receiving updates"},function(nodifiedPromise){});
}
}
})
.start();