import { getTweets } from "https://esm.town/v/geoffreylitt/getTweets";
import { email } from "https://esm.town/v/std/email?v=12";
import { OpenAI } from "https://esm.town/v/std/openai?v=4";
import { discordWebhook } from "https://esm.town/v/stevekrouse/discordWebhook";
import { twitterSearch } from "https://esm.town/v/stevekrouse/twitterSearch";
const usernames = [
"stevekrouse",
"worrydream",
"wattenberger",
"mappletons",
"maryrosecook",
"geoffreylitt",
"Mappletons",
];
const openai = new OpenAI();
export async function twitterAlert({ lastRunAt }: Interval) {
let results = [];
for (const username of usernames) {
const tweets = await getTweets(username);
results = results.concat(tweets.data.map(tweet => ({ ...tweet, username })));
await new Promise((resolve) => setTimeout(resolve, 1000));
}
async function filterTweets(tweets) {
const completion = await openai.chat.completions.create({
messages: [
{
role: "system",
content:
`You'll be given a list of tweets as JSON. I want you to filter the list and return a JSON array of IDs, plus a headline for the filtered list: { headline: string; ids: string[] } The filter criteria are:
I want to hear about important tweets sharing major updates, new projects, new papers or essays, website updates, etc. I don't want to see shitposts or jokes.
The headline should be short for an email subject, like 10 words, tease who said what with specifics of what they said.`,
},
{ role: "user", content: JSON.stringify(results) },
],
model: "gpt-4o",
response_format: { type: "json_object" },
});
const parsed = JSON.parse(completion.choices[0].message.content);
return { tweets: tweets.filter(tweet => parsed.ids.includes(tweet.id)), headline: parsed.headline };
}
const { tweets, headline } = await filterTweets(results);
const emailHtml = tweets.map((tweet) => {
const url = `https://fxtwitter.com/${tweet.username}/status/${tweet.id}`;
return `
<div style="border: 1px solid #e1e8ed; padding: 10px; margin-bottom: 10px; border-radius: 10px;">
<p style="margin: 0; font-size: 14px;"><strong>${tweet.username}:</strong> ${tweet.text}</p>
<a href="${url}" style="font-size: 12px; color: #1da1f2;">Read more</a>
</div>
`;
}).join("");
const htmlContent = `
<div style="font-family: Arial, sans-serif; color: #333;">
${emailHtml}
</div>
`;
email({ subject: headline, html: htmlContent });
console.log("Sent!", headline);
console.log(htmlContent);
}