-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexaMPLE.JS
581 lines (489 loc) · 19.4 KB
/
exaMPLE.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
require('dotenv').config();
const { GoogleGenerativeAI } = require("@google/generative-ai");
const { TwitterApi } = require('twitter-api-v2');
const schedule = require('node-schedule');
const axios = require('axios');
const https = require('https');
// Configuration
const config = {
tweetInterval: '0 * * * *', // Every hour at minute 0
testMode: true,
newsKeywords: ["AI", "Tech", "SpaceX", "NASA", "Machine Learning", "Innovation", "Startups", "SAAS"],
maxRetries: 3,
tweetMaxLength: 280,
aiPromptVariations: 5,
topTweetKeywords: ["ai", "saas", "google", "openai", "microsoft", "apple", "nvidia", "memecoins"]
};
// Validate environment variables
const requiredEnvVars = [
'GEMINI_API_KEY',
'TWITTER_API_KEY',
'TWITTER_API_SECRET',
'TWITTER_BEARER_TOKEN',
'TWITTER_ACCESS_TOKEN',
'TWITTER_ACCESS_SECRET'
];
requiredEnvVars.forEach(varName => {
if (!process.env[varName]) {
console.error(`Missing required environment variable: ${varName}`);
process.exit(1);
}
});
// Initialize APIs
const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
const aiModel = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
const twitterClient = new TwitterApi({
appKey: process.env.TWITTER_API_KEY,
appSecret: process.env.TWITTER_API_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_SECRET,
});
// Rate limit tracking
const rateLimits = {
tweets: {
remaining: 50,
reset: 0
}
};
// Enhanced logger
const logger = {
info: (...args) => console.log(`[${new Date().toISOString()}] INFO:`, ...args),
error: (...args) => console.error(`[${new Date().toISOString()}] ERROR:`, ...args)
};
// Twitter API Helper
class TwitterHelper {
static async searchRecentTweets(query) {
try {
const result = await twitterClient.v2.search(query, {
'tweet.fields': ['public_metrics'],
max_results: 10
});
if (!result?.data?.length) return null;
// Filter tweets with engagement
return result.data.filter(t =>
t.public_metrics.like_count > 10 ||
t.public_metrics.retweet_count > 5
);
} catch (error) {
logger.error('Twitter search failed:', error);
return null;
}
}
static async fetchTopTweets() {
const randomKeyword = config.topTweetKeywords[Math.floor(Math.random() * config.topTweetKeywords.length)];
const options = {
method: 'GET',
url: 'https://twitter-v24.p.rapidapi.com/search/',
params: {
query: randomKeyword,
section: 'top',
limit: '10'
},
headers: {
'x-rapidapi-key': 'aa81d9bcc0mshdbcda7e2ad75055p1ced75jsnf5dacefd3151',
'x-rapidapi-host': 'twitter-v24.p.rapidapi.com'
}
};
try {
const response = await axios.request(options);
if (response.data && response.data.search_by_raw_query && response.data.search_by_raw_query.search_timeline && response.data.search_by_raw_query.search_timeline.timeline && response.data.search_by_raw_query.search_timeline.timeline.instructions) {
const entries = response.data.search_by_raw_query.search_timeline.timeline.instructions.find(item => item.type === 'TimelineAddEntries')?.entries;
if (entries) {
const validTweets = entries.filter(entry => entry.entryId && entry.entryId.startsWith("tweet-")).map(entry => {
return entry.content?.itemContent?.tweet_results?.result?.legacy?.full_text;
}).filter(Boolean);
if(validTweets.length > 0){
return validTweets[Math.floor(Math.random() * validTweets.length)];
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
} catch (error) {
logger.error('Top tweets fetch failed:', error);
return null;
}
}
static async postTweet(text) {
if (config.testMode) {
logger.info('Test mode - Would have tweeted:', text);
return { data: { id: 'test_id' } };
}
try {
const response = await twitterClient.v2.tweet(text);
logger.info(`Tweet posted: ${response.data.id}`);
return response;
} catch (error) {
this.handleRateLimits(error);
throw error;
}
}
static handleRateLimits(error) {
if (error.rateLimit) {
rateLimits.tweets = {
remaining: error.rateLimit.remaining,
reset: error.rateLimit.reset
};
logger.info(`Rate limits updated - Remaining: ${error.rateLimit.remaining}`);
}
}
}
// AI Helper
class AIHelper {
static async generateTweet(prompt) {
let retries = 0;
while (retries < config.maxRetries) {
try {
const result = await aiModel.generateContent(prompt);
const text = result.response.text().trim();
if (!text) {
throw new Error('Invalid tweet length');
}
return text;
} catch (error) {
logger.error(`AI generation attempt ${retries + 1} failed:`, error);
}
retries++;
await new Promise(resolve => setTimeout(resolve, 2000));
}
return null;
}
}
// Content Generation
class ContentGenerator {
static async getNewsContext() {
try {
const response = await fetch(`https://api.currentsapi.services/v1/search?domain=zdnet.com&keywords=${
config.newsKeywords[Math.floor(Math.random() * config.newsKeywords.length)]
}&language=en&apiKey=${process.env.CURRENTS_API_KEY}`);
const data = await response.json();
return data.news?.[Math.floor(Math.random() * data.news.length)]?.description;
} catch (error) {
logger.error('News fetch failed:', error);
return null;
}
}
static async generateTweetContent() {
let retries = 0;
while (retries < config.maxRetries) {
try {
const news = await this.getNewsContext();
const topTweet = await TwitterHelper.fetchTopTweets();
const prompt = this.createPrompt(news, topTweet);
const tweet = await AIHelper.generateTweet(prompt);
if (tweet) {
if (news) {
return {text: tweet, source: 'news'}
} else if (topTweet) {
return {text: tweet, source: 'topTweet'}
}
else {
return {text: tweet, source: 'default'}
}
}
} catch (error) {
logger.error(`Content generation attempt ${retries + 1} failed:`, error);
}
retries++;
await new Promise(resolve => setTimeout(resolve, 2000));
}
const defaultPrompt = `Generate a short, funny tweet about a trending topic. Don't use hashtags or beg for engagement, keep it under ${config.tweetMaxLength} characters.`
const tweet = await AIHelper.generateTweet(defaultPrompt);
if(tweet){
return {text: tweet, source: 'default'}
}
return null;
}
static createPrompt(news, tweet) {
const prompts = [
`Create a casual tech-related tweet in the style of a 25-year-old, keeping it under ${config.tweetMaxLength} characters. Don't include any hashtags or beg for engagement, focus on trending topics. ${news ? 'React to this news:' + news : ''}`,
`Generate a humorous reaction to ${tweet ? 'this tweet: ' + tweet : 'current tech trends'}. Keep it conversational, but don't include any hashtags or beg for anything.`,
`Write a tweet that combines ${news ? 'this news: ' + news : 'tech'} with everyday life observations. Casual tone. Do not beg for anything or use hashtags.`,
`Create a tweet posing an interesting question about ${news ? 'this: ' + news : 'recent tech developments'}. Don't include hashtags or beg.`,
`Generate a short tech hot-take in the style of a young professional. Do not include hashtags or beg for anything, ${tweet ? 'Respond to: ' + tweet : ''}`
];
return prompts[Math.floor(Math.random() * prompts.length)];
}
}
// Main Execution
async function executeTweetCycle() {
try {
if (rateLimits.tweets.remaining < 1) {
const resetTime = new Date(rateLimits.tweets.reset * 1000);
logger.info(`Rate limit exhausted. Next reset at: ${resetTime.toISOString()}`);
return;
}
const tweetData = await ContentGenerator.generateTweetContent();
if(tweetData){
logger.info(`Tweet will be based on ${tweetData.source}`);
await TwitterHelper.postTweet(tweetData.text);
}
} catch (error) {
logger.error('Tweet cycle failed:', error);
}
}
// Initialization and Scheduling
(async () => {
logger.info('Starting Twitter bot...');
// Initial tweet
await executeTweetCycle();
// Schedule regular tweets
schedule.scheduleJob(config.tweetInterval, async () => {
logger.info('Starting scheduled tweet cycle...');
await executeTweetCycle();
});
if (config.testMode) {
logger.info('Running in test mode - tweets will not be actually posted');
schedule.scheduleJob('*/30 * * * * *', executeTweetCycle);
}
})();
// require('dotenv').config();
// const { GoogleGenerativeAI } = require("@google/generative-ai");
// const { TwitterApi } = require('twitter-api-v2');
// const schedule = require('node-schedule');
// const https = require('https');
// // Configuration
// const config = {
// tweetInterval: '0 * * * *', // Every hour at minute 0
// testMode: true,
// newsKeywords: ["AI", "Tech", "SpaceX", "NASA", "Machine Learning", "Innovation", "Startups", "SAAS"],
// maxRetries: 3,
// tweetMaxLength: 280,
// aiPromptVariations: 5
// };
// // Validate environment variables
// const requiredEnvVars = [
// 'GEMINI_API_KEY',
// 'TWITTER_API_KEY',
// 'TWITTER_API_SECRET',
// 'TWITTER_ACCESS_TOKEN',
// 'TWITTER_ACCESS_SECRET'
// ];
// requiredEnvVars.forEach(varName => {
// if (!process.env[varName]) {
// console.error(`Missing required environment variable: ${varName}`);
// process.exit(1);
// }
// });
// // Initialize APIs
// const genAI = new GoogleGenerativeAI(process.env.GEMINI_API_KEY);
// const aiModel = genAI.getGenerativeModel({ model: "gemini-1.5-flash" });
// const twitterClient = new TwitterApi({
// appKey: process.env.TWITTER_API_KEY,
// appSecret: process.env.TWITTER_API_SECRET,
// accessToken: process.env.TWITTER_ACCESS_TOKEN,
// accessSecret: process.env.TWITTER_ACCESS_SECRET,
// });
// // Rate limit tracking
// const rateLimits = {
// tweets: {
// remaining: 50,
// reset: 0
// }
// };
// // Enhanced logger
// const logger = {
// info: (...args) => console.log(`[${new Date().toISOString()}] INFO:`, ...args),
// error: (...args) => console.error(`[${new Date().toISOString()}] ERROR:`, ...args)
// };
// // Twitter API Helper
// class TwitterHelper {
// static async searchRecentTweets(query) {
// try {
// const result = await twitterClient.v2.search(query, {
// 'tweet.fields': ['public_metrics'],
// max_results: 10
// });
// if (!result?.data?.length) return null;
// // Filter tweets with engagement
// return result.data.filter(t =>
// t.public_metrics.like_count > 10 ||
// t.public_metrics.retweet_count > 5
// );
// } catch (error) {
// logger.error('Twitter search failed:', error);
// return null;
// }
// }
// static async fetchTopTweets() {
// return new Promise((resolve, reject) => {
// const options = {
// method: 'GET',
// hostname: 'twitter-api47.p.rapidapi.com',
// port: null,
// path: '/v2/search?query=spacex&type=Top',
// headers: {
// 'x-rapidapi-key': 'aa81d9bcc0mshdbcda7e2ad75055p1ced75jsnf5dacefd3151',
// 'x-rapidapi-host': 'twitter-api47.p.rapidapi.com'
// }
// };
// const req = https.request(options, function (res) {
// const chunks = [];
// res.on('data', function (chunk) {
// chunks.push(chunk);
// });
// res.on('end', function () {
// const body = Buffer.concat(chunks);
// try {
// const data = JSON.parse(body.toString());
// if (data && data.tweets && data.tweets.length > 0) {
// const randomTweet = data.tweets[Math.floor(Math.random() * data.tweets.length)];
// if (randomTweet.content && randomTweet.content.itemContent && randomTweet.content.itemContent.tweet_results && randomTweet.content.itemContent.tweet_results.result && randomTweet.content.itemContent.tweet_results.result.legacy) {
// resolve(randomTweet.content.itemContent.tweet_results.result.legacy.full_text);
// } else {
// resolve(null);
// }
// } else {
// resolve(null);
// }
// } catch (error) {
// reject(error);
// }
// });
// res.on('error', (error) => {
// reject(error);
// });
// });
// req.on('error', (error) => {
// reject(error);
// });
// req.end();
// });
// }
// static async postTweet(text) {
// if (config.testMode) {
// logger.info('Test mode - Would have tweeted:', text);
// return { data: { id: 'test_id' } };
// }
// try {
// const response = await twitterClient.v2.tweet(text);
// logger.info(`Tweet posted: ${response.data.id}`);
// return response;
// } catch (error) {
// this.handleRateLimits(error);
// throw error;
// }
// }
// static handleRateLimits(error) {
// if (error.rateLimit) {
// rateLimits.tweets = {
// remaining: error.rateLimit.remaining,
// reset: error.rateLimit.reset
// };
// logger.info(`Rate limits updated - Remaining: ${error.rateLimit.remaining}`);
// }
// }
// }
// // AI Helper
// class AIHelper {
// static async generateTweet(prompt) {
// let retries = 0;
// while (retries < config.maxRetries) {
// try {
// const result = await aiModel.generateContent(prompt);
// const text = result.response.text().trim();
// if (!text || text.length > config.tweetMaxLength) {
// throw new Error('Invalid tweet length');
// }
// return text;
// } catch (error) {
// logger.error(`AI generation attempt ${retries + 1} failed:`, error);
// }
// retries++;
// await new Promise(resolve => setTimeout(resolve, 2000));
// }
// return null;
// }
// }
// // Content Generation
// class ContentGenerator {
// static async getNewsContext() {
// try {
// const response = await fetch(`https://api.currentsapi.services/v1/search?keywords=${
// config.newsKeywords[Math.floor(Math.random() * config.newsKeywords.length)]
// }&language=en&apiKey=${process.env.CURRENTS_API_KEY}`);
// const data = await response.json();
// return data.news?.[Math.floor(Math.random() * data.news.length)]?.description;
// } catch (error) {
// logger.error('News fetch failed:', error);
// return null;
// }
// }
// static async generateTweetContent() {
// let retries = 0;
// while (retries < config.maxRetries) {
// try {
// const news = await this.getNewsContext();
// const topTweet = await TwitterHelper.fetchTopTweets();
// const prompt = this.createPrompt(news, topTweet);
// const tweet = await AIHelper.generateTweet(prompt);
// if (tweet) return tweet;
// } catch (error) {
// logger.error(`Content generation attempt ${retries + 1} failed:`, error);
// }
// retries++;
// await new Promise(resolve => setTimeout(resolve, 2000));
// }
// return this.generateFallbackContent();
// }
// static createPrompt(news, tweet) {
// const prompts = [
// `Create a casual tech-related tweet in the style of a 25-year-old, keeping it under ${config.tweetMaxLength} characters. Don't include any hashtags or beg for engagement, focus on trending topics. ${news ? 'React to this news:' + news : ''}`,
// `Generate a humorous reaction to ${tweet ? 'this tweet: ' + tweet : 'current tech trends'}. Keep it conversational, but don't include any hashtags or beg for anything.`,
// `Write a tweet that combines ${news ? 'this news: ' + news : 'tech'} with everyday life observations. Casual tone. Do not beg for anything or use hashtags.`,
// `Create a tweet posing an interesting question about ${news ? 'this: ' + news : 'recent tech developments'}. Don't include hashtags or beg.`,
// `Generate a short tech hot-take in the style of a young professional. Do not include hashtags or beg for anything, ${tweet ? 'Respond to: ' + tweet : ''}`
// ];
// return prompts[Math.floor(Math.random() * prompts.length)];
// }
// static generateFallbackContent() {
// const fallbacks = [
// "Just read an interesting tech article but can't share details yet. What's everyone reading in the tech space today?",
// "Thinking about how fast AI is evolving. What tech development has surprised you most recently?",
// "Sometimes I wonder if we're living in the future yet. What piece of tech still feels futuristic to you?",
// "Had a random thought about space exploration and everyday tech. What's your favorite sci-fi tech becoming reality?",
// "Debating with friends about the most impactful tech of the decade. What would you nominate?",
// "I'm feeling very philosophical today, what do you think will be the most significant thing in the future, AI or Space?",
// "Just thinking about the future, what will be the biggest change in the world?",
// "What should i learn today?",
// "What is the most useful tech?",
// "Did anyone else experience that?",
// ];
// return fallbacks[Math.floor(Math.random() * fallbacks.length)];
// }
// }
// // Main Execution
// async function executeTweetCycle() {
// try {
// if (rateLimits.tweets.remaining < 1) {
// const resetTime = new Date(rateLimits.tweets.reset * 1000);
// logger.info(`Rate limit exhausted. Next reset at: ${resetTime.toISOString()}`);
// return;
// }
// const tweetContent = await ContentGenerator.generateTweetContent();
// if(tweetContent){
// await TwitterHelper.postTweet(tweetContent);
// }
// } catch (error) {
// logger.error('Tweet cycle failed:', error);
// }
// }
// // Initialization and Scheduling
// (async () => {
// logger.info('Starting Twitter bot...');
// // Initial tweet
// await executeTweetCycle();
// // Schedule regular tweets
// schedule.scheduleJob(config.tweetInterval, async () => {
// logger.info('Starting scheduled tweet cycle...');
// await executeTweetCycle();
// });
// if (config.testMode) {
// logger.info('Running in test mode - tweets will not be actually posted');
// schedule.scheduleJob('*/30 * * * * *', executeTweetCycle);
// }
// })();