Created
January 13, 2023 21:50
-
-
Save stanleycyang/1ddf897fd683e0c4dc7a711c71380abf to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const Twitter = require("twitter"); | |
const openai = require("openai"); | |
module.exports.improveTweets = (event, context, callback) => { | |
// Authenticate with the Twitter API | |
const client = new Twitter({ | |
consumer_key: process.env.CONSUMER_KEY, | |
consumer_secret: process.env.CONSUMER_SECRET, | |
access_token_key: process.env.ACCESS_TOKEN, | |
access_token_secret: process.env.ACCESS_TOKEN_SECRET, | |
}); | |
// Authenticate with the OpenAI API | |
openai.apiKey = process.env.OPENAI_API_KEY; | |
// Search for tweets containing the word "AI" | |
const query = "AI"; | |
const params = { q: query, count: 100 }; | |
client.get("search/tweets", params, (error, tweets) => { | |
if (error) { | |
return callback(error); | |
} | |
// Sort the results by the number of retweets | |
const sortedResults = tweets.statuses.sort((a, b) => { | |
return b.retweet_count - a.retweet_count; | |
}); | |
// Get the best tweet | |
const bestTweet = sortedResults[0]; | |
// Use the OpenAI API to improve the best tweet | |
const prompt = `Improve this tweet: ${bestTweet.text}`; | |
openai.Completion.create( | |
{ | |
engine: "text-davinci-002", | |
prompt: prompt, | |
}, | |
(err, response) => { | |
if (err) { | |
console.log(err); | |
return callback(err); | |
} | |
// Print the improved tweet | |
console.log(`Original tweet: ${bestTweet.text}`); | |
console.log(`Improved tweet: ${response.choices[0].text}\n`); | |
callback(null, "Best tweet improved"); | |
} | |
); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment