@zhin.js/adapter-telegram 1.0.68 → 1.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/CHANGELOG.md +825 -16
- package/README.md +106 -77
- package/adapters/telegram.js +34 -0
- package/adapters/telegram.ts +39 -0
- package/agent/PERMITS.md +24 -0
- package/agent/tools/create_invite.ts +18 -0
- package/agent/tools/list_admins.ts +24 -0
- package/agent/tools/member_count.ts +16 -0
- package/agent/tools/pin_message.ts +19 -0
- package/agent/tools/react.ts +18 -0
- package/agent/tools/send_poll.ts +32 -0
- package/agent/tools/send_sticker.ts +17 -0
- package/agent/tools/set_description.ts +17 -0
- package/agent/tools/set_permissions.ts +31 -0
- package/agent/tools/unpin_message.ts +19 -0
- package/commands/endpoint/add/[id].js +3 -0
- package/commands/endpoint/add/[id].ts +3 -0
- package/commands/endpoint/list.js +3 -0
- package/commands/endpoint/list.ts +3 -0
- package/commands/endpoint/remove/[id].js +3 -0
- package/commands/endpoint/remove/[id].ts +3 -0
- package/lib/client.d.ts +12 -0
- package/lib/client.js +2 -0
- package/lib/endpoint.d.ts +112 -0
- package/lib/endpoint.js +535 -0
- package/lib/index.d.ts +4 -18
- package/lib/index.js +4 -455
- package/lib/markdown-to-html.d.ts +9 -0
- package/lib/markdown-to-html.js +75 -0
- package/lib/platform-permit.d.ts +17 -0
- package/lib/platform-permit.js +51 -0
- package/lib/polling.d.ts +9 -0
- package/lib/polling.js +57 -0
- package/lib/protocol.d.ts +299 -0
- package/lib/protocol.js +474 -0
- package/lib/telegram-endpoint-commands.d.ts +1 -0
- package/lib/telegram-endpoint-commands.js +16 -0
- package/lib/telegram-runtime-state.d.ts +1 -0
- package/lib/telegram-runtime-state.js +6 -0
- package/lib/webhook.d.ts +12 -0
- package/lib/webhook.js +55 -0
- package/package.json +59 -28
- package/plugin.js +19 -0
- package/schema.json +110 -0
- package/src/client.ts +16 -0
- package/src/endpoint.ts +679 -0
- package/src/index.ts +39 -426
- package/src/markdown-to-html.ts +86 -0
- package/src/platform-permit.ts +65 -0
- package/src/polling.ts +76 -0
- package/src/protocol.ts +767 -0
- package/src/telegram-endpoint-commands.ts +17 -0
- package/src/telegram-runtime-state.ts +7 -0
- package/src/webhook.ts +75 -0
- package/client/Dashboard.tsx +0 -295
- package/client/index.tsx +0 -11
- package/client/tsconfig.json +0 -7
- package/client/utils/api.ts +0 -17
- package/dist/index.js +0 -32
- package/lib/adapter.d.ts +0 -18
- package/lib/adapter.d.ts.map +0 -1
- package/lib/adapter.js +0 -55
- package/lib/adapter.js.map +0 -1
- package/lib/bot.d.ts +0 -140
- package/lib/bot.d.ts.map +0 -1
- package/lib/bot.js +0 -866
- package/lib/bot.js.map +0 -1
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js.map +0 -1
- package/lib/types.d.ts +0 -29
- package/lib/types.d.ts.map +0 -1
- package/lib/types.js +0 -2
- package/lib/types.js.map +0 -1
- package/plugin.yml +0 -3
- package/src/adapter.ts +0 -64
- package/src/bot.ts +0 -983
- package/src/types.ts +0 -32
- /package/{skills/telegram/SKILL.md → agent/skills/telegram.md} +0 -0
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telegram Bot API protocol helpers — no legacy Adapter/Endpoint / segment-mapper.
|
|
3
|
+
* Canonicalization is owned by gateway/core before endpoint.send.
|
|
4
|
+
*/
|
|
5
|
+
import type { IncomingMessage } from 'node:http';
|
|
6
|
+
import type { Segment } from '@zhin.js/core/runtime';
|
|
7
|
+
import type { ConversationKind, ConversationRef } from '@zhin.js/im-contract';
|
|
8
|
+
/** Plugin Runtime owner config (`plugins.<instanceKey>` / schema.json). */
|
|
9
|
+
export interface TelegramAdapterConfig {
|
|
10
|
+
readonly id?: string;
|
|
11
|
+
readonly token?: string;
|
|
12
|
+
/** Default true. `false` selects webhook mode (requires httpHostToken). */
|
|
13
|
+
readonly polling?: boolean;
|
|
14
|
+
readonly webhook?: {
|
|
15
|
+
readonly domain?: string;
|
|
16
|
+
readonly path?: string;
|
|
17
|
+
readonly secretToken?: string;
|
|
18
|
+
};
|
|
19
|
+
readonly allowedUpdates?: readonly string[];
|
|
20
|
+
readonly apiBaseUrl?: string;
|
|
21
|
+
/** Transitional: legacy root `endpoints[]` with `context: telegram`. */
|
|
22
|
+
readonly endpoints?: ReadonlyArray<Partial<ResolvedTelegramConfig> & {
|
|
23
|
+
readonly context?: string;
|
|
24
|
+
readonly polling?: boolean;
|
|
25
|
+
readonly webhook?: TelegramAdapterConfig['webhook'];
|
|
26
|
+
readonly allowedUpdates?: readonly string[];
|
|
27
|
+
readonly apiBaseUrl?: string;
|
|
28
|
+
}>;
|
|
29
|
+
}
|
|
30
|
+
export interface ResolvedTelegramConfig {
|
|
31
|
+
readonly context: 'telegram';
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly token: string;
|
|
34
|
+
readonly mode: 'polling' | 'webhook';
|
|
35
|
+
readonly allowedUpdates: readonly string[];
|
|
36
|
+
readonly apiBaseUrl: string;
|
|
37
|
+
readonly webhook?: {
|
|
38
|
+
readonly domain: string;
|
|
39
|
+
readonly path: string;
|
|
40
|
+
readonly secretToken?: string;
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export interface TelegramUser {
|
|
44
|
+
readonly id: number;
|
|
45
|
+
readonly is_bot?: boolean;
|
|
46
|
+
readonly first_name?: string;
|
|
47
|
+
readonly last_name?: string;
|
|
48
|
+
readonly username?: string;
|
|
49
|
+
}
|
|
50
|
+
export interface TelegramChat {
|
|
51
|
+
readonly id: number;
|
|
52
|
+
readonly type: 'private' | 'group' | 'supergroup' | 'channel';
|
|
53
|
+
readonly title?: string;
|
|
54
|
+
readonly username?: string;
|
|
55
|
+
}
|
|
56
|
+
export interface TelegramMessageEntity {
|
|
57
|
+
readonly type: string;
|
|
58
|
+
readonly offset: number;
|
|
59
|
+
readonly length: number;
|
|
60
|
+
readonly url?: string;
|
|
61
|
+
readonly user?: TelegramUser;
|
|
62
|
+
}
|
|
63
|
+
export interface TelegramPhotoSize {
|
|
64
|
+
readonly file_id: string;
|
|
65
|
+
readonly file_unique_id?: string;
|
|
66
|
+
readonly width?: number;
|
|
67
|
+
readonly height?: number;
|
|
68
|
+
readonly file_size?: number;
|
|
69
|
+
}
|
|
70
|
+
export interface TelegramMessage {
|
|
71
|
+
readonly message_id: number;
|
|
72
|
+
readonly date: number;
|
|
73
|
+
readonly chat: TelegramChat;
|
|
74
|
+
readonly from?: TelegramUser;
|
|
75
|
+
readonly text?: string;
|
|
76
|
+
readonly caption?: string;
|
|
77
|
+
readonly entities?: readonly TelegramMessageEntity[];
|
|
78
|
+
readonly reply_to_message?: TelegramMessage;
|
|
79
|
+
readonly photo?: readonly TelegramPhotoSize[];
|
|
80
|
+
readonly video?: {
|
|
81
|
+
readonly file_id: string;
|
|
82
|
+
readonly file_unique_id?: string;
|
|
83
|
+
readonly width?: number;
|
|
84
|
+
readonly height?: number;
|
|
85
|
+
readonly duration?: number;
|
|
86
|
+
readonly file_size?: number;
|
|
87
|
+
};
|
|
88
|
+
readonly audio?: {
|
|
89
|
+
readonly file_id: string;
|
|
90
|
+
readonly file_unique_id?: string;
|
|
91
|
+
readonly duration?: number;
|
|
92
|
+
readonly performer?: string;
|
|
93
|
+
readonly title?: string;
|
|
94
|
+
readonly file_size?: number;
|
|
95
|
+
};
|
|
96
|
+
readonly voice?: {
|
|
97
|
+
readonly file_id: string;
|
|
98
|
+
readonly file_unique_id?: string;
|
|
99
|
+
readonly duration?: number;
|
|
100
|
+
readonly file_size?: number;
|
|
101
|
+
};
|
|
102
|
+
readonly document?: {
|
|
103
|
+
readonly file_id: string;
|
|
104
|
+
readonly file_unique_id?: string;
|
|
105
|
+
readonly file_name?: string;
|
|
106
|
+
readonly mime_type?: string;
|
|
107
|
+
readonly file_size?: number;
|
|
108
|
+
};
|
|
109
|
+
readonly sticker?: {
|
|
110
|
+
readonly file_id: string;
|
|
111
|
+
readonly file_unique_id?: string;
|
|
112
|
+
readonly width?: number;
|
|
113
|
+
readonly height?: number;
|
|
114
|
+
readonly is_animated?: boolean;
|
|
115
|
+
readonly is_video?: boolean;
|
|
116
|
+
readonly emoji?: string;
|
|
117
|
+
};
|
|
118
|
+
readonly location?: {
|
|
119
|
+
readonly longitude: number;
|
|
120
|
+
readonly latitude: number;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
export interface TelegramCallbackQuery {
|
|
124
|
+
readonly id: string;
|
|
125
|
+
readonly from: TelegramUser;
|
|
126
|
+
readonly data?: string;
|
|
127
|
+
readonly message?: TelegramMessage;
|
|
128
|
+
}
|
|
129
|
+
export interface TelegramUpdate {
|
|
130
|
+
readonly update_id: number;
|
|
131
|
+
readonly message?: TelegramMessage;
|
|
132
|
+
readonly edited_message?: TelegramMessage;
|
|
133
|
+
readonly callback_query?: TelegramCallbackQuery;
|
|
134
|
+
}
|
|
135
|
+
export interface TelegramChatMember {
|
|
136
|
+
readonly status: string;
|
|
137
|
+
readonly user: TelegramUser;
|
|
138
|
+
readonly can_restrict_members?: boolean;
|
|
139
|
+
readonly can_pin_messages?: boolean;
|
|
140
|
+
readonly can_delete_messages?: boolean;
|
|
141
|
+
readonly can_manage_chat?: boolean;
|
|
142
|
+
}
|
|
143
|
+
export interface TelegramWireSegment {
|
|
144
|
+
readonly type: string;
|
|
145
|
+
readonly data?: Record<string, unknown>;
|
|
146
|
+
}
|
|
147
|
+
export interface TelegramInlineButton {
|
|
148
|
+
readonly text: string;
|
|
149
|
+
readonly callback_data: string;
|
|
150
|
+
}
|
|
151
|
+
export type TelegramOutboundAction = {
|
|
152
|
+
readonly method: 'sendMessage';
|
|
153
|
+
readonly params: {
|
|
154
|
+
readonly chat_id: number | string;
|
|
155
|
+
readonly text: string;
|
|
156
|
+
readonly parse_mode?: 'HTML';
|
|
157
|
+
readonly reply_parameters?: {
|
|
158
|
+
readonly message_id: number;
|
|
159
|
+
};
|
|
160
|
+
readonly reply_markup?: {
|
|
161
|
+
readonly inline_keyboard: TelegramInlineButton[][];
|
|
162
|
+
};
|
|
163
|
+
};
|
|
164
|
+
} | {
|
|
165
|
+
readonly method: 'sendPhoto';
|
|
166
|
+
readonly params: {
|
|
167
|
+
readonly chat_id: number | string;
|
|
168
|
+
readonly photo: string;
|
|
169
|
+
readonly caption?: string;
|
|
170
|
+
readonly parse_mode?: 'HTML';
|
|
171
|
+
readonly reply_parameters?: {
|
|
172
|
+
readonly message_id: number;
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
} | {
|
|
176
|
+
readonly method: 'sendVideo';
|
|
177
|
+
readonly params: {
|
|
178
|
+
readonly chat_id: number | string;
|
|
179
|
+
readonly video: string;
|
|
180
|
+
readonly caption?: string;
|
|
181
|
+
readonly parse_mode?: 'HTML';
|
|
182
|
+
readonly reply_parameters?: {
|
|
183
|
+
readonly message_id: number;
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
} | {
|
|
187
|
+
readonly method: 'sendAudio';
|
|
188
|
+
readonly params: {
|
|
189
|
+
readonly chat_id: number | string;
|
|
190
|
+
readonly audio: string;
|
|
191
|
+
readonly caption?: string;
|
|
192
|
+
readonly parse_mode?: 'HTML';
|
|
193
|
+
readonly reply_parameters?: {
|
|
194
|
+
readonly message_id: number;
|
|
195
|
+
};
|
|
196
|
+
};
|
|
197
|
+
} | {
|
|
198
|
+
readonly method: 'sendVoice';
|
|
199
|
+
readonly params: {
|
|
200
|
+
readonly chat_id: number | string;
|
|
201
|
+
readonly voice: string;
|
|
202
|
+
readonly caption?: string;
|
|
203
|
+
readonly parse_mode?: 'HTML';
|
|
204
|
+
readonly reply_parameters?: {
|
|
205
|
+
readonly message_id: number;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
} | {
|
|
209
|
+
readonly method: 'sendDocument';
|
|
210
|
+
readonly params: {
|
|
211
|
+
readonly chat_id: number | string;
|
|
212
|
+
readonly document: string;
|
|
213
|
+
readonly caption?: string;
|
|
214
|
+
readonly parse_mode?: 'HTML';
|
|
215
|
+
readonly reply_parameters?: {
|
|
216
|
+
readonly message_id: number;
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
} | {
|
|
220
|
+
readonly method: 'sendSticker';
|
|
221
|
+
readonly params: {
|
|
222
|
+
readonly chat_id: number | string;
|
|
223
|
+
readonly sticker: string;
|
|
224
|
+
readonly reply_parameters?: {
|
|
225
|
+
readonly message_id: number;
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
} | {
|
|
229
|
+
readonly method: 'sendLocation';
|
|
230
|
+
readonly params: {
|
|
231
|
+
readonly chat_id: number | string;
|
|
232
|
+
readonly latitude: number;
|
|
233
|
+
readonly longitude: number;
|
|
234
|
+
readonly reply_parameters?: {
|
|
235
|
+
readonly message_id: number;
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
};
|
|
239
|
+
export declare function resolveTelegramConfig(config?: TelegramAdapterConfig): ResolvedTelegramConfig;
|
|
240
|
+
export declare function normalizeWebhookPath(path: string): string;
|
|
241
|
+
export declare function buildWebhookUrl(webhook: NonNullable<ResolvedTelegramConfig['webhook']>): string;
|
|
242
|
+
export declare function readTextBody(request: IncomingMessage, options?: {
|
|
243
|
+
readonly limit?: number;
|
|
244
|
+
}): Promise<string>;
|
|
245
|
+
export declare function botApiUrl(config: Pick<ResolvedTelegramConfig, 'apiBaseUrl' | 'token'>, method: string): string;
|
|
246
|
+
/** Telegram chat.type → canonical 会话 kind(supergroup 即 group,无容器层级故无 parent)。 */
|
|
247
|
+
export declare function resolveTelegramChannelType(chatType: TelegramChat['type']): ConversationKind;
|
|
248
|
+
/**
|
|
249
|
+
* 入站归一化 → ConversationRef:Telegram 无 guild/群组容器层级,
|
|
250
|
+
* kind 直取 chat.type(private/group/supergroup/channel),无 parent。
|
|
251
|
+
*/
|
|
252
|
+
export declare function telegramInboundConversation(endpointKey: string, chat: Pick<TelegramChat, 'id' | 'type'>): ConversationRef;
|
|
253
|
+
export declare function senderDisplayName(user?: TelegramUser): string;
|
|
254
|
+
/** Build inbound text for OutboundMessageService.receive. */
|
|
255
|
+
export declare function formatInboundContent(msg: TelegramMessage): string;
|
|
256
|
+
export declare function formatCallbackContent(query: TelegramCallbackQuery): string;
|
|
257
|
+
/**
|
|
258
|
+
* 入站消息 → canonical Segment[];媒体只存在于 canonical 段,不重复写入文本。
|
|
259
|
+
* Telegram 附件只有不透明 file_id(需 getFile 二次解析,非 URL),
|
|
260
|
+
* 统一进 MediaRef kind=file;photo 取数组末尾(最大尺寸)。
|
|
261
|
+
*/
|
|
262
|
+
export declare function formatInboundSegments(msg: TelegramMessage): Segment[];
|
|
263
|
+
/**
|
|
264
|
+
* callback_query → action 段(Wave 1 C interactive 约定:
|
|
265
|
+
* {type:'action', data:{id, payload, sourceMessageId?}}),
|
|
266
|
+
* 与 formatCallbackContent / metadata.payload 同源。
|
|
267
|
+
*/
|
|
268
|
+
export declare function formatCallbackSegments(query: TelegramCallbackQuery): Segment[];
|
|
269
|
+
/**
|
|
270
|
+
* 出站待上传媒体(base64 / 本地路径 MediaRef 物化为 multipart 附件)。
|
|
271
|
+
* params 里以 `attach://<attachName>` 占位,endpoint 发送时替换为文件 part。
|
|
272
|
+
*/
|
|
273
|
+
export interface TelegramOutboundUpload {
|
|
274
|
+
readonly attachName: string;
|
|
275
|
+
readonly filename: string;
|
|
276
|
+
readonly source: {
|
|
277
|
+
readonly kind: 'base64';
|
|
278
|
+
readonly data: string;
|
|
279
|
+
} | {
|
|
280
|
+
readonly kind: 'path';
|
|
281
|
+
readonly path: string;
|
|
282
|
+
};
|
|
283
|
+
readonly mimeType?: string;
|
|
284
|
+
}
|
|
285
|
+
export interface TelegramOutboundPlan {
|
|
286
|
+
readonly actions: TelegramOutboundAction[];
|
|
287
|
+
readonly uploads: readonly TelegramOutboundUpload[];
|
|
288
|
+
}
|
|
289
|
+
/**
|
|
290
|
+
* Wire-encode an already-rendered outbound payload into Telegram Bot API actions.
|
|
291
|
+
* Segment canonicalization is intentionally not done here.
|
|
292
|
+
*/
|
|
293
|
+
export declare function formatOutboundActions(target: string | number, payload: unknown): TelegramOutboundAction[];
|
|
294
|
+
/**
|
|
295
|
+
* formatOutboundActions 的上传感知变体:canonical MediaRef kind=base64/path
|
|
296
|
+
* 的媒体段产出 `attach://` 占位 + uploads 清单(endpoint 走 multipart 表单上传);
|
|
297
|
+
* kind=url/file 保持字符串直发(file 即 Telegram file_id 不透明引用)。
|
|
298
|
+
*/
|
|
299
|
+
export declare function formatOutboundPlan(target: string | number, payload: unknown): TelegramOutboundPlan;
|