-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtwitterBot.js
70 lines (58 loc) · 2.43 KB
/
twitterBot.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
require('dotenv').config();
const Twitter = require('twitter-v2');
const { Configuration, OpenAIApi } = require('openai');
// Конфигуриране на Twitter API клиента
const client = new Twitter({
consumer_key: process.env.TWITTER_API_KEY,
consumer_secret: process.env.TWITTER_API_KEY_SECRET,
access_token_key: process.env.TWITTER_ACCESS_TOKEN,
access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
});
// Конфигуриране на OpenAI API клиента
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Функция за генериране на отговор на туит с помощта на ChatGPT
async function generateReply(tweet_content) {
const prompt = `Give me a funny response to this tweet: "${tweet_content}", that encourages people to click the link I send alongside it.`;
try {
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: prompt,
max_tokens: 100,
});
return response.data.choices[0].text.trim();
} catch (error) {
console.error("Error generating reply:", error);
return "Sorry, something went wrong!";
}
}
// Функция за обхождане на нови туитове и отговаряне с интервал от 30 секунди
async function replyToTweets() {
try {
// Търсене на туитове с конкретен критерий
const tweets = await client.get('tweets/search/recent', {
query: 'your search query', // Задай твоята търсена фраза или критерии
'tweet.fields': 'created_at',
});
if (tweets.data) {
for (let tweet of tweets.data) {
const reply = await generateReply(tweet.text);
await client.post('statuses/update', {
status: `@${tweet.author_id} ${reply}`,
in_reply_to_status_id: tweet.id
});
console.log(`Replied to tweet ${tweet.id}`);
// Изчакване за 30 секунди между всяка публикация
await new Promise(resolve => setTimeout(resolve, 30000));
}
}
} catch (error) {
console.error("Error replying to tweets:", error);
}
// Изчакване 1 минута преди ново опресняване
setTimeout(replyToTweets, 60000);
}
// Стартиране на цикъла
replyToTweets();