forked from dsgriffin/opensea-sales-x-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweet.js
65 lines (55 loc) · 1.95 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
const axios = require('axios');
const twit = require('twit');
const dotenv = require("dotenv")
dotenv.config()
const twitterConfig = {
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token: process.env.ACCESS_TOKEN_KEY,
access_token_secret: process.env.ACCESS_TOKEN_SECRET,
};
const twitterClient = new twit(twitterConfig);
// Tweet a text-based status
async function tweet(tweetText) {
const tweet = {
status: tweetText,
};
twitterClient.post('statuses/update', tweet, (error, tweet, response) => {
if (!error) {
console.log(`Successfully tweeted: ${tweetText}`);
} else {
console.error(error);
}
});
}
// OPTIONAL - use this method if you want the tweet to include the full image file of the OpenSea item in the tweet.
async function tweetWithImage(tweetText, imageUrl) {
// Format our image to base64
const processedImage = await getBase64(imageUrl);
// Upload the item's image from OpenSea to Twitter & retrieve a reference to it
twitterClient.post('media/upload', { media_data: processedImage }, (error, media, response) => {
if (!error) {
const tweet = {
status: tweetText,
media_ids: [media.media_id_string]
};
twitterClient.post('statuses/update', tweet, (error, tweet, response) => {
if (!error) {
console.log(`Successfully tweeted: ${tweetText}`);
} else {
console.error(error);
}
});
} else {
console.error(error);
}
});
}
// Format a provided URL into it's base64 representation
function getBase64(url) {
return axios.get(url, { responseType: 'arraybuffer'}).then(response => Buffer.from(response.data, 'binary').toString('base64'))
}
module.exports = {
tweet: tweet,
tweetWithImage: tweetWithImage
};