-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweet.js
74 lines (64 loc) · 1.72 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
67
68
69
70
71
72
73
74
const Twitter = require('twitter');
const fs = require('fs');
const client = new Twitter({
consumer_key: process.env.TWITTER_CONSUMER_KEY,
consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
});
const uploadImage = image => {
return new Promise((resolve, reject) => {
client.post('media/upload', { media: image }, function (
error,
media,
response
) {
if (!error) {
console.log('Image uploaded!');
resolve(media.media_id_string);
} else {
console.log(error, 'There was an error posting the image!');
reject();
}
});
});
};
const uploadImages = images => {
const promises = [];
images.forEach(image => {
promises.push(uploadImage(image));
});
return Promise.all(promises);
};
const postTweet = imageIds => {
const imagesString = imageIds.toString();
const status = {
status: '🥜 time!',
media_ids: imagesString, // Pass the media ids string
};
return new Promise((resolve, reject) => {
client.post('statuses/update', status, function (error, tweet, response) {
if (!error) {
console.log('Tweet posted!');
resolve(tweet);
} else {
console.log(error, 'There was an error posting the tweet!');
reject();
}
});
});
};
const sendTweet = async images => {
// Make post request on media endpoint. Pass file data as media parameter
try {
const imageIds = await uploadImages(images);
const tweet = await postTweet(imageIds);
console.log(tweet.text);
// Tweet it
} catch (e) {
console.log(e);
}
};
module.exports = {
sendTweet,
};