-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtweet.ts
41 lines (32 loc) · 1.22 KB
/
tweet.ts
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
import * as dotenv from "dotenv";
dotenv.config();
import fs from "fs";
import { TwitterApi } from "twitter-api-v2";
// Free API limit is 1500 tweets per month = 50 tweets per day = 2 tweets per hour
const TWEET_INTERVAL = 1000 * 60 * 30;
let interval: NodeJS.Timer;
console.log("Starting tweet bot");
const postTweet = async (reason: string) => {
console.log("Posting tweet because of", reason);
const tweets: string[] = JSON.parse(fs.readFileSync("tweets.json", "utf8"));
const tweet = tweets.shift();
if (!tweet) {
console.log("No more tweets to tweet");
clearInterval(interval);
return;
}
console.log("Tweeting:", tweet);
const consumerClient = new TwitterApi({
appKey: process.env.TWITTER_APP_KEY!,
appSecret: process.env.TWITTER_APP_SECRET!,
accessToken: process.env.TWITTER_ACCESS_TOKEN!,
accessSecret: process.env.TWITTER_ACCESS_SECRET!,
});
const { data: createdTweet } = await consumerClient.v2.tweet(tweet);
console.log("Tweeted:", createdTweet);
fs.writeFileSync("tweets.json", JSON.stringify(tweets, null, 2));
};
console.log("Posting first tweet");
postTweet("first tweet");
console.log("Setting interval");
interval = setInterval(() => postTweet("cron"), TWEET_INTERVAL);