@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.
- package/dist/utils.js +48 -16
- 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
|
|
138
|
-
const res = await fetch(
|
|
139
|
-
|
|
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
|
-
|
|
152
|
-
|
|
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
|
};
|