-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
47 lines (36 loc) · 1.15 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
const { TwitterApi } = require("twitter-api-v2");
const fetch = require("node-fetch");
if (process.env.NODE_ENV === "development") {
require("dotenv").config();
}
const twitterClient = new TwitterApi({
appKey: process.env.API_KEY,
appSecret: process.env.API_KEY_SECRET,
accessToken: process.env.ACCESS_TOKEN,
accessSecret: process.env.ACCESS_TOKEN_SECRET,
});
async function uploadMediaToTwitter(imageData) {
const mediaKey = await twitterClient.v1.uploadMedia(imageData, {
type: "jpg",
});
return mediaKey;
}
async function main() {
try {
const resp = await fetch(
"https://shibe.online/api/shibes?count=1&urls=true&httpsUrls=true"
);
const json = await resp.json();
const imageUrl = json[0];
const imgResp = await fetch(imageUrl);
const imgBuffer = Buffer.from(await imgResp.arrayBuffer());
const mediaKey = await uploadMediaToTwitter(imgBuffer);
const tweet = await twitterClient.v2.tweet({
media: { media_ids: [mediaKey] },
});
console.log("New shibe successfully posted!", tweet);
} catch (error) {
console.warn("There was a problem posting to shibe bot", error);
}
}
main();