@@ -33,24 +33,69 @@ export const currentNewsAction: Action = {
33
33
throw new Error ( 'NEWS_API_KEY environment variable is not set' ) ;
34
34
}
35
35
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 ) {
40
80
return "No news articles found." ;
41
81
}
42
82
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" ) ;
47
92
} catch ( error ) {
48
93
console . error ( "Error fetching news:" , error ) ;
49
94
return "Sorry, there was an error fetching the news." ;
50
95
}
51
96
}
52
97
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 .`
54
99
55
100
const searchTerm = await generateText ( {
56
101
runtime : _runtime ,
@@ -59,6 +104,9 @@ export const currentNewsAction: Action = {
59
104
stop : [ "\n" ] ,
60
105
} ) ;
61
106
107
+ // For debugging
108
+ console . log ( "Search term extracted:" , searchTerm ) ;
109
+
62
110
const currentNews = await getCurrentNews ( searchTerm ) ;
63
111
const responseText = ` *protocol droid noises*\n\n${ currentNews } ` ;
64
112
0 commit comments