-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathTelegramAdapter.ts
More file actions
152 lines (124 loc) · 4.62 KB
/
TelegramAdapter.ts
File metadata and controls
152 lines (124 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { Telegraf } from 'telegraf';
import type { ChannelAdapter } from './ChannelAdapter';
import type { IncomingMessage } from '../types';
export const TELEGRAM_CHANNEL_TYPE = 'telegram';
export const TELEGRAM_MAX_MESSAGE_LENGTH = 4096;
export interface TelegramAdapterOptions {
botToken: string;
}
/**
* Telegram Bot API adapter using telegraf with long polling.
*/
export class TelegramAdapter implements ChannelAdapter {
readonly type = TELEGRAM_CHANNEL_TYPE;
private bot: Telegraf;
private messageHandler: ((msg: IncomingMessage) => Promise<void>) | null = null;
private running = false;
constructor(options: TelegramAdapterOptions) {
this.bot = new Telegraf(options.botToken);
}
async start(): Promise<void> {
this.bot.on('text', async (ctx) => {
if (!this.messageHandler) return;
const msg: IncomingMessage = {
channelType: TELEGRAM_CHANNEL_TYPE,
chatId: String(ctx.message.chat.id),
userId: String(ctx.message.from.id),
text: ctx.message.text,
timestamp: new Date(ctx.message.date * 1000),
};
try {
await this.messageHandler(msg);
} catch (error) {
const errorMessage = error instanceof Error ? error.message : 'Unknown error';
await ctx.reply(`Error processing message: ${errorMessage}`);
}
});
await this.bot.launch();
this.running = true;
}
async stop(): Promise<void> {
this.running = false;
await this.bot.stop();
}
/**
* Send a message to a chat. Automatically chunks messages exceeding
* Telegram's 4096-char limit, preferring newline boundaries.
*/
async sendMessage(chatId: string, text: string): Promise<void> {
const chunks = chunkMessage(text, TELEGRAM_MAX_MESSAGE_LENGTH);
for (const chunk of chunks) {
const html = markdownToHtml(chunk);
await this.bot.telegram.sendMessage(chatId, html, { parse_mode: 'HTML' });
}
}
onMessage(handler: (msg: IncomingMessage) => Promise<void>): void {
this.messageHandler = handler;
}
async isHealthy(): Promise<boolean> {
return this.running;
}
}
/**
* Split text into chunks of maxLen or fewer characters,
* preferring to split at newline boundaries.
*/
function chunkMessage(text: string, maxLen: number): string[] {
if (text.length <= maxLen) {
return [text];
}
const chunks: string[] = [];
let remaining = text;
while (remaining.length > 0) {
if (remaining.length <= maxLen) {
chunks.push(remaining);
break;
}
// Find the last newline within the limit
const searchArea = remaining.slice(0, maxLen);
const lastNewline = searchArea.lastIndexOf('\n');
let splitAt: number;
if (lastNewline > 0) {
splitAt = lastNewline + 1; // include the newline in the current chunk
} else {
// No newline found — hard split at maxLen
splitAt = maxLen;
}
chunks.push(remaining.slice(0, splitAt));
remaining = remaining.slice(splitAt);
}
return chunks;
}
function escapeHtml(text: string): string {
return text.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
/**
* Convert standard Markdown to Telegram HTML.
* Handles code blocks first to prevent formatting inside them.
*/
function markdownToHtml(text: string): string {
const codeBlocks: string[] = [];
const inlineCodes: string[] = [];
let result = text.replace(/```(\w*)\n?([\s\S]*?)```/g, (_, lang, code) => {
const escaped = escapeHtml(code.trimEnd());
const block = lang
? `<pre><code class="language-${lang}">${escaped}</code></pre>`
: `<pre><code>${escaped}</code></pre>`;
codeBlocks.push(block);
return `\x00CODE${codeBlocks.length - 1}\x00`;
});
result = result.replace(/`([^`]+)`/g, (_, code) => {
inlineCodes.push(`<code>${escapeHtml(code)}</code>`);
return `\x00INLINE${inlineCodes.length - 1}\x00`;
});
result = escapeHtml(result);
result = result
.replace(/\*\*(.+?)\*\*/g, '<b>$1</b>')
.replace(/__(.+?)__/g, '<b>$1</b>')
.replace(/\*(.+?)\*/g, '<i>$1</i>')
.replace(/_(.+?)_/g, '<i>$1</i>')
.replace(/~~(.+?)~~/g, '<s>$1</s>');
result = result.replace(/\x00CODE(\d+)\x00/g, (_, i) => codeBlocks[parseInt(i)]);
result = result.replace(/\x00INLINE(\d+)\x00/g, (_, i) => inlineCodes[parseInt(i)]);
return result;
}