@leitwerk-dev/telegram 0.1.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/README.md +145 -0
- package/dist/LICENSE +13 -0
- package/dist/actions.d.ts +9 -0
- package/dist/actions.d.ts.map +1 -0
- package/dist/actions.js +32 -0
- package/dist/actions.js.map +1 -0
- package/dist/bridge.d.ts +132 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +2032 -0
- package/dist/bridge.js.map +1 -0
- package/dist/config.d.ts +25 -0
- package/dist/config.d.ts.map +1 -0
- package/dist/config.js +70 -0
- package/dist/config.js.map +1 -0
- package/dist/fake-telegram-client.d.ts +82 -0
- package/dist/fake-telegram-client.d.ts.map +1 -0
- package/dist/fake-telegram-client.js +107 -0
- package/dist/fake-telegram-client.js.map +1 -0
- package/dist/form-session.d.ts +143 -0
- package/dist/form-session.d.ts.map +1 -0
- package/dist/form-session.js +218 -0
- package/dist/form-session.js.map +1 -0
- package/dist/grammy-telegram-client.d.ts +131 -0
- package/dist/grammy-telegram-client.d.ts.map +1 -0
- package/dist/grammy-telegram-client.js +151 -0
- package/dist/grammy-telegram-client.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +55 -0
- package/dist/index.js.map +1 -0
- package/dist/launch-session.d.ts +103 -0
- package/dist/launch-session.d.ts.map +1 -0
- package/dist/launch-session.js +435 -0
- package/dist/launch-session.js.map +1 -0
- package/dist/markdown-html.d.ts +29 -0
- package/dist/markdown-html.d.ts.map +1 -0
- package/dist/markdown-html.js +259 -0
- package/dist/markdown-html.js.map +1 -0
- package/dist/mermaid-image.d.ts +2 -0
- package/dist/mermaid-image.d.ts.map +1 -0
- package/dist/mermaid-image.js +99 -0
- package/dist/mermaid-image.js.map +1 -0
- package/dist/process-feed.d.ts +58 -0
- package/dist/process-feed.d.ts.map +1 -0
- package/dist/process-feed.js +165 -0
- package/dist/process-feed.js.map +1 -0
- package/dist/process-thread-store.d.ts +18 -0
- package/dist/process-thread-store.d.ts.map +1 -0
- package/dist/process-thread-store.js +74 -0
- package/dist/process-thread-store.js.map +1 -0
- package/dist/topic-title.d.ts +7 -0
- package/dist/topic-title.d.ts.map +1 -0
- package/dist/topic-title.js +29 -0
- package/dist/topic-title.js.map +1 -0
- package/dist/types.d.ts +118 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +19 -0
- package/dist/types.js.map +1 -0
- package/package.json +57 -0
- package/src/actions.ts +41 -0
- package/src/bridge.ts +2870 -0
- package/src/config.ts +108 -0
- package/src/fake-telegram-client.ts +167 -0
- package/src/form-session.ts +359 -0
- package/src/grammy-telegram-client.ts +292 -0
- package/src/index.ts +68 -0
- package/src/launch-session.ts +620 -0
- package/src/markdown-html.ts +297 -0
- package/src/mermaid-image.ts +99 -0
- package/src/process-feed.ts +237 -0
- package/src/process-thread-store.ts +98 -0
- package/src/topic-title.ts +35 -0
- package/src/types.ts +138 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import type { ServerExtensionLogger } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
import { Bot, InputFile } from "grammy";
|
|
3
|
+
import {
|
|
4
|
+
BaseTelegramClientEventRegistrar,
|
|
5
|
+
type TelegramCallbackUpdate,
|
|
6
|
+
type TelegramClient,
|
|
7
|
+
type TelegramForumTopicClosedUpdate,
|
|
8
|
+
type TelegramForumTopicCreatedUpdate,
|
|
9
|
+
type TelegramReplyMarkup,
|
|
10
|
+
type TelegramSendFileInput,
|
|
11
|
+
type TelegramSendMessageInput,
|
|
12
|
+
type TelegramSentMessage,
|
|
13
|
+
type TelegramTextUpdate,
|
|
14
|
+
type TelegramUserRef,
|
|
15
|
+
} from "./types.js";
|
|
16
|
+
|
|
17
|
+
interface GrammyBotLike {
|
|
18
|
+
api: {
|
|
19
|
+
createForumTopic(chatId: string, name: string): Promise<{ message_thread_id: number }>;
|
|
20
|
+
editForumTopic(
|
|
21
|
+
chatId: string,
|
|
22
|
+
messageThreadId: number,
|
|
23
|
+
options: { name: string },
|
|
24
|
+
): Promise<unknown>;
|
|
25
|
+
closeForumTopic(chatId: string, messageThreadId: number): Promise<unknown>;
|
|
26
|
+
sendMessage(
|
|
27
|
+
chatId: string,
|
|
28
|
+
text: string,
|
|
29
|
+
options?: Record<string, unknown>,
|
|
30
|
+
): Promise<{ message_id: number }>;
|
|
31
|
+
sendPhoto(
|
|
32
|
+
chatId: string,
|
|
33
|
+
photo: InputFile,
|
|
34
|
+
options?: Record<string, unknown>,
|
|
35
|
+
): Promise<{ message_id: number }>;
|
|
36
|
+
sendDocument(
|
|
37
|
+
chatId: string,
|
|
38
|
+
document: InputFile,
|
|
39
|
+
options?: Record<string, unknown>,
|
|
40
|
+
): Promise<{ message_id: number }>;
|
|
41
|
+
answerCallbackQuery(id: string, options?: Record<string, unknown>): Promise<unknown>;
|
|
42
|
+
};
|
|
43
|
+
on(filter: "message:text", handler: (ctx: GrammyTextContext) => void | Promise<void>): void;
|
|
44
|
+
on(
|
|
45
|
+
filter: "message:forum_topic_created",
|
|
46
|
+
handler: (ctx: GrammyForumTopicCreatedContext) => void | Promise<void>,
|
|
47
|
+
): void;
|
|
48
|
+
on(
|
|
49
|
+
filter: "message:forum_topic_closed",
|
|
50
|
+
handler: (ctx: GrammyForumTopicClosedContext) => void | Promise<void>,
|
|
51
|
+
): void;
|
|
52
|
+
on(
|
|
53
|
+
filter: "callback_query:data",
|
|
54
|
+
handler: (ctx: GrammyCallbackContext) => void | Promise<void>,
|
|
55
|
+
): void;
|
|
56
|
+
start(options?: Record<string, unknown>): Promise<void>;
|
|
57
|
+
stop(): Promise<void>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
interface GrammyTextContext {
|
|
61
|
+
message: {
|
|
62
|
+
message_id: number;
|
|
63
|
+
message_thread_id?: number;
|
|
64
|
+
chat: { id: number | string };
|
|
65
|
+
from?: { id: number; username?: string };
|
|
66
|
+
text: string;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
interface GrammyForumTopicCreatedContext {
|
|
71
|
+
message: {
|
|
72
|
+
message_id: number;
|
|
73
|
+
message_thread_id?: number;
|
|
74
|
+
chat: { id: number | string };
|
|
75
|
+
from?: { id: number; username?: string };
|
|
76
|
+
forum_topic_created: unknown;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
interface GrammyForumTopicClosedContext {
|
|
81
|
+
message: {
|
|
82
|
+
message_id: number;
|
|
83
|
+
message_thread_id?: number;
|
|
84
|
+
chat: { id: number | string };
|
|
85
|
+
from?: { id: number; username?: string };
|
|
86
|
+
forum_topic_closed: unknown;
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
interface GrammyCallbackContext {
|
|
91
|
+
callbackQuery: {
|
|
92
|
+
id: string;
|
|
93
|
+
from?: { id: number; username?: string };
|
|
94
|
+
data: string;
|
|
95
|
+
message?: {
|
|
96
|
+
message_thread_id?: number;
|
|
97
|
+
chat: { id: number | string };
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function toTelegramReplyMarkup(markup: TelegramReplyMarkup | undefined) {
|
|
103
|
+
if (!markup) return undefined;
|
|
104
|
+
if (markup.inlineKeyboard) {
|
|
105
|
+
return {
|
|
106
|
+
inline_keyboard: markup.inlineKeyboard.map((row) =>
|
|
107
|
+
row.map((button) => ({ text: button.text, callback_data: button.callbackData })),
|
|
108
|
+
),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
if (markup.forceReply) {
|
|
112
|
+
return {
|
|
113
|
+
force_reply: true,
|
|
114
|
+
...(markup.inputFieldPlaceholder
|
|
115
|
+
? { input_field_placeholder: markup.inputFieldPlaceholder }
|
|
116
|
+
: {}),
|
|
117
|
+
...(markup.selective !== undefined ? { selective: markup.selective } : {}),
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
function toUserRef(value: { id: number; username?: string } | undefined): TelegramUserRef | null {
|
|
124
|
+
return value ? { id: value.id, ...(value.username ? { username: value.username } : {}) } : null;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export class GrammyTelegramClient
|
|
128
|
+
extends BaseTelegramClientEventRegistrar
|
|
129
|
+
implements TelegramClient
|
|
130
|
+
{
|
|
131
|
+
private readonly bot: GrammyBotLike;
|
|
132
|
+
private startPromise: Promise<void> | null = null;
|
|
133
|
+
|
|
134
|
+
constructor(
|
|
135
|
+
private readonly input: {
|
|
136
|
+
botToken: string;
|
|
137
|
+
botFactory?: (token: string) => GrammyBotLike;
|
|
138
|
+
logger?: ServerExtensionLogger;
|
|
139
|
+
},
|
|
140
|
+
) {
|
|
141
|
+
super();
|
|
142
|
+
this.bot =
|
|
143
|
+
input.botFactory?.(input.botToken) ?? (new Bot(input.botToken) as unknown as GrammyBotLike);
|
|
144
|
+
this.bot.on("message:text", (ctx) => this.dispatchText(ctx));
|
|
145
|
+
this.bot.on("message:forum_topic_created", (ctx) => this.dispatchForumTopicCreated(ctx));
|
|
146
|
+
this.bot.on("message:forum_topic_closed", (ctx) => this.dispatchForumTopicClosed(ctx));
|
|
147
|
+
this.bot.on("callback_query:data", (ctx) => this.dispatchCallback(ctx));
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async start(): Promise<void> {
|
|
151
|
+
if (this.startPromise) return;
|
|
152
|
+
const started = this.bot.start({
|
|
153
|
+
allowed_updates: ["message", "callback_query"],
|
|
154
|
+
drop_pending_updates: true,
|
|
155
|
+
});
|
|
156
|
+
this.startPromise = started.catch((error) => this.logWarn(error, "Telegram polling failed"));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async stop(): Promise<void> {
|
|
160
|
+
const started = this.startPromise;
|
|
161
|
+
if (!started) return;
|
|
162
|
+
this.startPromise = null;
|
|
163
|
+
await this.bot.stop();
|
|
164
|
+
await started;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async createForumTopic(input: {
|
|
168
|
+
chatId: string;
|
|
169
|
+
name: string;
|
|
170
|
+
}): Promise<{ messageThreadId: number }> {
|
|
171
|
+
const result = await this.bot.api.createForumTopic(input.chatId, input.name);
|
|
172
|
+
return { messageThreadId: result.message_thread_id };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async editForumTopic(input: {
|
|
176
|
+
chatId: string;
|
|
177
|
+
messageThreadId: number;
|
|
178
|
+
name: string;
|
|
179
|
+
}): Promise<void> {
|
|
180
|
+
await this.bot.api.editForumTopic(input.chatId, input.messageThreadId, { name: input.name });
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async closeForumTopic(input: { chatId: string; messageThreadId: number }): Promise<void> {
|
|
184
|
+
await this.bot.api.closeForumTopic(input.chatId, input.messageThreadId);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async sendMessage(input: TelegramSendMessageInput): Promise<TelegramSentMessage> {
|
|
188
|
+
const result = await this.bot.api.sendMessage(input.chatId, input.text, {
|
|
189
|
+
...(input.messageThreadId !== undefined ? { message_thread_id: input.messageThreadId } : {}),
|
|
190
|
+
...(input.parseMode ? { parse_mode: input.parseMode } : {}),
|
|
191
|
+
...(input.replyMarkup ? { reply_markup: toTelegramReplyMarkup(input.replyMarkup) } : {}),
|
|
192
|
+
});
|
|
193
|
+
return { messageId: result.message_id };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async sendPhoto(input: TelegramSendFileInput): Promise<TelegramSentMessage> {
|
|
197
|
+
const result = await this.bot.api.sendPhoto(
|
|
198
|
+
input.chatId,
|
|
199
|
+
new InputFile(input.bytes, input.filename),
|
|
200
|
+
this.fileOptions(input),
|
|
201
|
+
);
|
|
202
|
+
return { messageId: result.message_id };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
async sendDocument(input: TelegramSendFileInput): Promise<TelegramSentMessage> {
|
|
206
|
+
const result = await this.bot.api.sendDocument(
|
|
207
|
+
input.chatId,
|
|
208
|
+
new InputFile(input.bytes, input.filename),
|
|
209
|
+
this.fileOptions(input),
|
|
210
|
+
);
|
|
211
|
+
return { messageId: result.message_id };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
private fileOptions(input: TelegramSendFileInput): Record<string, unknown> {
|
|
215
|
+
return {
|
|
216
|
+
...(input.messageThreadId !== undefined ? { message_thread_id: input.messageThreadId } : {}),
|
|
217
|
+
...(input.caption ? { caption: input.caption } : {}),
|
|
218
|
+
...(input.parseMode ? { parse_mode: input.parseMode } : {}),
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
async answerCallbackQuery(input: {
|
|
223
|
+
callbackQueryId: string;
|
|
224
|
+
text?: string;
|
|
225
|
+
showAlert?: boolean;
|
|
226
|
+
}): Promise<void> {
|
|
227
|
+
await this.bot.api.answerCallbackQuery(input.callbackQueryId, {
|
|
228
|
+
...(input.text ? { text: input.text } : {}),
|
|
229
|
+
...(input.showAlert !== undefined ? { show_alert: input.showAlert } : {}),
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
private async dispatchText(ctx: GrammyTextContext): Promise<void> {
|
|
234
|
+
const message = ctx.message;
|
|
235
|
+
const update: TelegramTextUpdate = {
|
|
236
|
+
messageId: message.message_id,
|
|
237
|
+
chatId: String(message.chat.id),
|
|
238
|
+
...(message.message_thread_id !== undefined
|
|
239
|
+
? { messageThreadId: message.message_thread_id }
|
|
240
|
+
: {}),
|
|
241
|
+
from: toUserRef(message.from),
|
|
242
|
+
text: message.text,
|
|
243
|
+
};
|
|
244
|
+
await Promise.all([...this.textHandlers].map((handler) => handler(update)));
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
private async dispatchForumTopicCreated(ctx: GrammyForumTopicCreatedContext): Promise<void> {
|
|
248
|
+
const message = ctx.message;
|
|
249
|
+
if (message.message_thread_id === undefined) return;
|
|
250
|
+
const update: TelegramForumTopicCreatedUpdate = {
|
|
251
|
+
messageId: message.message_id,
|
|
252
|
+
chatId: String(message.chat.id),
|
|
253
|
+
messageThreadId: message.message_thread_id,
|
|
254
|
+
from: toUserRef(message.from),
|
|
255
|
+
};
|
|
256
|
+
await Promise.all([...this.forumTopicCreatedHandlers].map((handler) => handler(update)));
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
private async dispatchForumTopicClosed(ctx: GrammyForumTopicClosedContext): Promise<void> {
|
|
260
|
+
const message = ctx.message;
|
|
261
|
+
if (message.message_thread_id === undefined) return;
|
|
262
|
+
const update: TelegramForumTopicClosedUpdate = {
|
|
263
|
+
messageId: message.message_id,
|
|
264
|
+
chatId: String(message.chat.id),
|
|
265
|
+
messageThreadId: message.message_thread_id,
|
|
266
|
+
from: toUserRef(message.from),
|
|
267
|
+
};
|
|
268
|
+
await Promise.all([...this.forumTopicClosedHandlers].map((handler) => handler(update)));
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
private async dispatchCallback(ctx: GrammyCallbackContext): Promise<void> {
|
|
272
|
+
const callback = ctx.callbackQuery;
|
|
273
|
+
if (!callback.message) return;
|
|
274
|
+
const update: TelegramCallbackUpdate = {
|
|
275
|
+
id: callback.id,
|
|
276
|
+
chatId: String(callback.message.chat.id),
|
|
277
|
+
...(callback.message.message_thread_id !== undefined
|
|
278
|
+
? { messageThreadId: callback.message.message_thread_id }
|
|
279
|
+
: {}),
|
|
280
|
+
from: toUserRef(callback.from),
|
|
281
|
+
data: callback.data,
|
|
282
|
+
};
|
|
283
|
+
await Promise.all([...this.callbackHandlers].map((handler) => handler(update)));
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
private logWarn(error: unknown, message: string): void {
|
|
287
|
+
this.input.logger?.warn?.(
|
|
288
|
+
{ err: error instanceof Error ? error.message : String(error) },
|
|
289
|
+
message,
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import {
|
|
2
|
+
coreHostCapabilities,
|
|
3
|
+
createCapabilityToken,
|
|
4
|
+
type LeitwerkExtensionModule,
|
|
5
|
+
} from "@leitwerk-dev/process-sdk";
|
|
6
|
+
import { TelegramBridge } from "./bridge.js";
|
|
7
|
+
import { normalizeTelegramConfig } from "./config.js";
|
|
8
|
+
import { GrammyTelegramClient } from "./grammy-telegram-client.js";
|
|
9
|
+
import type { TelegramClient } from "./types.js";
|
|
10
|
+
|
|
11
|
+
export const telegramClientToken = createCapabilityToken<TelegramClient>("telegram:client");
|
|
12
|
+
|
|
13
|
+
export const manifest = {
|
|
14
|
+
id: "telegram",
|
|
15
|
+
version: "0.1.0",
|
|
16
|
+
} as const;
|
|
17
|
+
|
|
18
|
+
const telegramExtension: LeitwerkExtensionModule = {
|
|
19
|
+
manifest,
|
|
20
|
+
setupServer(api, rawConfig) {
|
|
21
|
+
const configResult = normalizeTelegramConfig(rawConfig);
|
|
22
|
+
if (!configResult.ok) {
|
|
23
|
+
throw new Error(`Invalid telegram extension config:\n${configResult.errors.join("\n")}`);
|
|
24
|
+
}
|
|
25
|
+
const config = configResult.config;
|
|
26
|
+
if (!config.enabled) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const deps = api.get(coreHostCapabilities.serverSetup);
|
|
31
|
+
if (!deps || Array.isArray(deps)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const providedClient = api.get(telegramClientToken);
|
|
36
|
+
const client = Array.isArray(providedClient) ? providedClient[0] : providedClient;
|
|
37
|
+
const telegramClient =
|
|
38
|
+
client ??
|
|
39
|
+
new GrammyTelegramClient({
|
|
40
|
+
botToken: config.botToken,
|
|
41
|
+
logger: api.logger,
|
|
42
|
+
});
|
|
43
|
+
if (!client) {
|
|
44
|
+
api.provide(telegramClientToken, telegramClient);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const bridge = new TelegramBridge({
|
|
48
|
+
config,
|
|
49
|
+
deps,
|
|
50
|
+
client: telegramClient,
|
|
51
|
+
logger: api.logger,
|
|
52
|
+
});
|
|
53
|
+
bridge.register(api.events);
|
|
54
|
+
api.onStart(() => bridge.start());
|
|
55
|
+
api.onStop(() => bridge.stop());
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
export default telegramExtension;
|
|
60
|
+
export * from "./bridge.js";
|
|
61
|
+
export * from "./config.js";
|
|
62
|
+
export * from "./fake-telegram-client.js";
|
|
63
|
+
export * from "./form-session.js";
|
|
64
|
+
export * from "./launch-session.js";
|
|
65
|
+
export * from "./markdown-html.js";
|
|
66
|
+
export * from "./process-thread-store.js";
|
|
67
|
+
export * from "./topic-title.js";
|
|
68
|
+
export type * from "./types.js";
|