Skip to content

Commit be682b5

Browse files
committed
updated & tested plugin
1 parent ddc8dea commit be682b5

File tree

2 files changed

+61
-13
lines changed

2 files changed

+61
-13
lines changed

packages/plugin-news/src/actions/news.ts

+57-9
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,69 @@ export const currentNewsAction: Action = {
3333
throw new Error('NEWS_API_KEY environment variable is not set');
3434
}
3535

36-
const response = await fetch(`https://newsapi.org/v2/everything?q=${searchTerm}&sortBy=publishedAt&apiKey=${apiKey}`);
37-
const data = await response.json();
38-
39-
if (!data.articles || !Array.isArray(data.articles)) {
36+
// Add quotes and additional context terms
37+
const enhancedSearchTerm = encodeURIComponent(`"${searchTerm}" AND (Spain OR Spanish OR Madrid OR Felipe)`);
38+
39+
const [everythingResponse, headlinesResponse] = await Promise.all([
40+
fetch(
41+
`https://newsapi.org/v2/everything?` +
42+
`q=${enhancedSearchTerm}&` +
43+
`sortBy=relevancy&` +
44+
`language=en&` +
45+
`pageSize=50&` +
46+
`apiKey=${apiKey}`
47+
),
48+
fetch(
49+
`https://newsapi.org/v2/top-headlines?` +
50+
`q=${searchTerm}&` +
51+
`country=es&` +
52+
`language=en&` +
53+
`pageSize=50&` +
54+
`apiKey=${apiKey}`
55+
)
56+
]);
57+
58+
const [everythingData, headlinesData] = await Promise.all([
59+
everythingResponse.json(),
60+
headlinesResponse.json()
61+
]);
62+
63+
// Combine and filter articles
64+
const allArticles = [
65+
...(headlinesData.articles || []),
66+
...(everythingData.articles || [])
67+
].filter(article =>
68+
article.title &&
69+
article.description &&
70+
(article.title.toLowerCase().includes(searchTerm.toLowerCase()) ||
71+
article.description.toLowerCase().includes(searchTerm.toLowerCase()))
72+
);
73+
74+
// Remove duplicates and get up to 15 articles
75+
const uniqueArticles = Array.from(
76+
new Map(allArticles.map(article => [article.title, article])).values()
77+
).slice(0, 15);
78+
79+
if (!uniqueArticles.length) {
4080
return "No news articles found.";
4181
}
4282

43-
return data.articles.slice(0, 5).map(article => {
44-
const content = article.content || article.description || "No content available";
45-
return `${article.title || "No title"}\n${article.description || "No description"}\n${article.url || ""}\n${content.slice(0, 1000)}`;
46-
}).join("\n\n");
83+
return uniqueArticles.map((article, index) => {
84+
const content = article.description || "No content available";
85+
const urlDomain = article.url ? new URL(article.url).hostname : "";
86+
return `📰 Article ${index + 1}\n` +
87+
`━━━━━━━━━━━━━━━━━━━━━━\n` +
88+
`📌 **${article.title || "No title"}**\n\n` +
89+
`📝 ${content}\n\n` +
90+
`🔗 Read more at: ${urlDomain}\n`;
91+
}).join("\n");
4792
} catch (error) {
4893
console.error("Error fetching news:", error);
4994
return "Sorry, there was an error fetching the news.";
5095
}
5196
}
5297

53-
const context = `Extract the search term from the {{userName}} message. The message is: ${_message.content.text}. Only return the search term, no other text.`
98+
const context = `What is the specific topic or subject the user wants news about? Extract ONLY the search term from this message: "${_message.content.text}". Return just the search term with no additional text, punctuation, or explanation.`
5499

55100
const searchTerm = await generateText({
56101
runtime: _runtime,
@@ -59,6 +104,9 @@ export const currentNewsAction: Action = {
59104
stop: ["\n"],
60105
});
61106

107+
// For debugging
108+
console.log("Search term extracted:", searchTerm);
109+
62110
const currentNews = await getCurrentNews(searchTerm);
63111
const responseText = ` *protocol droid noises*\n\n${currentNews}`;
64112

packages/plugin-news/src/index.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { Plugin } from "@ai16z/eliza";
2-
import { currentNewsAction } from "./actions/news.ts";
3-
4-
export * as actions from "./actions";
2+
import { currentNewsAction } from "./actions/news";
53

64
export const newsPlugin: Plugin = {
7-
name: "news",
5+
name: "newsPlugin",
86
description: "Get the latest news about a specific topic if asked by the user.",
97
actions: [currentNewsAction],
108
};
9+
10+
export default newsPlugin;

0 commit comments

Comments
 (0)