@codebam/cf-workers-telegram-bot 12.6.15 → 12.6.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/utils.js +39 -6
  2. package/package.json +1 -1
package/dist/utils.js CHANGED
@@ -164,7 +164,7 @@ export const searchTool = {
164
164
  // Continue to next fallback
165
165
  }
166
166
  }
167
- // Final fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
167
+ // Fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
168
168
  try {
169
169
  const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
170
170
  const res = await fetch(wikiUrl, {
@@ -173,18 +173,51 @@ export const searchTool = {
173
173
  if (res.status === 200) {
174
174
  const data = (await res.json());
175
175
  if (data && data.query && Array.isArray(data.query.search)) {
176
- const results = data.query.search.map((item) => ({
176
+ const wikiResults = data.query.search.map((item) => ({
177
177
  title: item.title,
178
178
  snippet: item.snippet.replace(/<\/?[^>]+(>|$)/g, ''), // strip HTML tags
179
179
  url: `https://en.wikipedia.org/wiki/${encodeURIComponent(item.title)}`,
180
180
  }));
181
- return JSON.stringify({ results });
181
+ if (wikiResults.length > 0) {
182
+ return JSON.stringify({ results: wikiResults });
183
+ }
182
184
  }
183
185
  }
184
186
  }
185
- catch (e) {
186
- return `Error executing search: All public search instances and Wikipedia fallback failed. Error: ${String(e)}`;
187
+ catch {
188
+ // Continue to next fallback
189
+ }
190
+ // Final fallback to Google News RSS search for recent general web/news results
191
+ try {
192
+ const googleNewsUrl = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=en-US&gl=US&ceid=US:en`;
193
+ const res = await fetch(googleNewsUrl, {
194
+ headers: { 'User-Agent': userAgent },
195
+ });
196
+ if (res.status === 200) {
197
+ const xml = await res.text();
198
+ const items = [];
199
+ const itemRegex = /<item>([\s\S]*?)<\/item>/g;
200
+ let match;
201
+ while ((match = itemRegex.exec(xml)) !== null && items.length < 5) {
202
+ const content = match[1];
203
+ const titleMatch = /<title>([\s\S]*?)<\/title>/.exec(content);
204
+ const linkMatch = /<link>([\s\S]*?)<\/link>/.exec(content);
205
+ const descMatch = /<description>([\s\S]*?)<\/description>/.exec(content);
206
+ const title = titleMatch ? titleMatch[1].replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
207
+ const link = linkMatch ? linkMatch[1] : '';
208
+ const desc = descMatch ? descMatch[1].replace(/<[^>]*>/g, '').replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
209
+ if (title && link) {
210
+ items.push({ title, url: link, snippet: desc });
211
+ }
212
+ }
213
+ if (items.length > 0) {
214
+ return JSON.stringify({ results: items });
215
+ }
216
+ }
217
+ }
218
+ catch {
219
+ // Continue
187
220
  }
188
- return 'Error executing search: All public search instances and Wikipedia fallback returned no results.';
221
+ return 'Error executing search: All public search instances, Wikipedia fallback, and Google News fallback returned no results.';
189
222
  },
190
223
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "12.6.15",
3
+ "version": "12.6.16",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",