-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathindex.js
89 lines (76 loc) · 2.39 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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import axios from "axios";
import Twit from "twit";
const twitterConsumerKey = "Consumer Key";
const twitterConsumerSecret = "Consumer Secret Key";
const twitterAccessToken = "Acess Token";
const twitterAccessTokenSecret = "Access Token Secret";
const openaiApiKey = "Open AI API key";
function getKeyword() {
// select random keywords
const keywords = [
"reactjs",
"javascript",
"front-end developer",
"javascript developer",
"html",
"css",
"developer job",
"tips for javascript developer",
"website",
"web design",
"developer job",
"MAANG companies",
"coding",
"reading book",
"programmer work culture",
];
const index = Math.floor(Math.random() * keywords.length);
return keywords[index];
}
const api = new Twit({
consumer_key: twitterConsumerKey,
consumer_secret: twitterConsumerSecret,
access_token: twitterAccessToken,
access_token_secret: twitterAccessTokenSecret,
});
async function searchAndComment() {
console.log("Searching for tweets...");
const query = `${getKeyword()}`; // you can also use the "OR" / "AND" between keywords: eg -> javascript OR html"
const maxTweets = 100;
const { data: searchResults } = await api.get("search/tweets", {
q: query,
count: maxTweets,
});
console.log(
`Found ${searchResults.statuses.length} tweets. Generating comments...`
);
for (const tweet of searchResults.statuses) {
const { data: response } = await axios.post(
"https://api.openai.com/v1/completions",
{
model: "text-davinci-003",
prompt: `Comment on this tweet: "${tweet.text}", the reply to this tweet must be like i am writing it and also include some emoji that matches the generated text`,
max_tokens: 70,
temperature: 0.5,
top_p: 1,
},
{
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${openaiApiKey}`,
},
}
);
const comment = response.choices[0].text;
console.log(comment);
const { data: postResponse } = await api.post("statuses/update", {
status: `@${tweet.user.screen_name} ${comment}`,
in_reply_to_status_id: tweet.id_str,
});
console.log(`Comment posted: ${postResponse.text}`);
// Delay each iteration for 30min
await new Promise((resolve) => setTimeout(resolve, 30 * 60 * 1000));
}
searchAndComment();
}
searchAndComment();