@codebam/cf-workers-telegram-bot 12.6.18 → 12.6.19

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/ai.js CHANGED
@@ -79,7 +79,7 @@ input, config) {
79
79
  return `- Name: ${t.name}\n Description: ${t.description}\n Parameters: ${JSON.stringify(t.parameters)}`;
80
80
  })
81
81
  .join('\n');
82
- const promptInstruction = `\n\n[SYSTEM INSTRUCTION] You have access to the following tools:\n${toolInstructions}\n\nTo use a tool, you MUST output a tool call wrapped in XML format, like so:\n<tool_call>{"name": "search", "arguments": {"query": "query" }}</tool_call>\nor\n<tool_call>{"name": "fetch", "arguments": {"url": "https://example.com" }}</tool_call>\n\nMake sure the tool call is outputted EXACTLY as shown. The system will intercept this call, execute the tool, and return the output to you. Do not write code or direct the user to run code; call the tools yourself.`;
82
+ const promptInstruction = `\n\n[SYSTEM INSTRUCTION] You have access to the following tools:\n${toolInstructions}\n\nTo use a tool, you MUST output a tool call wrapped in XML format, like so:\n<tool_call>{"name": "wikipedia", "arguments": {"query": "query" }}</tool_call>\nor\n<tool_call>{"name": "fetch", "arguments": {"url": "https://example.com" }}</tool_call>\n\nMake sure the tool call is outputted EXACTLY as shown. The system will intercept this call, execute the tool, and return the output to you. Do not write code or direct the user to run code; call the tools yourself.`;
83
83
  systemMsg.content = systemMsg.content + promptInstruction;
84
84
  }
85
85
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
package/dist/main.d.ts CHANGED
@@ -7,8 +7,8 @@ import TelegramCommand from './types/TelegramCommand.js';
7
7
  import TelegramGuestMessage from './types/TelegramGuestMessage.js';
8
8
  import PartialTelegramUpdate from './types/PartialTelegramUpdate.js';
9
9
  import TelegramInlineQueryType from './types/TelegramInlineQueryType.js';
10
- import { markdownToHtml, fetchTool, searchTool } from './utils.js';
10
+ import { markdownToHtml, fetchTool, wikipediaTool } from './utils.js';
11
11
  import { HistoryManager, getBalance } from './history_manager.js';
12
12
  export { extractText, customRunWithTools, streamAiResponseToTelegram, createMockTelegramExecutionContext } from './ai.js';
13
13
  export default TelegramBot;
14
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramVoice, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, markdownToHtml, fetchTool, searchTool, HistoryManager, getBalance, };
14
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramApiBaseParams, SendMessageParams, SendMessageDraftParams, SendPhotoParams, SendVideoParams, SendVoiceParams, SendChatActionParams, AnswerCallbackParams, AnswerInlineParams, AnswerGuestParams, SendInvoiceParams, AnswerPreCheckoutParams, TelegramApiParams, TelegramCommand, TelegramFrom, TelegramChat, TelegramUser, TelegramMessageEntity, TelegramPhotoSize, TelegramMessage, TelegramVoice, TelegramGuestMessage, TelegramInputMessageContent, TelegramInlineQuery, TelegramUpdate, PartialTelegramUpdate, TelegramInlineQueryType, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, ChatPermissions, TelegramBusinessMessage, TelegramCallbackQuery, TelegramPreCheckoutQuery, TelegramDocument, TelegramSuccessfulPayment, markdownToHtml, fetchTool, wikipediaTool, HistoryManager, getBalance, };
package/dist/main.js CHANGED
@@ -2,8 +2,8 @@ import TelegramBot from './telegram_bot.js';
2
2
  import TelegramExecutionContext from './telegram_execution_context.js';
3
3
  import Webhook from './webhook.js';
4
4
  import TelegramApi from './telegram_api.js';
5
- import { markdownToHtml, fetchTool, searchTool } from './utils.js';
5
+ import { markdownToHtml, fetchTool, wikipediaTool } from './utils.js';
6
6
  import { HistoryManager, getBalance } from './history_manager.js';
7
7
  export { extractText, customRunWithTools, streamAiResponseToTelegram, createMockTelegramExecutionContext } from './ai.js';
8
8
  export default TelegramBot;
9
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, searchTool, HistoryManager, getBalance, };
9
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, wikipediaTool, HistoryManager, getBalance, };
package/dist/utils.d.ts CHANGED
@@ -32,7 +32,7 @@ export declare const fetchTool: {
32
32
  body?: string;
33
33
  }) => Promise<string>;
34
34
  };
35
- export declare const searchTool: {
35
+ export declare const wikipediaTool: {
36
36
  name: string;
37
37
  description: string;
38
38
  parameters: {
package/dist/utils.js CHANGED
@@ -122,9 +122,9 @@ export const fetchTool = {
122
122
  }
123
123
  },
124
124
  };
125
- export const searchTool = {
126
- name: 'search',
127
- description: 'Perform a web search using the SearXNG search engine to look up answers, facts, news, and find information from different websites.',
125
+ export const wikipediaTool = {
126
+ name: 'wikipedia',
127
+ description: 'Perform a search on Wikipedia to look up answers, facts, and find information.',
128
128
  parameters: {
129
129
  type: 'object',
130
130
  properties: {
@@ -134,81 +134,7 @@ export const searchTool = {
134
134
  },
135
135
  function: async (args) => {
136
136
  const query = args.query || args.q || '';
137
- const instances = [
138
- 'https://searxng.site/',
139
- 'https://priv.au/',
140
- 'https://search.mdosch.de/',
141
- 'https://ooglester.com/',
142
- 'https://copp.gg/',
143
- 'https://baresearch.org/',
144
- ];
145
137
  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';
146
- for (const instance of instances) {
147
- try {
148
- const url = `${instance}search?q=${encodeURIComponent(query)}&format=json`;
149
- const res = await fetch(url, {
150
- method: 'GET',
151
- headers: {
152
- 'User-Agent': userAgent,
153
- Accept: 'application/json',
154
- },
155
- });
156
- if (res.status === 200) {
157
- const text = await res.text();
158
- const parsed = JSON.parse(text);
159
- if (parsed && Array.isArray(parsed.results) && parsed.results.length > 0) {
160
- return text.slice(0, 15000);
161
- }
162
- }
163
- }
164
- catch {
165
- // Continue to next fallback
166
- }
167
- }
168
- // Fallback to DuckDuckGo Lite Search
169
- try {
170
- const ddgUrl = 'https://lite.duckduckgo.com/lite/';
171
- const ddgRes = await fetch(ddgUrl, {
172
- method: 'POST',
173
- headers: {
174
- 'User-Agent': userAgent,
175
- 'Content-Type': 'application/x-www-form-urlencoded',
176
- Accept: 'text/html',
177
- },
178
- body: `q=${encodeURIComponent(query)}`,
179
- });
180
- if (ddgRes.status === 200 || ddgRes.status === 202) {
181
- const html = await ddgRes.text();
182
- const cleanHtml = (str) => str
183
- .replace(/<[^>]*>/g, '')
184
- .replace(/&nbsp;/g, ' ')
185
- .replace(/\s+/g, ' ')
186
- .trim();
187
- const links = [];
188
- const linkRegex = /<a[^>]*class='result-link'[^>]*href="([^"]+)"[^>]*>([\s\S]*?)<\/a>/g;
189
- let match;
190
- while ((match = linkRegex.exec(html)) !== null) {
191
- links.push({ url: match[1], title: cleanHtml(match[2]) });
192
- }
193
- const snippets = [];
194
- const snippetRegex = /<td[^>]*class='result-snippet'[^>]*>([\s\S]*?)<\/td>/g;
195
- while ((match = snippetRegex.exec(html)) !== null) {
196
- snippets.push(cleanHtml(match[1]));
197
- }
198
- if (links.length > 0) {
199
- const results = links.map((link, i) => ({
200
- title: link.title,
201
- url: link.url,
202
- snippet: snippets[i] || '',
203
- }));
204
- return JSON.stringify({ results });
205
- }
206
- }
207
- }
208
- catch {
209
- // Continue to next fallback
210
- }
211
- // Fallback to Wikipedia Search if DuckDuckGo Lite is blocked/rate-limited
212
138
  try {
213
139
  const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
214
140
  const res = await fetch(wikiUrl, {
@@ -228,40 +154,9 @@ export const searchTool = {
228
154
  }
229
155
  }
230
156
  }
231
- catch {
232
- // Continue to next fallback
233
- }
234
- // Final fallback to Google News RSS search for recent general web/news results
235
- try {
236
- const googleNewsUrl = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=en-US&gl=US&ceid=US:en`;
237
- const res = await fetch(googleNewsUrl, {
238
- headers: { 'User-Agent': userAgent },
239
- });
240
- if (res.status === 200) {
241
- const xml = await res.text();
242
- const items = [];
243
- const itemRegex = /<item>([\s\S]*?)<\/item>/g;
244
- let match;
245
- while ((match = itemRegex.exec(xml)) !== null && items.length < 5) {
246
- const content = match[1];
247
- const titleMatch = /<title>([\s\S]*?)<\/title>/.exec(content);
248
- const linkMatch = /<link>([\s\S]*?)<\/link>/.exec(content);
249
- const descMatch = /<description>([\s\S]*?)<\/description>/.exec(content);
250
- const title = titleMatch ? titleMatch[1].replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
251
- const link = linkMatch ? linkMatch[1] : '';
252
- const desc = descMatch ? descMatch[1].replace(/<[^>]*>/g, '').replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
253
- if (title && link) {
254
- items.push({ title, url: link, snippet: desc });
255
- }
256
- }
257
- if (items.length > 0) {
258
- return JSON.stringify({ results: items });
259
- }
260
- }
261
- }
262
- catch {
263
- // Continue
157
+ catch (e) {
158
+ return `Error executing Wikipedia search: ${String(e)}`;
264
159
  }
265
- return 'Error executing search: All public search instances, Wikipedia fallback, and Google News fallback returned no results.';
160
+ return 'Error executing Wikipedia search: No results found.';
266
161
  },
267
162
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "12.6.18",
3
+ "version": "12.6.19",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",