@ai-devkit/channel-connector 0.7.0 → 0.8.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/.swcrc +27 -0
- package/dist/ChannelManager.js +8 -12
- package/dist/ChannelManager.js.map +1 -1
- package/dist/ConfigStore.js +21 -17
- package/dist/ConfigStore.js.map +1 -1
- package/dist/__tests__/ChannelManager.test.js +70 -0
- package/dist/__tests__/ChannelManager.test.js.map +1 -0
- package/dist/__tests__/ConfigStore.test.js +146 -0
- package/dist/__tests__/ConfigStore.test.js.map +1 -0
- package/dist/__tests__/adapters/TelegramAdapter.test.js +240 -0
- package/dist/__tests__/adapters/TelegramAdapter.test.js.map +1 -0
- package/dist/__tests__/utils/telegramHtml.test.js +86 -0
- package/dist/__tests__/utils/telegramHtml.test.js.map +1 -0
- package/dist/adapters/ChannelAdapter.js +7 -1
- package/dist/adapters/ChannelAdapter.js.map +1 -1
- package/dist/adapters/TelegramAdapter.js +20 -32
- package/dist/adapters/TelegramAdapter.js.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/types.js +7 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/telegramHtml.js +35 -31
- package/dist/utils/telegramHtml.js.map +1 -1
- package/package.json +11 -7
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import * as telegrafModule from 'telegraf';
|
|
2
|
+
import { TelegramAdapter } from '../../adapters/TelegramAdapter.js';
|
|
3
|
+
// Mock telegraf
|
|
4
|
+
vi.mock('telegraf', ()=>{
|
|
5
|
+
const handlers = {};
|
|
6
|
+
const mockBot = {
|
|
7
|
+
launch: vi.fn().mockResolvedValue(undefined),
|
|
8
|
+
stop: vi.fn().mockResolvedValue(undefined),
|
|
9
|
+
on: vi.fn((event, handler)=>{
|
|
10
|
+
handlers[event] = handler;
|
|
11
|
+
}),
|
|
12
|
+
telegram: {
|
|
13
|
+
sendMessage: vi.fn().mockResolvedValue(undefined),
|
|
14
|
+
getMe: vi.fn().mockResolvedValue({
|
|
15
|
+
username: 'test_bot'
|
|
16
|
+
})
|
|
17
|
+
},
|
|
18
|
+
_handlers: handlers,
|
|
19
|
+
_triggerText: async (chatId, userId, text)=>{
|
|
20
|
+
const ctx = {
|
|
21
|
+
message: {
|
|
22
|
+
chat: {
|
|
23
|
+
id: chatId
|
|
24
|
+
},
|
|
25
|
+
from: {
|
|
26
|
+
id: userId
|
|
27
|
+
},
|
|
28
|
+
text,
|
|
29
|
+
date: Math.floor(Date.now() / 1000)
|
|
30
|
+
},
|
|
31
|
+
reply: vi.fn().mockResolvedValue(undefined)
|
|
32
|
+
};
|
|
33
|
+
if (handlers['text']) {
|
|
34
|
+
await handlers['text'](ctx);
|
|
35
|
+
}
|
|
36
|
+
return ctx;
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
Telegraf: vi.fn(function() {
|
|
41
|
+
return mockBot;
|
|
42
|
+
}),
|
|
43
|
+
__mockBot: mockBot
|
|
44
|
+
};
|
|
45
|
+
});
|
|
46
|
+
function getMockBot() {
|
|
47
|
+
return telegrafModule.__mockBot;
|
|
48
|
+
}
|
|
49
|
+
describe('TelegramAdapter', ()=>{
|
|
50
|
+
let adapter;
|
|
51
|
+
beforeEach(()=>{
|
|
52
|
+
vi.clearAllMocks();
|
|
53
|
+
adapter = new TelegramAdapter({
|
|
54
|
+
botToken: 'test-token-123'
|
|
55
|
+
});
|
|
56
|
+
});
|
|
57
|
+
describe('type', ()=>{
|
|
58
|
+
it('should return "telegram"', ()=>{
|
|
59
|
+
expect(adapter.type).toBe('telegram');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe('start', ()=>{
|
|
63
|
+
it('should launch the telegraf bot', async ()=>{
|
|
64
|
+
const bot = getMockBot();
|
|
65
|
+
await adapter.start();
|
|
66
|
+
expect(bot.launch).toHaveBeenCalled();
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
describe('stop', ()=>{
|
|
70
|
+
it('should stop the telegraf bot', async ()=>{
|
|
71
|
+
const bot = getMockBot();
|
|
72
|
+
await adapter.start();
|
|
73
|
+
await adapter.stop();
|
|
74
|
+
expect(bot.stop).toHaveBeenCalled();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe('onMessage', ()=>{
|
|
78
|
+
it('should silently ignore messages when no handler is registered', async ()=>{
|
|
79
|
+
// Don't register a handler
|
|
80
|
+
await adapter.start();
|
|
81
|
+
const bot = getMockBot();
|
|
82
|
+
const ctx = await bot._triggerText(12345, 67890, 'hello');
|
|
83
|
+
// Should not throw or reply
|
|
84
|
+
expect(ctx.reply).not.toHaveBeenCalled();
|
|
85
|
+
});
|
|
86
|
+
it('should handle non-Error thrown by handler', async ()=>{
|
|
87
|
+
const handler = vi.fn().mockRejectedValue('string error');
|
|
88
|
+
adapter.onMessage(handler);
|
|
89
|
+
await adapter.start();
|
|
90
|
+
const bot = getMockBot();
|
|
91
|
+
const ctx = await bot._triggerText(12345, 67890, 'hello');
|
|
92
|
+
expect(ctx.reply).toHaveBeenCalledWith('Error processing message: Unknown error');
|
|
93
|
+
});
|
|
94
|
+
it('should call handler with IncomingMessage on incoming text', async ()=>{
|
|
95
|
+
const handler = vi.fn().mockResolvedValue(undefined);
|
|
96
|
+
adapter.onMessage(handler);
|
|
97
|
+
await adapter.start();
|
|
98
|
+
const bot = getMockBot();
|
|
99
|
+
await bot._triggerText(12345, 67890, 'hello agent');
|
|
100
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
101
|
+
const msg = handler.mock.calls[0][0];
|
|
102
|
+
expect(msg.channelType).toBe('telegram');
|
|
103
|
+
expect(msg.chatId).toBe('12345');
|
|
104
|
+
expect(msg.userId).toBe('67890');
|
|
105
|
+
expect(msg.text).toBe('hello agent');
|
|
106
|
+
expect(msg.timestamp).toBeInstanceOf(Date);
|
|
107
|
+
});
|
|
108
|
+
it('should handle handler errors gracefully', async ()=>{
|
|
109
|
+
const handler = vi.fn().mockRejectedValue(new Error('handler failed'));
|
|
110
|
+
adapter.onMessage(handler);
|
|
111
|
+
await adapter.start();
|
|
112
|
+
const bot = getMockBot();
|
|
113
|
+
const ctx = await bot._triggerText(12345, 67890, 'hello');
|
|
114
|
+
// Should not throw, and should reply with error
|
|
115
|
+
expect(ctx.reply).toHaveBeenCalledWith(expect.stringContaining('Error processing message'));
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
describe('sendMessage', ()=>{
|
|
119
|
+
it('should send plain text with parse_mode HTML', async ()=>{
|
|
120
|
+
const bot = getMockBot();
|
|
121
|
+
await adapter.sendMessage('12345', 'hello from bot');
|
|
122
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledWith('12345', 'hello from bot', {
|
|
123
|
+
parse_mode: 'HTML'
|
|
124
|
+
});
|
|
125
|
+
});
|
|
126
|
+
it('should render markdown as Telegram HTML', async ()=>{
|
|
127
|
+
const bot = getMockBot();
|
|
128
|
+
await adapter.sendMessage('12345', '**bold** and *italic* and `code`');
|
|
129
|
+
const sent = bot.telegram.sendMessage.mock.calls[0][1];
|
|
130
|
+
expect(sent).toContain('<b>bold</b>');
|
|
131
|
+
expect(sent).toContain('<i>italic</i>');
|
|
132
|
+
expect(sent).toContain('<code>code</code>');
|
|
133
|
+
expect(bot.telegram.sendMessage.mock.calls[0][2]).toEqual({
|
|
134
|
+
parse_mode: 'HTML'
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
it('should chunk messages exceeding 4096 chars', async ()=>{
|
|
138
|
+
const bot = getMockBot();
|
|
139
|
+
const line = 'A'.repeat(100) + '\n';
|
|
140
|
+
const longMessage = line.repeat(50); // 5050 chars
|
|
141
|
+
await adapter.sendMessage('12345', longMessage);
|
|
142
|
+
expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);
|
|
143
|
+
for (const call of bot.telegram.sendMessage.mock.calls){
|
|
144
|
+
expect(call[1].length).toBeLessThanOrEqual(4096);
|
|
145
|
+
expect(call[2]).toEqual({
|
|
146
|
+
parse_mode: 'HTML'
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
it('should hard split at 4096 when no newlines available', async ()=>{
|
|
151
|
+
const bot = getMockBot();
|
|
152
|
+
const longMessage = 'A'.repeat(5000);
|
|
153
|
+
await adapter.sendMessage('12345', longMessage);
|
|
154
|
+
expect(bot.telegram.sendMessage.mock.calls.length).toBe(2);
|
|
155
|
+
expect(bot.telegram.sendMessage.mock.calls[0][1].length).toBe(4096);
|
|
156
|
+
expect(bot.telegram.sendMessage.mock.calls[1][1].length).toBe(904);
|
|
157
|
+
});
|
|
158
|
+
it('should prefer paragraph (\\n\\n) over single \\n when chunking', async ()=>{
|
|
159
|
+
const bot = getMockBot();
|
|
160
|
+
// 4 paragraphs of ~1500 chars each, total > 4096
|
|
161
|
+
const paragraph = 'A'.repeat(1500);
|
|
162
|
+
const message = `${paragraph}\n\n${paragraph}\n\n${paragraph}\n\n${paragraph}`;
|
|
163
|
+
await adapter.sendMessage('12345', message);
|
|
164
|
+
// First chunk should end at a \n\n boundary, not mid-paragraph
|
|
165
|
+
const firstChunk = bot.telegram.sendMessage.mock.calls[0][1];
|
|
166
|
+
expect(firstChunk.endsWith('\n\n')).toBe(true);
|
|
167
|
+
});
|
|
168
|
+
it('should send short messages in a single call', async ()=>{
|
|
169
|
+
const bot = getMockBot();
|
|
170
|
+
await adapter.sendMessage('12345', 'short message');
|
|
171
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);
|
|
172
|
+
});
|
|
173
|
+
it('should retry as plain text when Telegram rejects HTML with parse-entities error', async ()=>{
|
|
174
|
+
const bot = getMockBot();
|
|
175
|
+
const parseError = Object.assign(new Error('400: Bad Request'), {
|
|
176
|
+
description: "Bad Request: can't parse entities: Unsupported start tag \"foo\""
|
|
177
|
+
});
|
|
178
|
+
bot.telegram.sendMessage.mockRejectedValueOnce(parseError).mockResolvedValueOnce(undefined);
|
|
179
|
+
await adapter.sendMessage('12345', '**hello**');
|
|
180
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);
|
|
181
|
+
// First call: rendered HTML with parse_mode
|
|
182
|
+
const [, htmlChunk, htmlOpts] = bot.telegram.sendMessage.mock.calls[0];
|
|
183
|
+
expect(htmlChunk).toContain('<b>hello</b>');
|
|
184
|
+
expect(htmlOpts).toEqual({
|
|
185
|
+
parse_mode: 'HTML'
|
|
186
|
+
});
|
|
187
|
+
// Second call: same content, plain text (tags stripped, no parse_mode)
|
|
188
|
+
const [, plainChunk, plainOpts] = bot.telegram.sendMessage.mock.calls[1];
|
|
189
|
+
expect(plainChunk).toBe('hello');
|
|
190
|
+
expect(plainOpts).toBeUndefined();
|
|
191
|
+
});
|
|
192
|
+
it('should detect parse-entities error from "message" field too', async ()=>{
|
|
193
|
+
const bot = getMockBot();
|
|
194
|
+
// Some error shapes carry the marker on `message` rather than `description`
|
|
195
|
+
const parseError = new Error("can't parse entities");
|
|
196
|
+
bot.telegram.sendMessage.mockRejectedValueOnce(parseError).mockResolvedValueOnce(undefined);
|
|
197
|
+
await adapter.sendMessage('12345', '**hi**');
|
|
198
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);
|
|
199
|
+
});
|
|
200
|
+
it('should decode HTML entities when falling back to plain text', async ()=>{
|
|
201
|
+
const bot = getMockBot();
|
|
202
|
+
const parseError = Object.assign(new Error('400'), {
|
|
203
|
+
description: "Bad Request: can't parse entities"
|
|
204
|
+
});
|
|
205
|
+
bot.telegram.sendMessage.mockRejectedValueOnce(parseError).mockResolvedValueOnce(undefined);
|
|
206
|
+
// Source has chars that escapeHtml encodes; fallback should decode them
|
|
207
|
+
await adapter.sendMessage('12345', 'a < b && c > d');
|
|
208
|
+
const [, plainChunk] = bot.telegram.sendMessage.mock.calls[1];
|
|
209
|
+
expect(plainChunk).toContain('a < b && c > d');
|
|
210
|
+
expect(plainChunk).not.toContain('<');
|
|
211
|
+
expect(plainChunk).not.toContain('&');
|
|
212
|
+
});
|
|
213
|
+
it('should propagate non-parse-entities errors without falling back', async ()=>{
|
|
214
|
+
const bot = getMockBot();
|
|
215
|
+
const otherError = Object.assign(new Error('403'), {
|
|
216
|
+
description: 'Forbidden: bot was blocked by the user'
|
|
217
|
+
});
|
|
218
|
+
bot.telegram.sendMessage.mockRejectedValueOnce(otherError);
|
|
219
|
+
await expect(adapter.sendMessage('12345', 'hi')).rejects.toBe(otherError);
|
|
220
|
+
// Only the HTML attempt should have happened — no fallback retry
|
|
221
|
+
expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);
|
|
222
|
+
});
|
|
223
|
+
});
|
|
224
|
+
describe('isHealthy', ()=>{
|
|
225
|
+
it('should return true after start', async ()=>{
|
|
226
|
+
await adapter.start();
|
|
227
|
+
expect(await adapter.isHealthy()).toBe(true);
|
|
228
|
+
});
|
|
229
|
+
it('should return false before start', async ()=>{
|
|
230
|
+
expect(await adapter.isHealthy()).toBe(false);
|
|
231
|
+
});
|
|
232
|
+
it('should return false after stop', async ()=>{
|
|
233
|
+
await adapter.start();
|
|
234
|
+
await adapter.stop();
|
|
235
|
+
expect(await adapter.isHealthy()).toBe(false);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
//# sourceMappingURL=TelegramAdapter.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/__tests__/adapters/TelegramAdapter.test.ts"],"sourcesContent":["import * as telegrafModule from 'telegraf';\nimport { TelegramAdapter } from '../../adapters/TelegramAdapter.js';\nimport type { IncomingMessage } from '../../types.js';\n\n// Mock telegraf\nvi.mock('telegraf', () => {\n const handlers: Record<string, (...args: any[]) => any> = {};\n const mockBot = {\n launch: vi.fn().mockResolvedValue(undefined),\n stop: vi.fn().mockResolvedValue(undefined),\n on: vi.fn((event: string, handler: (...args: any[]) => any) => {\n handlers[event] = handler;\n }),\n telegram: {\n sendMessage: vi.fn().mockResolvedValue(undefined),\n getMe: vi.fn().mockResolvedValue({ username: 'test_bot' }),\n },\n _handlers: handlers,\n _triggerText: async (chatId: number, userId: number, text: string) => {\n const ctx = {\n message: {\n chat: { id: chatId },\n from: { id: userId },\n text,\n date: Math.floor(Date.now() / 1000),\n },\n reply: vi.fn().mockResolvedValue(undefined),\n };\n if (handlers['text']) {\n await handlers['text'](ctx);\n }\n return ctx;\n },\n };\n return {\n Telegraf: vi.fn(function () { return mockBot; }),\n __mockBot: mockBot,\n };\n});\n\nfunction getMockBot() {\n return (telegrafModule as unknown as { __mockBot: ReturnType<typeof vi.fn>['mock'] & Record<string, unknown> }).__mockBot;\n}\n\ndescribe('TelegramAdapter', () => {\n let adapter: TelegramAdapter;\n\n beforeEach(() => {\n vi.clearAllMocks();\n adapter = new TelegramAdapter({ botToken: 'test-token-123' });\n });\n\n describe('type', () => {\n it('should return \"telegram\"', () => {\n expect(adapter.type).toBe('telegram');\n });\n });\n\n describe('start', () => {\n it('should launch the telegraf bot', async () => {\n const bot = getMockBot();\n await adapter.start();\n expect(bot.launch).toHaveBeenCalled();\n });\n });\n\n describe('stop', () => {\n it('should stop the telegraf bot', async () => {\n const bot = getMockBot();\n await adapter.start();\n await adapter.stop();\n expect(bot.stop).toHaveBeenCalled();\n });\n });\n\n describe('onMessage', () => {\n it('should silently ignore messages when no handler is registered', async () => {\n // Don't register a handler\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n // Should not throw or reply\n expect(ctx.reply).not.toHaveBeenCalled();\n });\n\n it('should handle non-Error thrown by handler', async () => {\n const handler = vi.fn().mockRejectedValue('string error');\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n expect(ctx.reply).toHaveBeenCalledWith(\n 'Error processing message: Unknown error'\n );\n });\n\n it('should call handler with IncomingMessage on incoming text', async () => {\n const handler = vi.fn().mockResolvedValue(undefined);\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n await bot._triggerText(12345, 67890, 'hello agent');\n\n expect(handler).toHaveBeenCalledTimes(1);\n const msg: IncomingMessage = handler.mock.calls[0][0];\n expect(msg.channelType).toBe('telegram');\n expect(msg.chatId).toBe('12345');\n expect(msg.userId).toBe('67890');\n expect(msg.text).toBe('hello agent');\n expect(msg.timestamp).toBeInstanceOf(Date);\n });\n\n it('should handle handler errors gracefully', async () => {\n const handler = vi.fn().mockRejectedValue(new Error('handler failed'));\n adapter.onMessage(handler);\n await adapter.start();\n\n const bot = getMockBot();\n const ctx = await bot._triggerText(12345, 67890, 'hello');\n\n // Should not throw, and should reply with error\n expect(ctx.reply).toHaveBeenCalledWith(\n expect.stringContaining('Error processing message')\n );\n });\n });\n\n describe('sendMessage', () => {\n it('should send plain text with parse_mode HTML', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', 'hello from bot');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledWith(\n '12345',\n 'hello from bot',\n { parse_mode: 'HTML' }\n );\n });\n\n it('should render markdown as Telegram HTML', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', '**bold** and *italic* and `code`');\n\n const sent = bot.telegram.sendMessage.mock.calls[0][1];\n expect(sent).toContain('<b>bold</b>');\n expect(sent).toContain('<i>italic</i>');\n expect(sent).toContain('<code>code</code>');\n expect(bot.telegram.sendMessage.mock.calls[0][2]).toEqual({\n parse_mode: 'HTML',\n });\n });\n\n it('should chunk messages exceeding 4096 chars', async () => {\n const bot = getMockBot();\n const line = 'A'.repeat(100) + '\\n';\n const longMessage = line.repeat(50); // 5050 chars\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBeGreaterThan(1);\n for (const call of bot.telegram.sendMessage.mock.calls) {\n expect(call[1].length).toBeLessThanOrEqual(4096);\n expect(call[2]).toEqual({ parse_mode: 'HTML' });\n }\n });\n\n it('should hard split at 4096 when no newlines available', async () => {\n const bot = getMockBot();\n const longMessage = 'A'.repeat(5000);\n\n await adapter.sendMessage('12345', longMessage);\n\n expect(bot.telegram.sendMessage.mock.calls.length).toBe(2);\n expect(bot.telegram.sendMessage.mock.calls[0][1].length).toBe(4096);\n expect(bot.telegram.sendMessage.mock.calls[1][1].length).toBe(904);\n });\n\n it('should prefer paragraph (\\\\n\\\\n) over single \\\\n when chunking', async () => {\n const bot = getMockBot();\n // 4 paragraphs of ~1500 chars each, total > 4096\n const paragraph = 'A'.repeat(1500);\n const message = `${paragraph}\\n\\n${paragraph}\\n\\n${paragraph}\\n\\n${paragraph}`;\n\n await adapter.sendMessage('12345', message);\n\n // First chunk should end at a \\n\\n boundary, not mid-paragraph\n const firstChunk = bot.telegram.sendMessage.mock.calls[0][1];\n expect(firstChunk.endsWith('\\n\\n')).toBe(true);\n });\n\n it('should send short messages in a single call', async () => {\n const bot = getMockBot();\n await adapter.sendMessage('12345', 'short message');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n });\n\n it('should retry as plain text when Telegram rejects HTML with parse-entities error', async () => {\n const bot = getMockBot();\n const parseError = Object.assign(new Error('400: Bad Request'), {\n description: \"Bad Request: can't parse entities: Unsupported start tag \\\"foo\\\"\",\n });\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n await adapter.sendMessage('12345', '**hello**');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);\n\n // First call: rendered HTML with parse_mode\n const [, htmlChunk, htmlOpts] = bot.telegram.sendMessage.mock.calls[0];\n expect(htmlChunk).toContain('<b>hello</b>');\n expect(htmlOpts).toEqual({ parse_mode: 'HTML' });\n\n // Second call: same content, plain text (tags stripped, no parse_mode)\n const [, plainChunk, plainOpts] = bot.telegram.sendMessage.mock.calls[1];\n expect(plainChunk).toBe('hello');\n expect(plainOpts).toBeUndefined();\n });\n\n it('should detect parse-entities error from \"message\" field too', async () => {\n const bot = getMockBot();\n // Some error shapes carry the marker on `message` rather than `description`\n const parseError = new Error(\"can't parse entities\");\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n await adapter.sendMessage('12345', '**hi**');\n\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(2);\n });\n\n it('should decode HTML entities when falling back to plain text', async () => {\n const bot = getMockBot();\n const parseError = Object.assign(new Error('400'), {\n description: \"Bad Request: can't parse entities\",\n });\n bot.telegram.sendMessage\n .mockRejectedValueOnce(parseError)\n .mockResolvedValueOnce(undefined);\n\n // Source has chars that escapeHtml encodes; fallback should decode them\n await adapter.sendMessage('12345', 'a < b && c > d');\n\n const [, plainChunk] = bot.telegram.sendMessage.mock.calls[1];\n expect(plainChunk).toContain('a < b && c > d');\n expect(plainChunk).not.toContain('<');\n expect(plainChunk).not.toContain('&');\n });\n\n it('should propagate non-parse-entities errors without falling back', async () => {\n const bot = getMockBot();\n const otherError = Object.assign(new Error('403'), {\n description: 'Forbidden: bot was blocked by the user',\n });\n bot.telegram.sendMessage.mockRejectedValueOnce(otherError);\n\n await expect(adapter.sendMessage('12345', 'hi')).rejects.toBe(otherError);\n\n // Only the HTML attempt should have happened — no fallback retry\n expect(bot.telegram.sendMessage).toHaveBeenCalledTimes(1);\n });\n });\n\n describe('isHealthy', () => {\n it('should return true after start', async () => {\n await adapter.start();\n expect(await adapter.isHealthy()).toBe(true);\n });\n\n it('should return false before start', async () => {\n expect(await adapter.isHealthy()).toBe(false);\n });\n\n it('should return false after stop', async () => {\n await adapter.start();\n await adapter.stop();\n expect(await adapter.isHealthy()).toBe(false);\n });\n });\n});\n"],"names":["telegrafModule","TelegramAdapter","vi","mock","handlers","mockBot","launch","fn","mockResolvedValue","undefined","stop","on","event","handler","telegram","sendMessage","getMe","username","_handlers","_triggerText","chatId","userId","text","ctx","message","chat","id","from","date","Math","floor","Date","now","reply","Telegraf","__mockBot","getMockBot","describe","adapter","beforeEach","clearAllMocks","botToken","it","expect","type","toBe","bot","start","toHaveBeenCalled","not","mockRejectedValue","onMessage","toHaveBeenCalledWith","toHaveBeenCalledTimes","msg","calls","channelType","timestamp","toBeInstanceOf","Error","stringContaining","parse_mode","sent","toContain","toEqual","line","repeat","longMessage","length","toBeGreaterThan","call","toBeLessThanOrEqual","paragraph","firstChunk","endsWith","parseError","Object","assign","description","mockRejectedValueOnce","mockResolvedValueOnce","htmlChunk","htmlOpts","plainChunk","plainOpts","toBeUndefined","otherError","rejects","isHealthy"],"mappings":"AAAA,YAAYA,oBAAoB,WAAW;AAC3C,SAASC,eAAe,QAAQ,oCAAoC;AAGpE,gBAAgB;AAChBC,GAAGC,IAAI,CAAC,YAAY;IAChB,MAAMC,WAAoD,CAAC;IAC3D,MAAMC,UAAU;QACZC,QAAQJ,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;QAClCC,MAAMR,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;QAChCE,IAAIT,GAAGK,EAAE,CAAC,CAACK,OAAeC;YACtBT,QAAQ,CAACQ,MAAM,GAAGC;QACtB;QACAC,UAAU;YACNC,aAAab,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;YACvCO,OAAOd,GAAGK,EAAE,GAAGC,iBAAiB,CAAC;gBAAES,UAAU;YAAW;QAC5D;QACAC,WAAWd;QACXe,cAAc,OAAOC,QAAgBC,QAAgBC;YACjD,MAAMC,MAAM;gBACRC,SAAS;oBACLC,MAAM;wBAAEC,IAAIN;oBAAO;oBACnBO,MAAM;wBAAED,IAAIL;oBAAO;oBACnBC;oBACAM,MAAMC,KAAKC,KAAK,CAACC,KAAKC,GAAG,KAAK;gBAClC;gBACAC,OAAO/B,GAAGK,EAAE,GAAGC,iBAAiB,CAACC;YACrC;YACA,IAAIL,QAAQ,CAAC,OAAO,EAAE;gBAClB,MAAMA,QAAQ,CAAC,OAAO,CAACmB;YAC3B;YACA,OAAOA;QACX;IACJ;IACA,OAAO;QACHW,UAAUhC,GAAGK,EAAE,CAAC;YAAc,OAAOF;QAAS;QAC9C8B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CAACC,WAAW,EAAEsC,qBAAqB,CAAC;QAC3D;IACJ;IAEAhB,SAAS,aAAa;QAClBK,GAAG,kCAAkC;YACjC,MAAMJ,QAAQS,KAAK;YACnBJ,OAAO,MAAML,QAAQkD,SAAS,IAAI3C,IAAI,CAAC;QAC3C;QAEAH,GAAG,oCAAoC;YACnCC,OAAO,MAAML,QAAQkD,SAAS,IAAI3C,IAAI,CAAC;QAC3C;QAEAH,GAAG,kCAAkC;YACjC,MAAMJ,QAAQS,KAAK;YACnB,MAAMT,QAAQ5B,IAAI;YAClBiC,OAAO,MAAML,QAAQkD,SAAS,IAAI3C,IAAI,CAAC;QAC3C;IACJ;AACJ"}
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { markdownToTelegramHtml } from '../../utils/telegramHtml.js';
|
|
2
|
+
describe('markdownToTelegramHtml', ()=>{
|
|
3
|
+
it('renders bold, italic, strikethrough', ()=>{
|
|
4
|
+
const out = markdownToTelegramHtml('**b** _i_ ~~s~~');
|
|
5
|
+
expect(out).toContain('<b>b</b>');
|
|
6
|
+
expect(out).toContain('<i>i</i>');
|
|
7
|
+
expect(out).toContain('<s>s</s>');
|
|
8
|
+
});
|
|
9
|
+
it('renders inline code and fenced code with language', ()=>{
|
|
10
|
+
const md = 'Run `npm test`\n\n```ts\nconst x = 1;\n```';
|
|
11
|
+
const out = markdownToTelegramHtml(md);
|
|
12
|
+
expect(out).toContain('<code>npm test</code>');
|
|
13
|
+
expect(out).toContain('<pre><code class="language-ts">const x = 1;</code></pre>');
|
|
14
|
+
});
|
|
15
|
+
it('renders links and converts headings to bold', ()=>{
|
|
16
|
+
const md = '# Title\n\nSee [docs](https://x.com).';
|
|
17
|
+
const out = markdownToTelegramHtml(md);
|
|
18
|
+
expect(out).toContain('<b>Title</b>');
|
|
19
|
+
expect(out).toContain('<a href="https://x.com">docs</a>');
|
|
20
|
+
});
|
|
21
|
+
it('renders images as alt-text links', ()=>{
|
|
22
|
+
const out = markdownToTelegramHtml('');
|
|
23
|
+
expect(out).toBe('<a href="https://x.com/d.png">diagram</a>');
|
|
24
|
+
});
|
|
25
|
+
it('falls back to URL when image has no alt text', ()=>{
|
|
26
|
+
const out = markdownToTelegramHtml('');
|
|
27
|
+
expect(out).toContain('<a href="https://x.com/d.png">https://x.com/d.png</a>');
|
|
28
|
+
});
|
|
29
|
+
it('renders unordered lists with bullets', ()=>{
|
|
30
|
+
const out = markdownToTelegramHtml('- one\n- two\n- three');
|
|
31
|
+
expect(out).toContain('• one');
|
|
32
|
+
expect(out).toContain('• two');
|
|
33
|
+
expect(out).toContain('• three');
|
|
34
|
+
expect(out).not.toContain('<ul>');
|
|
35
|
+
});
|
|
36
|
+
it('renders ordered lists with numbers', ()=>{
|
|
37
|
+
const out = markdownToTelegramHtml('1. one\n2. two');
|
|
38
|
+
expect(out).toContain('1. one');
|
|
39
|
+
expect(out).toContain('2. two');
|
|
40
|
+
expect(out).not.toContain('<ol>');
|
|
41
|
+
});
|
|
42
|
+
it('renders tables as ASCII inside <pre>', ()=>{
|
|
43
|
+
const md = '| a | b |\n|---|---|\n| 1 | 2 |';
|
|
44
|
+
const out = markdownToTelegramHtml(md);
|
|
45
|
+
expect(out.startsWith('<pre>')).toBe(true);
|
|
46
|
+
expect(out).toContain('a');
|
|
47
|
+
expect(out).toContain('b');
|
|
48
|
+
expect(out).toContain('1');
|
|
49
|
+
expect(out).toContain('2');
|
|
50
|
+
expect(out).not.toContain('<table>');
|
|
51
|
+
});
|
|
52
|
+
it('uses <blockquote> for quotes', ()=>{
|
|
53
|
+
const out = markdownToTelegramHtml('> quoted');
|
|
54
|
+
expect(out).toContain('<blockquote>');
|
|
55
|
+
expect(out).toContain('quoted');
|
|
56
|
+
expect(out).toContain('</blockquote>');
|
|
57
|
+
});
|
|
58
|
+
it('escapes HTML special chars in plain text', ()=>{
|
|
59
|
+
const out = markdownToTelegramHtml('a < b && c > d');
|
|
60
|
+
expect(out).toContain('<');
|
|
61
|
+
expect(out).toContain('&');
|
|
62
|
+
expect(out).toContain('>');
|
|
63
|
+
expect(out).not.toContain(' < ');
|
|
64
|
+
});
|
|
65
|
+
it('escapes HTML special chars inside code', ()=>{
|
|
66
|
+
const out = markdownToTelegramHtml('`<script>`');
|
|
67
|
+
expect(out).toContain('<code><script></code>');
|
|
68
|
+
});
|
|
69
|
+
it('strips raw HTML blocks', ()=>{
|
|
70
|
+
const out = markdownToTelegramHtml('hello\n\n<div>raw</div>\n\nworld');
|
|
71
|
+
expect(out).not.toContain('<div>');
|
|
72
|
+
expect(out).toContain('hello');
|
|
73
|
+
expect(out).toContain('world');
|
|
74
|
+
});
|
|
75
|
+
it('renders horizontal rule as a divider', ()=>{
|
|
76
|
+
const out = markdownToTelegramHtml('above\n\n---\n\nbelow');
|
|
77
|
+
expect(out).toContain('above');
|
|
78
|
+
expect(out).toContain('———');
|
|
79
|
+
expect(out).toContain('below');
|
|
80
|
+
});
|
|
81
|
+
it('passes plain text through unchanged', ()=>{
|
|
82
|
+
expect(markdownToTelegramHtml('hello world')).toBe('hello world');
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
//# sourceMappingURL=telegramHtml.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/__tests__/utils/telegramHtml.test.ts"],"sourcesContent":["import { markdownToTelegramHtml } from '../../utils/telegramHtml.js';\n\ndescribe('markdownToTelegramHtml', () => {\n it('renders bold, italic, strikethrough', () => {\n const out = markdownToTelegramHtml('**b** _i_ ~~s~~');\n expect(out).toContain('<b>b</b>');\n expect(out).toContain('<i>i</i>');\n expect(out).toContain('<s>s</s>');\n });\n\n it('renders inline code and fenced code with language', () => {\n const md = 'Run `npm test`\\n\\n```ts\\nconst x = 1;\\n```';\n const out = markdownToTelegramHtml(md);\n expect(out).toContain('<code>npm test</code>');\n expect(out).toContain('<pre><code class=\"language-ts\">const x = 1;</code></pre>');\n });\n\n it('renders links and converts headings to bold', () => {\n const md = '# Title\\n\\nSee [docs](https://x.com).';\n const out = markdownToTelegramHtml(md);\n expect(out).toContain('<b>Title</b>');\n expect(out).toContain('<a href=\"https://x.com\">docs</a>');\n });\n\n it('renders images as alt-text links', () => {\n const out = markdownToTelegramHtml('');\n expect(out).toBe('<a href=\"https://x.com/d.png\">diagram</a>');\n });\n\n it('falls back to URL when image has no alt text', () => {\n const out = markdownToTelegramHtml('');\n expect(out).toContain('<a href=\"https://x.com/d.png\">https://x.com/d.png</a>');\n });\n\n it('renders unordered lists with bullets', () => {\n const out = markdownToTelegramHtml('- one\\n- two\\n- three');\n expect(out).toContain('• one');\n expect(out).toContain('• two');\n expect(out).toContain('• three');\n expect(out).not.toContain('<ul>');\n });\n\n it('renders ordered lists with numbers', () => {\n const out = markdownToTelegramHtml('1. one\\n2. two');\n expect(out).toContain('1. one');\n expect(out).toContain('2. two');\n expect(out).not.toContain('<ol>');\n });\n\n it('renders tables as ASCII inside <pre>', () => {\n const md = '| a | b |\\n|---|---|\\n| 1 | 2 |';\n const out = markdownToTelegramHtml(md);\n expect(out.startsWith('<pre>')).toBe(true);\n expect(out).toContain('a');\n expect(out).toContain('b');\n expect(out).toContain('1');\n expect(out).toContain('2');\n expect(out).not.toContain('<table>');\n });\n\n it('uses <blockquote> for quotes', () => {\n const out = markdownToTelegramHtml('> quoted');\n expect(out).toContain('<blockquote>');\n expect(out).toContain('quoted');\n expect(out).toContain('</blockquote>');\n });\n\n it('escapes HTML special chars in plain text', () => {\n const out = markdownToTelegramHtml('a < b && c > d');\n expect(out).toContain('<');\n expect(out).toContain('&');\n expect(out).toContain('>');\n expect(out).not.toContain(' < ');\n });\n\n it('escapes HTML special chars inside code', () => {\n const out = markdownToTelegramHtml('`<script>`');\n expect(out).toContain('<code><script></code>');\n });\n\n it('strips raw HTML blocks', () => {\n const out = markdownToTelegramHtml('hello\\n\\n<div>raw</div>\\n\\nworld');\n expect(out).not.toContain('<div>');\n expect(out).toContain('hello');\n expect(out).toContain('world');\n });\n\n it('renders horizontal rule as a divider', () => {\n const out = markdownToTelegramHtml('above\\n\\n---\\n\\nbelow');\n expect(out).toContain('above');\n expect(out).toContain('———');\n expect(out).toContain('below');\n });\n\n it('passes plain text through unchanged', () => {\n expect(markdownToTelegramHtml('hello world')).toBe('hello world');\n });\n});\n"],"names":["markdownToTelegramHtml","describe","it","out","expect","toContain","md","toBe","not","startsWith"],"mappings":"AAAA,SAASA,sBAAsB,QAAQ,8BAA8B;AAErEC,SAAS,0BAA0B;IAC/BC,GAAG,uCAAuC;QACtC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,qDAAqD;QACpD,MAAMI,KAAK;QACX,MAAMH,MAAMH,uBAAuBM;QACnCF,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,+CAA+C;QAC9C,MAAMI,KAAK;QACX,MAAMH,MAAMH,uBAAuBM;QACnCF,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,oCAAoC;QACnC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKI,IAAI,CAAC;IACrB;IAEAL,GAAG,gDAAgD;QAC/C,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;IAC1B;IAEAH,GAAG,wCAAwC;QACvC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAAKK,GAAG,CAACH,SAAS,CAAC;IAC9B;IAEAH,GAAG,sCAAsC;QACrC,MAAMC,MAAMH,uBAAuB;QACnCI,OAAOD,KAAKE,SAAS,CAAC;QACtBD,OAAOD,KAA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C;IACvD;AACJ"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/ChannelAdapter.ts"],"sourcesContent":["import type { IncomingMessage } from '../types.js';\n\n/**\n * Interface for messaging platform adapters.\n *\n * Implementations connect to a specific platform (Telegram, Slack, etc.)\n * and provide a generic send/receive abstraction.\n */\nexport interface ChannelAdapter {\n /** Identifier for this channel type (e.g., 'telegram') */\n readonly type: string;\n\n /** Start listening for incoming messages */\n start(): Promise<void>;\n\n /** Stop listening and clean up resources */\n stop(): Promise<void>;\n\n /**\n * Send a message to a specific chat.\n * Implementations should handle platform-specific limits\n * (e.g., chunking at 4096 chars for Telegram).\n */\n sendMessage(chatId: string, text: string): Promise<void>;\n\n /**\n * Register a handler for incoming text messages.\n * Fire-and-forget — handler returns void.\n * Responses are sent separately via sendMessage().\n */\n onMessage(handler: (msg: IncomingMessage) => Promise<void>): void;\n\n /** Check if the adapter is connected and healthy */\n isHealthy(): Promise<boolean>;\n}\n"],"names":[],"mappings":"AAEA;;;;;CAKC,GACD,WA0BC"}
|
|
@@ -5,30 +5,27 @@ export const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;
|
|
|
5
5
|
const TELEGRAM_PARSE_MODE = 'HTML';
|
|
6
6
|
/**
|
|
7
7
|
* Telegram Bot API adapter using telegraf with long polling.
|
|
8
|
-
*/
|
|
9
|
-
export class TelegramAdapter {
|
|
8
|
+
*/ export class TelegramAdapter {
|
|
10
9
|
type = TELEGRAM_CHANNEL_TYPE;
|
|
11
10
|
bot;
|
|
12
11
|
messageHandler = null;
|
|
13
12
|
running = false;
|
|
14
|
-
constructor(options)
|
|
13
|
+
constructor(options){
|
|
15
14
|
this.bot = new Telegraf(options.botToken);
|
|
16
15
|
}
|
|
17
16
|
async start() {
|
|
18
|
-
this.bot.on('text', async (ctx)
|
|
19
|
-
if (!this.messageHandler)
|
|
20
|
-
return;
|
|
17
|
+
this.bot.on('text', async (ctx)=>{
|
|
18
|
+
if (!this.messageHandler) return;
|
|
21
19
|
const msg = {
|
|
22
20
|
channelType: TELEGRAM_CHANNEL_TYPE,
|
|
23
21
|
chatId: String(ctx.message.chat.id),
|
|
24
22
|
userId: String(ctx.message.from.id),
|
|
25
23
|
text: ctx.message.text,
|
|
26
|
-
timestamp: new Date(ctx.message.date * 1000)
|
|
24
|
+
timestamp: new Date(ctx.message.date * 1000)
|
|
27
25
|
};
|
|
28
26
|
try {
|
|
29
27
|
await this.messageHandler(msg);
|
|
30
|
-
}
|
|
31
|
-
catch (error) {
|
|
28
|
+
} catch (error) {
|
|
32
29
|
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
33
30
|
await ctx.reply(`Error processing message: ${errorMessage}`);
|
|
34
31
|
}
|
|
@@ -45,17 +42,16 @@ export class TelegramAdapter {
|
|
|
45
42
|
* Long messages are chunked at paragraph boundaries when possible; very
|
|
46
43
|
* long single blocks (e.g. a `<pre>` over 4096 chars) may still split
|
|
47
44
|
* mid-tag and produce a partial render in the second chunk.
|
|
48
|
-
*/
|
|
49
|
-
async sendMessage(chatId, text) {
|
|
45
|
+
*/ async sendMessage(chatId, text) {
|
|
50
46
|
const html = markdownToTelegramHtml(text);
|
|
51
47
|
const chunks = chunkMessage(html, TELEGRAM_MAX_MESSAGE_LENGTH);
|
|
52
|
-
for (const chunk of chunks)
|
|
48
|
+
for (const chunk of chunks){
|
|
53
49
|
try {
|
|
54
|
-
await this.bot.telegram.sendMessage(chatId, chunk, {
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
50
|
+
await this.bot.telegram.sendMessage(chatId, chunk, {
|
|
51
|
+
parse_mode: TELEGRAM_PARSE_MODE
|
|
52
|
+
});
|
|
53
|
+
} catch (error) {
|
|
54
|
+
if (!isParseEntitiesError(error)) throw error;
|
|
59
55
|
// Telegram rejected the rendered HTML — fall back to plain text
|
|
60
56
|
// so the user still gets the content (just unformatted).
|
|
61
57
|
await this.bot.telegram.sendMessage(chatId, htmlToPlainText(chunk));
|
|
@@ -70,28 +66,21 @@ export class TelegramAdapter {
|
|
|
70
66
|
}
|
|
71
67
|
}
|
|
72
68
|
function isParseEntitiesError(error) {
|
|
73
|
-
if (!error || typeof error !== 'object')
|
|
74
|
-
return false;
|
|
69
|
+
if (!error || typeof error !== 'object') return false;
|
|
75
70
|
const description = error.description;
|
|
76
71
|
const message = error.message;
|
|
77
72
|
return ((description ?? '') + (message ?? '')).includes("can't parse entities");
|
|
78
73
|
}
|
|
79
74
|
function htmlToPlainText(html) {
|
|
80
|
-
return html
|
|
81
|
-
.replace(/<[^>]+>/g, '')
|
|
82
|
-
.replace(/</g, '<')
|
|
83
|
-
.replace(/>/g, '>')
|
|
84
|
-
.replace(/"/g, '"')
|
|
85
|
-
.replace(/&/g, '&');
|
|
75
|
+
return html.replace(/<[^>]+>/g, '').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/&/g, '&');
|
|
86
76
|
}
|
|
87
77
|
/**
|
|
88
78
|
* Split text into chunks of maxLen or fewer characters. Prefers paragraph
|
|
89
79
|
* boundaries (\n\n), then single newlines (\n), then hard-splits at maxLen.
|
|
90
|
-
*/
|
|
91
|
-
function chunkMessage(text, maxLen) {
|
|
80
|
+
*/ function chunkMessage(text, maxLen) {
|
|
92
81
|
const chunks = [];
|
|
93
82
|
let remaining = text;
|
|
94
|
-
while
|
|
83
|
+
while(remaining.length > 0){
|
|
95
84
|
if (remaining.length <= maxLen) {
|
|
96
85
|
chunks.push(remaining);
|
|
97
86
|
break;
|
|
@@ -101,11 +90,9 @@ function chunkMessage(text, maxLen) {
|
|
|
101
90
|
let splitAt;
|
|
102
91
|
if (lastParagraph > 0) {
|
|
103
92
|
splitAt = lastParagraph + 2;
|
|
104
|
-
}
|
|
105
|
-
else if (lastNewline > 0) {
|
|
93
|
+
} else if (lastNewline > 0) {
|
|
106
94
|
splitAt = lastNewline + 1;
|
|
107
|
-
}
|
|
108
|
-
else {
|
|
95
|
+
} else {
|
|
109
96
|
splitAt = maxLen;
|
|
110
97
|
}
|
|
111
98
|
chunks.push(remaining.slice(0, splitAt));
|
|
@@ -113,4 +100,5 @@ function chunkMessage(text, maxLen) {
|
|
|
113
100
|
}
|
|
114
101
|
return chunks;
|
|
115
102
|
}
|
|
103
|
+
|
|
116
104
|
//# sourceMappingURL=TelegramAdapter.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/adapters/TelegramAdapter.ts"],"sourcesContent":["import { Telegraf } from 'telegraf';\nimport type { ChannelAdapter } from './ChannelAdapter.js';\nimport { markdownToTelegramHtml } from '../utils/telegramHtml.js';\nimport type { IncomingMessage } from '../types.js';\n\nexport const TELEGRAM_CHANNEL_TYPE = 'telegram';\nexport const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;\nconst TELEGRAM_PARSE_MODE = 'HTML' as const;\n\nexport interface TelegramAdapterOptions {\n botToken: string;\n}\n\n/**\n * Telegram Bot API adapter using telegraf with long polling.\n */\nexport class TelegramAdapter implements ChannelAdapter {\n readonly type = TELEGRAM_CHANNEL_TYPE;\n\n private bot: Telegraf;\n private messageHandler: ((msg: IncomingMessage) => Promise<void>) | null = null;\n private running = false;\n\n constructor(options: TelegramAdapterOptions) {\n this.bot = new Telegraf(options.botToken);\n }\n\n async start(): Promise<void> {\n this.bot.on('text', async (ctx) => {\n if (!this.messageHandler) return;\n\n const msg: IncomingMessage = {\n channelType: TELEGRAM_CHANNEL_TYPE,\n chatId: String(ctx.message.chat.id),\n userId: String(ctx.message.from.id),\n text: ctx.message.text,\n timestamp: new Date(ctx.message.date * 1000),\n };\n\n try {\n await this.messageHandler(msg);\n } catch (error) {\n const errorMessage = error instanceof Error ? error.message : 'Unknown error';\n await ctx.reply(`Error processing message: ${errorMessage}`);\n }\n });\n\n await this.bot.launch();\n this.running = true;\n }\n\n async stop(): Promise<void> {\n this.running = false;\n await this.bot.stop();\n }\n\n /**\n * Input is treated as markdown and rendered as Telegram-compatible HTML.\n * Long messages are chunked at paragraph boundaries when possible; very\n * long single blocks (e.g. a `<pre>` over 4096 chars) may still split\n * mid-tag and produce a partial render in the second chunk.\n */\n async sendMessage(chatId: string, text: string): Promise<void> {\n const html = markdownToTelegramHtml(text);\n const chunks = chunkMessage(html, TELEGRAM_MAX_MESSAGE_LENGTH);\n for (const chunk of chunks) {\n try {\n await this.bot.telegram.sendMessage(chatId, chunk, { parse_mode: TELEGRAM_PARSE_MODE });\n } catch (error) {\n if (!isParseEntitiesError(error)) throw error;\n // Telegram rejected the rendered HTML — fall back to plain text\n // so the user still gets the content (just unformatted).\n await this.bot.telegram.sendMessage(chatId, htmlToPlainText(chunk));\n }\n }\n }\n\n onMessage(handler: (msg: IncomingMessage) => Promise<void>): void {\n this.messageHandler = handler;\n }\n\n async isHealthy(): Promise<boolean> {\n return this.running;\n }\n}\n\nfunction isParseEntitiesError(error: unknown): boolean {\n if (!error || typeof error !== 'object') return false;\n const description = (error as { description?: string }).description;\n const message = (error as { message?: string }).message;\n return ((description ?? '') + (message ?? '')).includes(\"can't parse entities\");\n}\n\nfunction htmlToPlainText(html: string): string {\n return html\n .replace(/<[^>]+>/g, '')\n .replace(/</g, '<')\n .replace(/>/g, '>')\n .replace(/"/g, '\"')\n .replace(/&/g, '&');\n}\n\n/**\n * Split text into chunks of maxLen or fewer characters. Prefers paragraph\n * boundaries (\\n\\n), then single newlines (\\n), then hard-splits at maxLen.\n */\nfunction chunkMessage(text: string, maxLen: number): string[] {\n const chunks: string[] = [];\n let remaining = text;\n\n while (remaining.length > 0) {\n if (remaining.length <= maxLen) {\n chunks.push(remaining);\n break;\n }\n\n const lastParagraph = remaining.lastIndexOf('\\n\\n', maxLen - 2);\n const lastNewline = remaining.lastIndexOf('\\n', maxLen - 1);\n\n let splitAt: number;\n if (lastParagraph > 0) {\n splitAt = lastParagraph + 2;\n } else if (lastNewline > 0) {\n splitAt = lastNewline + 1;\n } else {\n splitAt = maxLen;\n }\n\n chunks.push(remaining.slice(0, splitAt));\n remaining = remaining.slice(splitAt);\n }\n\n return chunks;\n}\n"],"names":["Telegraf","markdownToTelegramHtml","TELEGRAM_CHANNEL_TYPE","TELEGRAM_MAX_MESSAGE_LENGTH","TELEGRAM_PARSE_MODE","TelegramAdapter","type","bot","messageHandler","running","options","botToken","start","on","ctx","msg","channelType","chatId","String","message","chat","id","userId","from","text","timestamp","Date","date","error","errorMessage","Error","reply","launch","stop","sendMessage","html","chunks","chunkMessage","chunk","telegram","parse_mode","isParseEntitiesError","htmlToPlainText","onMessage","handler","isHealthy","description","includes","replace","maxLen","remaining","length","push","lastParagraph","lastIndexOf","lastNewline","splitAt","slice"],"mappings":"AAAA,SAASA,QAAQ,QAAQ,WAAW;AAEpC,SAASC,sBAAsB,QAAQ,2BAA2B;AAGlE,OAAO,MAAMC,wBAAwB,WAAW;AAChD,OAAO,MAAMC,8BAA8B,KAAK;AAChD,MAAMC,sBAAsB;AAM5B;;CAEC,GACD,OAAO,MAAMC;IACAC,OAAOJ,sBAAsB;IAE9BK,IAAc;IACdC,iBAAmE,KAAK;IACxEC,UAAU,MAAM;IAExB,YAAYC,OAA+B,CAAE;QACzC,IAAI,CAACH,GAAG,GAAG,IAAIP,SAASU,QAAQC,QAAQ;IAC5C;IAEA,MAAMC,QAAuB;QACzB,IAAI,CAACL,GAAG,CAACM,EAAE,CAAC,QAAQ,OAAOC;YACvB,IAAI,CAAC,IAAI,CAACN,cAAc,EAAE;YAE1B,MAAMO,MAAuB;gBACzBC,aAAad;gBACbe,QAAQC,OAAOJ,IAAIK,OAAO,CAACC,IAAI,CAACC,EAAE;gBAClCC,QAAQJ,OAAOJ,IAAIK,OAAO,CAACI,IAAI,CAACF,EAAE;gBAClCG,MAAMV,IAAIK,OAAO,CAACK,IAAI;gBACtBC,WAAW,IAAIC,KAAKZ,IAAIK,OAAO,CAACQ,IAAI,GAAG;YAC3C;YAEA,IAAI;gBACA,MAAM,IAAI,CAACnB,cAAc,CAACO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CAAC;AAC5D;AAEA,SAASL,gBAAgBP,IAAY;IACjC,OAAOA,KACFa,OAAO,CAAC,YAAY,IACpBA,OAAO,CAAC,SAAS,KACjBA,OAAO,CAAC,SAAS,KACjBA,OAAO,CAAC,WAAW,KACnBA,OAAO,CAAC,UAAU;AAC3B;AAEA;;;CAGC,GACD,SAASX,aAAab,IAAY,EAAEyB,MAAc;IAC9C,MAAMb,SAAmB,EAAE;IAC3B,IAAIc,YAAY1B;IAEhB,MAAO0B,UAAUC,MAAM,GAAG,EAAG;QACzB,IAAID,UAAUC,MAAM,IAAIF,QAAQ;YAC5Bb,OAAOgB,IAAI,CAACF;YACZ;QACJ;QAEA,MAAMG,gBAAgBH,UAAUI,WAAW,CAAC,QAAQL,SAAS;QAC7D,MAAMM,cAAcL,UAAUI,WAAW,CAAC,MAAML,SAAS;QAEzD,IAAIO;QACJ,IAAIH,gBAAgB,GAAG;YACnBG,UAAUH,gBAAgB;QAC9B,OAAO,IAAIE,cAAc,GAAG;YACxBC,UAAUD,cAAc;QAC5B,OAAO;YACHC,UAAUP;QACd;QAEAb,OAAOgB,IAAI,CAACF,UAAUO,KAAK,CAAC,GAAGD;QAC/BN,YAAYA,UAAUO,KAAK,CAACD;IAChC;IAEA,OAAOpB;AACX"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export { ChannelManager } from './ChannelManager.js';\nexport { ConfigStore } from './ConfigStore.js';\nexport { TelegramAdapter, TELEGRAM_CHANNEL_TYPE, TELEGRAM_MAX_MESSAGE_LENGTH } from './adapters/TelegramAdapter.js';\nexport type { TelegramAdapterOptions } from './adapters/TelegramAdapter.js';\n\nexport type { ChannelAdapter } from './adapters/ChannelAdapter.js';\n\nexport type {\n IncomingMessage,\n MessageHandler,\n ChannelConfig,\n ChannelEntry,\n ChannelType,\n TelegramConfig,\n} from './types.js';\n"],"names":["ChannelManager","ConfigStore","TelegramAdapter","TELEGRAM_CHANNEL_TYPE","TELEGRAM_MAX_MESSAGE_LENGTH"],"mappings":"AAAA,SAASA,cAAc,QAAQ,sBAAsB;AACrD,SAASC,WAAW,QAAQ,mBAAmB;AAC/C,SAASC,eAAe,EAAEC,qBAAqB,EAAEC,2BAA2B,QAAQ,gCAAgC"}
|
package/dist/types.js
CHANGED
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/**\n * An incoming message from a messaging platform.\n * Generic — no agent-specific concepts.\n */\nexport interface IncomingMessage {\n channelType: string;\n chatId: string;\n userId: string;\n text: string;\n timestamp: Date;\n metadata?: Record<string, unknown>;\n}\n\n/**\n * Handler function provided by the consumer (e.g., CLI).\n * Fire-and-forget — returns void. Responses are sent separately via sendMessage().\n */\nexport type MessageHandler = (message: IncomingMessage) => Promise<void>;\n\n/**\n * Root configuration for all channels.\n */\nexport interface ChannelConfig {\n channels: Record<string, ChannelEntry>;\n}\n\n/**\n * Configuration entry for a single channel.\n */\nexport interface ChannelEntry {\n type: ChannelType;\n enabled: boolean;\n createdAt: string;\n config: TelegramConfig;\n}\n\n/**\n * Supported channel types.\n */\nexport type ChannelType = 'telegram' | 'slack' | 'whatsapp';\n\n/**\n * Telegram-specific configuration.\n */\nexport interface TelegramConfig {\n botToken: string;\n botUsername: string;\n authorizedChatId?: number;\n}\n"],"names":[],"mappings":"AAAA;;;CAGC,GAsCD;;CAEC,GACD,WAIC"}
|