@codebam/cf-workers-telegram-bot 12.6.11 → 12.6.13

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/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 } from './utils.js';
10
+ import { markdownToHtml, fetchTool, searchTool } 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, 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, searchTool, 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 } from './utils.js';
5
+ import { markdownToHtml, fetchTool, searchTool } 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, HistoryManager, getBalance, };
9
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, searchTool, HistoryManager, getBalance, };
package/dist/utils.d.ts CHANGED
@@ -32,3 +32,20 @@ export declare const fetchTool: {
32
32
  body?: string;
33
33
  }) => Promise<string>;
34
34
  };
35
+ export declare const searchTool: {
36
+ name: string;
37
+ description: string;
38
+ parameters: {
39
+ type: string;
40
+ properties: {
41
+ query: {
42
+ type: string;
43
+ description: string;
44
+ };
45
+ };
46
+ required: string[];
47
+ };
48
+ function: ({ query }: {
49
+ query: string;
50
+ }) => Promise<string>;
51
+ };
package/dist/utils.js CHANGED
@@ -122,3 +122,69 @@ 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.',
128
+ parameters: {
129
+ type: 'object',
130
+ properties: {
131
+ query: { type: 'string', description: 'The search query to search for' },
132
+ },
133
+ required: ['query'],
134
+ },
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
168
+ try {
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 },
172
+ });
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
+ }
184
+ }
185
+ catch (e) {
186
+ return `Error executing search: All public search instances and Wikipedia fallback failed. Error: ${String(e)}`;
187
+ }
188
+ return 'Error executing search: All public search instances and Wikipedia fallback returned no results.';
189
+ },
190
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "12.6.11",
3
+ "version": "12.6.13",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",