@codebam/cf-workers-telegram-bot 12.6.17 → 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 +1 -1
- package/dist/main.d.ts +2 -2
- package/dist/main.js +2 -2
- package/dist/utils.d.ts +1 -1
- package/dist/utils.js +6 -68
- package/package.json +1 -1
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": "
|
|
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,
|
|
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,
|
|
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,
|
|
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,
|
|
9
|
+
export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, markdownToHtml, fetchTool, wikipediaTool, HistoryManager, getBalance, };
|
package/dist/utils.d.ts
CHANGED
package/dist/utils.js
CHANGED
|
@@ -122,9 +122,9 @@ export const fetchTool = {
|
|
|
122
122
|
}
|
|
123
123
|
},
|
|
124
124
|
};
|
|
125
|
-
export const
|
|
126
|
-
name: '
|
|
127
|
-
description: 'Perform a
|
|
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,38 +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 Wikipedia Search if all SearXNG instances fail/are rate-limited
|
|
169
138
|
try {
|
|
170
139
|
const wikiUrl = `https://en.wikipedia.org/w/api.php?action=query&list=search&srsearch=${encodeURIComponent(query)}&utf8=&format=json`;
|
|
171
140
|
const res = await fetch(wikiUrl, {
|
|
@@ -185,40 +154,9 @@ export const searchTool = {
|
|
|
185
154
|
}
|
|
186
155
|
}
|
|
187
156
|
}
|
|
188
|
-
catch {
|
|
189
|
-
|
|
190
|
-
}
|
|
191
|
-
// Final fallback to Google News RSS search for recent general web/news results
|
|
192
|
-
try {
|
|
193
|
-
const googleNewsUrl = `https://news.google.com/rss/search?q=${encodeURIComponent(query)}&hl=en-US&gl=US&ceid=US:en`;
|
|
194
|
-
const res = await fetch(googleNewsUrl, {
|
|
195
|
-
headers: { 'User-Agent': userAgent },
|
|
196
|
-
});
|
|
197
|
-
if (res.status === 200) {
|
|
198
|
-
const xml = await res.text();
|
|
199
|
-
const items = [];
|
|
200
|
-
const itemRegex = /<item>([\s\S]*?)<\/item>/g;
|
|
201
|
-
let match;
|
|
202
|
-
while ((match = itemRegex.exec(xml)) !== null && items.length < 5) {
|
|
203
|
-
const content = match[1];
|
|
204
|
-
const titleMatch = /<title>([\s\S]*?)<\/title>/.exec(content);
|
|
205
|
-
const linkMatch = /<link>([\s\S]*?)<\/link>/.exec(content);
|
|
206
|
-
const descMatch = /<description>([\s\S]*?)<\/description>/.exec(content);
|
|
207
|
-
const title = titleMatch ? titleMatch[1].replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
|
|
208
|
-
const link = linkMatch ? linkMatch[1] : '';
|
|
209
|
-
const desc = descMatch ? descMatch[1].replace(/<[^>]*>/g, '').replace(/<!\[CDATA\[([\s\S]*?)\]\]>/g, '$1') : '';
|
|
210
|
-
if (title && link) {
|
|
211
|
-
items.push({ title, url: link, snippet: desc });
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
if (items.length > 0) {
|
|
215
|
-
return JSON.stringify({ results: items });
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
catch {
|
|
220
|
-
// Continue
|
|
157
|
+
catch (e) {
|
|
158
|
+
return `Error executing Wikipedia search: ${String(e)}`;
|
|
221
159
|
}
|
|
222
|
-
return 'Error executing search:
|
|
160
|
+
return 'Error executing Wikipedia search: No results found.';
|
|
223
161
|
},
|
|
224
162
|
};
|