@codebam/cf-workers-telegram-bot 11.12.0 → 11.13.0

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
@@ -27,5 +27,6 @@ import TelegramCallbackQuery from './types/TelegramCallbackQuery.js';
27
27
  import TelegramPreCheckoutQuery from './types/TelegramPreCheckoutQuery.js';
28
28
  import TelegramDocument from './types/TelegramDocument.js';
29
29
  import TelegramSuccessfulPayment from './types/TelegramSuccessfulPayment.js';
30
+ import { markdownToHtml, fetchTool } from './utils.js';
30
31
  export default TelegramBot;
31
- 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, };
32
+ 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, };
package/dist/main.js CHANGED
@@ -8,5 +8,6 @@ import TelegramInlineQueryResultPhoto from './types/TelegramInlineQueryResultPho
8
8
  import TelegramInlineQueryResultArticle from './types/TelegramInlineQueryResultArticle.js';
9
9
  import TelegramInlineQueryResultVideo from './types/TelegramInlineQueryResultVideo.js';
10
10
  import TelegramInlineQueryResultVoice from './types/TelegramInlineQueryResultVoice.js';
11
+ import { markdownToHtml, fetchTool } from './utils.js';
11
12
  export default TelegramBot;
12
- export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, };
13
+ export { TelegramBot, TelegramExecutionContext, Webhook, TelegramApi, TelegramUpdate, TelegramInlineQueryResult, TelegramInlineQueryResultPhoto, TelegramInlineQueryResultArticle, TelegramInlineQueryResultVideo, TelegramInlineQueryResultVoice, markdownToHtml, fetchTool, };
@@ -0,0 +1,34 @@
1
+ export declare function markdownToHtml(s: string): Promise<string>;
2
+ export declare const fetchTool: {
3
+ name: string;
4
+ description: string;
5
+ parameters: {
6
+ type: string;
7
+ properties: {
8
+ url: {
9
+ type: string;
10
+ description: string;
11
+ };
12
+ method: {
13
+ type: string;
14
+ enum: string[];
15
+ default: string;
16
+ };
17
+ headers: {
18
+ type: string;
19
+ description: string;
20
+ };
21
+ body: {
22
+ type: string;
23
+ description: string;
24
+ };
25
+ };
26
+ required: string[];
27
+ };
28
+ function: ({ url, method, headers, body }: {
29
+ url: string;
30
+ method?: string;
31
+ headers?: Record<string, string>;
32
+ body?: string;
33
+ }) => Promise<string>;
34
+ };
package/dist/utils.js ADDED
@@ -0,0 +1,109 @@
1
+ import { marked } from 'marked';
2
+ export async function markdownToHtml(s) {
3
+ const renderer = new marked.Renderer();
4
+ // Telegram supports: b, strong, i, em, u, ins, s, strike, del, span, tg-spoiler, a, code, pre, blockquote
5
+ renderer.heading = ({ tokens, depth }) => {
6
+ const text = renderer.parser.parseInline(tokens);
7
+ if (depth === 1) {
8
+ return `<b>${text}</b>\n\n`;
9
+ }
10
+ if (depth === 2) {
11
+ return `<b>${text}</b>\n\n`;
12
+ }
13
+ return `<b>${text}</b>\n\n`;
14
+ };
15
+ renderer.paragraph = ({ tokens }) => {
16
+ const text = renderer.parser.parseInline(tokens);
17
+ return `${text}\n\n`;
18
+ };
19
+ renderer.br = () => '\n';
20
+ renderer.list = ({ items, ordered, start }) => {
21
+ let result = '';
22
+ for (let i = 0; i < items.length; i++) {
23
+ const item = items[i];
24
+ const prefix = ordered ? `${(start !== '' && start !== undefined) ? Number(start) + i : i + 1}. ` : '• ';
25
+ result += `${prefix}${renderer.listitem(item)}\n`;
26
+ }
27
+ return result;
28
+ };
29
+ renderer.listitem = (item) => {
30
+ return renderer.parser.parseInline(item.tokens);
31
+ };
32
+ renderer.strong = ({ tokens }) => `<b>${renderer.parser.parseInline(tokens)}</b>`;
33
+ renderer.em = ({ tokens }) => `<i>${renderer.parser.parseInline(tokens)}</i>`;
34
+ renderer.codespan = ({ text }) => `<code>${text}</code>`;
35
+ renderer.code = ({ text, lang }) => {
36
+ if (lang) {
37
+ return `<pre><code class="language-${lang}">${text}</code></pre>\n`;
38
+ }
39
+ return `<pre><code>${text}</code></pre>\n`;
40
+ };
41
+ renderer.del = ({ tokens }) => `<s>${renderer.parser.parseInline(tokens)}</s>`;
42
+ renderer.link = ({ href, tokens }) => `<a href="${href}">${renderer.parser.parseInline(tokens)}</a>`;
43
+ renderer.image = ({ href, text }) => `<a href="${href}">${text}</a>`;
44
+ renderer.blockquote = ({ tokens }) => {
45
+ return `<blockquote>${renderer.parser.parse(tokens)}</blockquote>\n`;
46
+ };
47
+ renderer.hr = () => `────────\n\n`;
48
+ // html tag pass-through for supported tags or escaping
49
+ renderer.html = ({ text }) => {
50
+ const allowedTags = [
51
+ 'b', 'strong', 'i', 'em', 'u', 'ins', 's', 'strike', 'del',
52
+ 'span', 'tg-spoiler', 'a', 'code', 'pre', 'blockquote'
53
+ ];
54
+ const match = /^<\/?([a-z0-9-]+)(?:\s+[^>]*)?>/i.exec(text);
55
+ if (match) {
56
+ const tagName = match[1].toLowerCase();
57
+ if (allowedTags.includes(tagName)) {
58
+ return text; // Allow through
59
+ }
60
+ }
61
+ // Escape everything else
62
+ return text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
63
+ };
64
+ renderer.text = (token) => {
65
+ if ('tokens' in token && token.tokens) {
66
+ return renderer.parser.parseInline(token.tokens);
67
+ }
68
+ // Escape standard HTML entities
69
+ return token.text.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
70
+ };
71
+ marked.setOptions({
72
+ gfm: true,
73
+ breaks: true,
74
+ });
75
+ const parsed = await marked.parse(s, { renderer });
76
+ // Trim multiple newlines
77
+ return parsed.replace(/\n{3,}/g, '\n\n').trim();
78
+ }
79
+ export const fetchTool = {
80
+ name: 'fetch',
81
+ description: 'Make an HTTP request to fetch a website or API, returning the HTML or JSON. You MUST use this tool when the user asks to fetch a URL, visit a website, or make a GET request, instead of writing code.',
82
+ parameters: {
83
+ type: 'object',
84
+ properties: {
85
+ url: { type: 'string', description: 'The URL to fetch' },
86
+ method: { type: 'string', enum: ['GET', 'POST', 'PUT', 'DELETE'], default: 'GET' },
87
+ headers: { type: 'object', description: 'HTTP headers to include in the request' },
88
+ body: { type: 'string', description: 'The request body' }
89
+ },
90
+ required: ['url']
91
+ },
92
+ function: async ({ url, method, headers, body }) => {
93
+ try {
94
+ const res = await fetch(url, {
95
+ method: method || 'GET',
96
+ headers: {
97
+ 'User-Agent': 'Mozilla/5.0 (Cloudflare Worker Telegram Bot)',
98
+ ...headers
99
+ },
100
+ body: body ? (typeof body === 'string' ? body : JSON.stringify(body)) : undefined
101
+ });
102
+ const text = await res.text();
103
+ return text.slice(0, 10000);
104
+ }
105
+ catch (e) {
106
+ return `Error executing fetch: ${String(e)}`;
107
+ }
108
+ }
109
+ };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@codebam/cf-workers-telegram-bot",
3
- "version": "11.12.0",
3
+ "version": "11.13.0",
4
4
  "description": "serverless telegram bot on cf workers",
5
5
  "main": "./dist/main.js",
6
6
  "module": "./dist/main.js",
7
- "types": "./dist/types.d.ts",
7
+ "types": "./dist/main.d.ts",
8
8
  "files": [
9
9
  "dist",
10
10
  "LICENSE",
@@ -53,7 +53,8 @@
53
53
  "wrangler": "^4.90.1"
54
54
  },
55
55
  "dependencies": {
56
- "@eslint/eslintrc": "^3.3.5"
56
+ "@eslint/eslintrc": "^3.3.5",
57
+ "marked": "^18.0.3"
57
58
  },
58
59
  "typedocOptions": {
59
60
  "entryPoints": [