-
Notifications
You must be signed in to change notification settings - Fork 0
/
lambda.js
425 lines (359 loc) · 11.3 KB
/
lambda.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* Perform the following steps:
*
* 1) Get list of blog tweet templates
* 2) Get history of blog tweets
* 3) Choose random template, weighted so least tweeted is most likely
* 4) Get Twitter credentials
* 5) Tweet template + link to blog post
* 6) Update tweet history to reflect this new tweet
* 7) Use SNS to send e-mail notification
*
*/
var AWS = require("aws-sdk");
AWS.config.update({region: 'us-east-1'});
var sns = new AWS.SNS();
var Twit = require("twit");
var docClient = new AWS.DynamoDB.DocumentClient();
var blogServer = "https://ian-says.com/";
var snsTopic = "arn:aws:sns:us-east-1:632386139671:blog-tweeted";
/**
* Get tweet templates from DynamoDB
*/
function getTemplates(soFar) {
var templateTable = {
// Limit: 2,
TableName: "blog-tweet-templates"
};
// If we've done nothing yet, make the first DB call
if (!soFar) {
return docClient.scan(templateTable).promise()
.then(getTemplates([]));
}
/**
* This is the function to handle the result of the promise from getting
* the templates.
*/
return function (data) {
// Combine new data with existing data.
// Also add "tweeted" count of zero - will be populated from history
// table, later.
var allItems = soFar;
data.Items.forEach(function (item) {
item.tweeted = 0;
allItems[item["template-id"]] = item;
});
// Continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined") {
templateTable.ExclusiveStartKey = data.LastEvaluatedKey;
return docClient.scan(templateTable).promise()
.then(getTemplates(allItems));
}
// If there are no more items, return our data as a resolved promise,
// so it can be chained
return Promise.resolve(allItems);
};
}
/**
* Get our tweet history from DynamoDB
*/
function getHistory(soFar) {
var historyTable = {
// Limit: 2,
TableName: "blog-tweet-history"
};
// If "soFar" is null, we should make an initial call to DynamoDB.
if (soFar === null) {
return function (templates) {
return docClient.scan(historyTable).promise()
.then(getHistory(templates));
};
}
// If we get here, we are handling the response from a tweet history
// DynamoDB scan
return function (data) {
// Enrich templates with history information
var enriched = soFar;
data.Items.forEach(function (item) {
enriched[item["template-id"]].tweeted += 1;
});
// Continue scanning if we have more items
if (typeof data.LastEvaluatedKey != "undefined") {
historyTable.ExclusiveStartKey = data.LastEvaluatedKey;
return docClient.scan(historyTable).promise()
.then(getHistory(enriched));
}
// If there are no more items, select a random tweet and return it
var selectedTweet = randomTweet(enriched);
if (selectedTweet !== null) {
return Promise.resolve(selectedTweet);
}
return Promise.reject(new Error("Could not find an active tweet template"));
};
}
/**
* We want to weight the selection of tweets, so the one which
* has been tweeted more times has less chance of being selected
*/
function randomTweet(tweets) {
// Find the total of all the tweeted counts
var total = tweets.map(function (tweet) {
return tweet.tweeted;
}).reduce(function (sum, val) {
return sum + val;
}, 0);
// Edge case for empty table (total = 0), set it to 1
if (total === 0) {
total = 1;
}
// Assign weights based on occurrences
var weights = tweets.map(function (tweet) {
if (!tweet.enabled) {
return null;
}
return {
"template": tweet["template-id"],
"weight": (total - tweet.tweeted) / total
};
}).filter(x => {return x;});
// Weighting calculation doesn't work if there's only one active template
// (because total and tweet.tweeted are equal, so weight is zero)
if (weights.length === 1) {
return tweets[weights[0].template];
}
// Build up an array based on weighting
var selection = [];
weights.forEach(function (tweet) {
var n;
for (n = 0; n < Math.floor(100 * tweet.weight); n++) {
selection.push(tweet.template);
}
});
// Edge case - we have no active templates
if (selection.length === 0) {
return null;
}
return tweets[selection[Math.floor(Math.random() * selection.length)]];
}
/**
* Get our Twitter credentials
*/
function getTwitterCredentials(tweet) {
var credentialsTable = {
TableName: "blog-tweet-keys",
Key: {
user: "ianf"
}
};
return docClient.get(credentialsTable).promise()
.then(processTwitterCredentials(tweet));
}
/**
* Process the response from a Twitter credential query
*/
function processTwitterCredentials(tweet) {
return function (data) {
return Promise.resolve({
tweet: tweet,
credentials: data.Item
});
};
}
/**
* We want the past 5 tweets to all have been manual, if we are going to auto-post
*/
function checkForNaturalTweets(data) {
return function(tweets) {
var naturalPosts = tweets.data
.map(x => x.source)
.filter(x => x.includes("Twitter for iPhone"));
data.okayToPost = tweets.data.length === naturalPosts.length;
return data;
};
}
/**
* Check recent tweets (to avoid spamming your timeline
*/
function checkRecentTweets(data) {
var twitter = new Twit({
consumer_key: data.credentials["consumer-key"],
consumer_secret: data.credentials["consumer-secret"],
access_token: data.credentials["access-token"],
access_token_secret: data.credentials["access-secret"]
});
return twitter.get("statuses/user_timeline", {count: 5})
.then(checkForNaturalTweets(data));
}
/**
* Send a tweet
*/
function sendTweet(data) {
var twitter = new Twit({
consumer_key: data.credentials["consumer-key"],
consumer_secret: data.credentials["consumer-secret"],
access_token: data.credentials["access-token"],
access_token_secret: data.credentials["access-secret"]
});
return twitter.post("statuses/update", { status: data.tweet.message + " " + blogServer + "articles/" + data.tweet.slug + "/" })
.then(processSentTweet(data.tweet));
}
/**
* Mock sending a tweet, for testing
*/
function mockSendTweet(data) {
return Promise.resolve({
data: {
id_str: (Math.floor(Math.random() * 999999999) + 1000000000).toString(),
text: "This is a mock tweet",
created_at: "Sometime",
user: {
screen_name: "mockuser",
id_str: "12345"
}
}
}).then(processSentTweet(data.tweet));
}
/**
* Handle result of a tweet - adds in template details
*/
function processSentTweet(template) {
return function(data) {
data.template = template;
// Check for failure
if (data.resp.statusCode !== 200) {
var errorPayload = {
response: {
status: data.resp.statusCode,
message: data.resp.statusMessage,
date: data.resp.headers.date
},
errors: data.data.errors,
template: template
};
updateErrors(errorPayload);
errorToSns(errorPayload);
return Promise.reject(errorPayload);
}
return Promise.resolve(data);
};
}
/**
* Update the tweet errors table in DynamoDB
*/
function updateErrors(data) {
var params = {
TableName: "blog-tweet-errors",
Item: {
date: data.response.date,
status: "" + data.response.status + " " + data.response.message,
"template-id": data.template["template-id"],
slug: data.template.slug,
errors: data.errors
.map(err => {return "" + err.code + " " + err.message;})
.join("; ")
}
};
// Deep into errors here, so use a callback
docClient.put(params, function (err, data) {
if (err) {
console.error(err);
}
});
}
/**
* Send an e-mail on error in tweeting
*/
function errorToSns(data) {
var params = {
Message: JSON.stringify(data),
Subject: "ERROR tweeting about blog: " + data.template.slug,
TopicArn: snsTopic
};
// Use a callback, because we're buried in errors already
sns.publish(params, function (err, data) {
if (err) {
console.error(err);
}
});
}
/**
* Update the history table in DynamoDB with details of our tweet
*/
function updateHistory(tweet) {
var params = {
TableName: "blog-tweet-history",
Item: {
"template-id": tweet.template["template-id"],
"tweet-id": tweet.data.id_str,
message: tweet.data.text,
slug: tweet.template.slug,
user: tweet.data.user.screen_name,
"user-id": tweet.data.user.id_str,
date: tweet.data.created_at
}
};
// So, "put" doesn't allow us to return the created item, so we pass
// it on from our params
return docClient.put(params).promise()
.then(function () { return Promise.resolve(params.Item); });
}
/**
* Add an entry to an SNS queue, to trigger an e-mail
*/
function sendToSns(data) {
var params = {
Message: JSON.stringify(data),
Subject: "Tweeted about blog: " + data.slug,
TopicArn: snsTopic
};
return sns.publish(params).promise()
.then(confirmSnsPublication(data));
}
/**
* Add a "didn't post" message to SNS
*/
function sendSkippedToSns(data) {
var params = {
Message: "Skipped automated blog tweet because there are not enough natural tweets since the last automated tweet",
Subject: "Skipped automated blog tweet",
TopicArn: snsTopic
};
return sns.publish(params).promise()
.then(confirmSnsPublication(data));
}
/**
* Validate that the SNS publish succeeded
*/
function confirmSnsPublication(tweet) {
return function (data) {
console.log("TWEET", tweet);
console.log("DATA", data);
return Promise.resolve(tweet);
};
}
// What we expose from the module
exports.handler = (event, context, callback) => {
function errorHandler(err) {
console.error(err);
callback("Error tweeting about blog");
}
function successMessage(data) {
console.info(data);
callback(null, "Tweeted about blog: " + data.slug);
}
getTemplates()
.then(getHistory(null))
.then(getTwitterCredentials)
.then(checkRecentTweets)
.then(data => {
if (data.okayToPost) {
// mockSendTweet(data)
return sendTweet(data)
.then(updateHistory)
.then(sendToSns)
.then(successMessage);
}
return sendSkippedToSns(data);
})
.catch(errorHandler);
};