-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.js
66 lines (57 loc) · 1.68 KB
/
tweet.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
'use strict';
const http = require('http');
const TwitterClient = require('twitter');
const Tweets = require('./data/tweets.json')
const Twitter = new TwitterClient({
"consumer_key": process.env.CONSUMER_KEY,
"consumer_secret": process.env.CONSUMER_SECRET,
"access_token_key": process.env.TOKEN_KEY,
"access_token_secret": process.env.TOKEN_SECRET
});
let tweet = (tweet, callback) => {
Twitter.post('statuses/update', { status: tweet.message, media_ids: tweet.media_ids }, function (error, tweet, response) {
if (error) {
console.log(error);
}
callback(null, {
statusCode: 200,
body: JSON.stringify({
tweet: tweet,
response: response
}),
});
});
};
module.exports.tweet = (event, context, callback) => {
Tweets.map((item) => {
let now = new Date();
let date = `${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()}T${now.getHours()}`;
if (date == item.tweetOn) {
if (item.media) {
http.get(item.media, function(res){
var imageData = '';
res.setEncoding('binary');
res.on('data', function(chunk){
imageData += chunk;
});
res.on('end', function(){
Twitter.post('media/upload', {media: imageData}, function(error, media, response) {
if (!error) {
tweet({
message: item.media,
media_ids: media.media_id_string
}, callback);
} else {
console.log(error);
}
});
});
});
} else {
tweet(item, callback);
}
} else {
console.log('Nothing to do');
}
});
};