By: Mayne
Show top 10 hacker news in notification
// index.ts
export default async function (input: Input, context: Context) {
try {
// Fetch top stories from Hacker News API
const topStoriesResponse = await fetch('https://hacker-news.firebaseio.com/v0/topstories.json');
const topStories = await topStoriesResponse.json();
// Get only top 10 story IDs
const top10Ids = topStories.slice(0, 10);
// Fetch details for each story
const storiesPromises = top10Ids.map(async (id: number) => {
const storyResponse = await fetch(`https://hacker-news.firebaseio.com/v0/item/${id}.json`);
return await storyResponse.json();
});
const stories = await Promise.all(storiesPromises);
// Format stories for notification
const storiesList = stories
.map((story: any, index: number) => `${index + 1}. [${story.title}](${story.url || '#'})`)
.join('\n');
// Show notification with top 10 stories
eidos.currentSpace.notify({
title: "Top 10 Hacker News Stories",
description: `Here are the current top 10 stories on Hacker News:\n\n${storiesList}`
});
return { success: true, message: "Displayed top 10 Hacker News stories" };
} catch (error) {
const errorMessage = error instanceof Error ? error.message : String(error);
eidos.currentSpace.notify({
title: "Error",
description: `Failed to fetch Hacker News stories: ${errorMessage}`
});
return { success: false, error: errorMessage };
}
}
// Define input JSON schema (no input parameters needed for this script)
export const inputJSONSchema = {
type: "object",
properties: {}
};
// Define output JSON schema
export const outputJSONSchema = {
type: "object",
properties: {
success: {
type: "boolean"
},
message: {
type: "string"
},
error: {
type: "string"
}
}
};
// Export commands for script execution
export const commands = [
{
name: "default",
description: "Fetch and display top 10 Hacker News stories",
inputJSONSchema: inputJSONSchema,
outputJSONSchema: outputJSONSchema,
asTableAction: false
}
];