@codebam/cf-workers-telegram-bot 12.6.12 → 12.6.14

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 +48 -16
  2. package/package.json +1 -1
package/dist/utils.js CHANGED
@@ -133,26 +133,58 @@ export const searchTool = {
133
133
  required: ['query'],
134
134
  },
135
135
  function: async ({ query }) => {
136
+ const instances = [
137
+ 'https://searxng.site/',
138
+ 'https://priv.au/',
139
+ 'https://search.mdosch.de/',
140
+ 'https://ooglester.com/',
141
+ 'https://copp.gg/',
142
+ 'https://baresearch.org/',
143
+ ];
144
+ const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36';
145
+ for (const instance of instances) {
146
+ try {
147
+ const url = `${instance}search?q=${encodeURIComponent(query)}&format=json`;
148
+ const res = await fetch(url, {
149
+ method: 'GET',
150
+ headers: {
151
+ 'User-Agent': userAgent,
152
+ Accept: 'application/json',
153
+ },
154
+ });
155
+ if (res.status === 200) {
156
+ const text = await res.text();
157
+ const parsed = JSON.parse(text);
158
+ if (parsed && Array.isArray(parsed.results) && parsed.results.length > 0) {
159
+ return text.slice(0, 15000);
160
+ }
161
+ }
162
+ }
163
+ catch {
164
+ // Continue to next fallback
165
+ }
166
+ }
167
+ // Final fallback to Wikipedia Search if all SearXNG instances fail/are rate-limited
136
168
  try {
137
- const url = `https://searxng.p.rapidapi.com/search?q=${encodeURIComponent(query)}&categories=general&engines=google%2Cbing&language=auto&pageno=1&format=json&results_on_new_tab=0&image_proxy=true&safesearch=0`;
138
- const res = await fetch(url, {
139
- method: 'POST',
140
- headers: {
141
- 'x-rapidapi-key': '4c2f1bba03msh767a933cd87b87ep18bd39jsnc2eb23fa9f5d',
142
- 'x-rapidapi-host': 'searxng.p.rapidapi.com',
143
- 'Content-Type': 'application/json',
144
- 'User-Agent': 'Mozilla/5.0 (Cloudflare Worker Telegram Bot)',
145
- },
146
- body: JSON.stringify({
147
- key1: 'value',
148
- key2: 'value',
149
- }),
169
+ const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
170
+ const res = await fetch(wikiUrl, {
171
+ headers: { 'User-Agent': userAgent },
150
172
  });
151
- const text = await res.text();
152
- return text.slice(0, 15000);
173
+ if (res.status === 200) {
174
+ const data = (await res.json());
175
+ if (data && data.query && Array.isArray(data.query.search)) {
176
+ const results = data.query.search.map((item) => ({
177
+ title: item.title,
178
+ snippet: item.snippet.replace(/<\/?[^>]+(>|$)/g, ''), // strip HTML tags
179
+ url: `https://en.wikipedia.org/wiki/${encodeURIComponent(item.title)}`,
180
+ }));
181
+ return JSON.stringify({ results });
182
+ }
183
+ }
153
184
  }
154
185
  catch (e) {
155
- return `Error executing search: ${String(e)}`;
186
+ return `Error executing search: All public search instances and Wikipedia fallback failed. Error: ${String(e)}`;
156
187
  }
188
+ return 'Error executing search: All public search instances and Wikipedia fallback returned no results.';
157
189
  },
158
190
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "12.6.12",
3
+ "version": "12.6.14",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",