@casys/mcp-bridge 0.2.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/esm/_dnt.shims.d.ts +2 -0
- package/esm/_dnt.shims.d.ts.map +1 -0
- package/esm/_dnt.shims.js +57 -0
- package/esm/adapters/base-adapter.d.ts +25 -0
- package/esm/adapters/base-adapter.d.ts.map +1 -0
- package/esm/adapters/base-adapter.js +86 -0
- package/esm/adapters/line/adapter.d.ts +11 -0
- package/esm/adapters/line/adapter.d.ts.map +1 -0
- package/esm/adapters/line/adapter.js +10 -0
- package/esm/adapters/line/types.d.ts +25 -0
- package/esm/adapters/line/types.d.ts.map +1 -0
- package/esm/adapters/line/types.js +4 -0
- package/esm/adapters/telegram/adapter.d.ts +11 -0
- package/esm/adapters/telegram/adapter.d.ts.map +1 -0
- package/esm/adapters/telegram/adapter.js +10 -0
- package/esm/adapters/telegram/platform-adapter.d.ts +40 -0
- package/esm/adapters/telegram/platform-adapter.d.ts.map +1 -0
- package/esm/adapters/telegram/platform-adapter.js +214 -0
- package/esm/adapters/telegram/sdk-bridge.d.ts +8 -0
- package/esm/adapters/telegram/sdk-bridge.d.ts.map +1 -0
- package/esm/adapters/telegram/sdk-bridge.js +22 -0
- package/esm/adapters/telegram/types.d.ts +93 -0
- package/esm/adapters/telegram/types.d.ts.map +1 -0
- package/esm/adapters/telegram/types.js +6 -0
- package/esm/client/bridge.js +424 -0
- package/esm/core/adapter.d.ts +88 -0
- package/esm/core/adapter.d.ts.map +1 -0
- package/esm/core/adapter.js +10 -0
- package/esm/core/bridge-client.d.ts +77 -0
- package/esm/core/bridge-client.d.ts.map +1 -0
- package/esm/core/bridge-client.js +275 -0
- package/esm/core/message-router.d.ts +71 -0
- package/esm/core/message-router.d.ts.map +1 -0
- package/esm/core/message-router.js +187 -0
- package/esm/core/protocol.d.ts +116 -0
- package/esm/core/protocol.d.ts.map +1 -0
- package/esm/core/protocol.js +203 -0
- package/esm/core/resource-resolver.d.ts +27 -0
- package/esm/core/resource-resolver.d.ts.map +1 -0
- package/esm/core/resource-resolver.js +85 -0
- package/esm/core/transport.d.ts +46 -0
- package/esm/core/transport.d.ts.map +1 -0
- package/esm/core/transport.js +85 -0
- package/esm/core/types.d.ts +187 -0
- package/esm/core/types.d.ts.map +1 -0
- package/esm/core/types.js +35 -0
- package/esm/mod.d.ts +36 -0
- package/esm/mod.d.ts.map +1 -0
- package/esm/mod.js +33 -0
- package/esm/package.json +3 -0
- package/esm/resource-server/csp.d.ts +36 -0
- package/esm/resource-server/csp.d.ts.map +1 -0
- package/esm/resource-server/csp.js +36 -0
- package/esm/resource-server/injector.d.ts +18 -0
- package/esm/resource-server/injector.d.ts.map +1 -0
- package/esm/resource-server/injector.js +39 -0
- package/esm/resource-server/server.d.ts +107 -0
- package/esm/resource-server/server.d.ts.map +1 -0
- package/esm/resource-server/server.js +483 -0
- package/esm/resource-server/session.d.ts +60 -0
- package/esm/resource-server/session.d.ts.map +1 -0
- package/esm/resource-server/session.js +86 -0
- package/esm/resource-server/telegram-auth.d.ts +45 -0
- package/esm/resource-server/telegram-auth.d.ts.map +1 -0
- package/esm/resource-server/telegram-auth.js +161 -0
- package/package.json +31 -0
- package/script/_dnt.shims.d.ts +2 -0
- package/script/_dnt.shims.d.ts.map +1 -0
- package/script/_dnt.shims.js +60 -0
- package/script/adapters/base-adapter.d.ts +25 -0
- package/script/adapters/base-adapter.d.ts.map +1 -0
- package/script/adapters/base-adapter.js +113 -0
- package/script/adapters/line/adapter.d.ts +11 -0
- package/script/adapters/line/adapter.d.ts.map +1 -0
- package/script/adapters/line/adapter.js +14 -0
- package/script/adapters/line/types.d.ts +25 -0
- package/script/adapters/line/types.d.ts.map +1 -0
- package/script/adapters/line/types.js +5 -0
- package/script/adapters/telegram/adapter.d.ts +11 -0
- package/script/adapters/telegram/adapter.d.ts.map +1 -0
- package/script/adapters/telegram/adapter.js +14 -0
- package/script/adapters/telegram/platform-adapter.d.ts +40 -0
- package/script/adapters/telegram/platform-adapter.d.ts.map +1 -0
- package/script/adapters/telegram/platform-adapter.js +241 -0
- package/script/adapters/telegram/sdk-bridge.d.ts +8 -0
- package/script/adapters/telegram/sdk-bridge.d.ts.map +1 -0
- package/script/adapters/telegram/sdk-bridge.js +48 -0
- package/script/adapters/telegram/types.d.ts +93 -0
- package/script/adapters/telegram/types.d.ts.map +1 -0
- package/script/adapters/telegram/types.js +7 -0
- package/script/client/bridge.js +424 -0
- package/script/core/adapter.d.ts +88 -0
- package/script/core/adapter.d.ts.map +1 -0
- package/script/core/adapter.js +11 -0
- package/script/core/bridge-client.d.ts +77 -0
- package/script/core/bridge-client.d.ts.map +1 -0
- package/script/core/bridge-client.js +302 -0
- package/script/core/message-router.d.ts +71 -0
- package/script/core/message-router.d.ts.map +1 -0
- package/script/core/message-router.js +191 -0
- package/script/core/protocol.d.ts +116 -0
- package/script/core/protocol.d.ts.map +1 -0
- package/script/core/protocol.js +230 -0
- package/script/core/resource-resolver.d.ts +27 -0
- package/script/core/resource-resolver.d.ts.map +1 -0
- package/script/core/resource-resolver.js +89 -0
- package/script/core/transport.d.ts +46 -0
- package/script/core/transport.d.ts.map +1 -0
- package/script/core/transport.js +112 -0
- package/script/core/types.d.ts +187 -0
- package/script/core/types.d.ts.map +1 -0
- package/script/core/types.js +38 -0
- package/script/mod.d.ts +36 -0
- package/script/mod.d.ts.map +1 -0
- package/script/mod.js +76 -0
- package/script/package.json +3 -0
- package/script/resource-server/csp.d.ts +36 -0
- package/script/resource-server/csp.d.ts.map +1 -0
- package/script/resource-server/csp.js +39 -0
- package/script/resource-server/injector.d.ts +18 -0
- package/script/resource-server/injector.d.ts.map +1 -0
- package/script/resource-server/injector.js +42 -0
- package/script/resource-server/server.d.ts +107 -0
- package/script/resource-server/server.d.ts.map +1 -0
- package/script/resource-server/server.js +487 -0
- package/script/resource-server/session.d.ts +60 -0
- package/script/resource-server/session.d.ts.map +1 -0
- package/script/resource-server/session.js +90 -0
- package/script/resource-server/telegram-auth.d.ts +45 -0
- package/script/resource-server/telegram-auth.d.ts.map +1 -0
- package/script/resource-server/telegram-auth.js +164 -0
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.TelegramPlatformAdapter = void 0;
|
|
27
|
+
/**
|
|
28
|
+
* Telegram PlatformAdapter for the MCP Apps Bridge.
|
|
29
|
+
*
|
|
30
|
+
* Implements the high-level PlatformAdapter interface by wrapping the
|
|
31
|
+
* Telegram WebApp SDK. Maps Telegram theme, viewport, and lifecycle
|
|
32
|
+
* events to MCP Apps HostContext and LifecycleEvents.
|
|
33
|
+
*
|
|
34
|
+
* This runs client-side inside the Telegram Mini App WebView.
|
|
35
|
+
*/
|
|
36
|
+
const dntShim = __importStar(require("../../_dnt.shims.js"));
|
|
37
|
+
const sdk_bridge_js_1 = require("./sdk-bridge.js");
|
|
38
|
+
/**
|
|
39
|
+
* Maps Telegram WebApp SDK to the MCP Apps PlatformAdapter interface.
|
|
40
|
+
*
|
|
41
|
+
* Usage:
|
|
42
|
+
* ```ts
|
|
43
|
+
* const adapter = new TelegramPlatformAdapter();
|
|
44
|
+
* const client = new BridgeClient({ platform: adapter, ... });
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
class TelegramPlatformAdapter {
|
|
48
|
+
name = "telegram";
|
|
49
|
+
tg = null;
|
|
50
|
+
lifecycleHandlers = [];
|
|
51
|
+
boundThemeHandler = null;
|
|
52
|
+
boundViewportHandler = null;
|
|
53
|
+
boundActivatedHandler = null;
|
|
54
|
+
boundDeactivatedHandler = null;
|
|
55
|
+
initialize() {
|
|
56
|
+
this.tg = (0, sdk_bridge_js_1.getTelegramWebApp)();
|
|
57
|
+
// Signal to Telegram that the app is ready
|
|
58
|
+
this.tg.ready();
|
|
59
|
+
// Expand to full viewport by default
|
|
60
|
+
if (!this.tg.isExpanded) {
|
|
61
|
+
this.tg.expand();
|
|
62
|
+
}
|
|
63
|
+
// Register event listeners
|
|
64
|
+
this.setupEventListeners();
|
|
65
|
+
return Promise.resolve(this.buildHostContext());
|
|
66
|
+
}
|
|
67
|
+
getTheme() {
|
|
68
|
+
if (!this.tg) {
|
|
69
|
+
throw new Error("[TelegramPlatformAdapter] Not initialized.");
|
|
70
|
+
}
|
|
71
|
+
return this.tg.colorScheme;
|
|
72
|
+
}
|
|
73
|
+
getContainerDimensions() {
|
|
74
|
+
if (!this.tg) {
|
|
75
|
+
throw new Error("[TelegramPlatformAdapter] Not initialized.");
|
|
76
|
+
}
|
|
77
|
+
// deno-lint-ignore no-explicit-any
|
|
78
|
+
const _global = dntShim.dntGlobalThis;
|
|
79
|
+
return {
|
|
80
|
+
width: _global.innerWidth ?? 0,
|
|
81
|
+
maxHeight: this.tg.viewportStableHeight,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
onLifecycleEvent(handler) {
|
|
85
|
+
this.lifecycleHandlers.push(handler);
|
|
86
|
+
}
|
|
87
|
+
openLink(url) {
|
|
88
|
+
if (!this.tg) {
|
|
89
|
+
throw new Error("[TelegramPlatformAdapter] Not initialized.");
|
|
90
|
+
}
|
|
91
|
+
this.tg.openLink(url);
|
|
92
|
+
return Promise.resolve();
|
|
93
|
+
}
|
|
94
|
+
sendMessage(text) {
|
|
95
|
+
if (!this.tg) {
|
|
96
|
+
throw new Error("[TelegramPlatformAdapter] Not initialized.");
|
|
97
|
+
}
|
|
98
|
+
// WARNING: sendData() closes the Mini App!
|
|
99
|
+
this.tg.sendData(text);
|
|
100
|
+
return Promise.resolve();
|
|
101
|
+
}
|
|
102
|
+
getAuthData() {
|
|
103
|
+
if (!this.tg) {
|
|
104
|
+
throw new Error("[TelegramPlatformAdapter] Not initialized. Call initialize() first.");
|
|
105
|
+
}
|
|
106
|
+
return {
|
|
107
|
+
initData: this.tg.initData,
|
|
108
|
+
initDataUnsafe: this.tg.initDataUnsafe,
|
|
109
|
+
platform: this.tg.platform,
|
|
110
|
+
version: this.tg.version,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Clean up Telegram event listeners.
|
|
115
|
+
* Call this when the bridge is destroyed.
|
|
116
|
+
*/
|
|
117
|
+
destroy() {
|
|
118
|
+
if (this.tg) {
|
|
119
|
+
if (this.boundThemeHandler) {
|
|
120
|
+
this.tg.offEvent("themeChanged", this.boundThemeHandler);
|
|
121
|
+
}
|
|
122
|
+
if (this.boundViewportHandler) {
|
|
123
|
+
this.tg.offEvent("viewportChanged", this.boundViewportHandler);
|
|
124
|
+
}
|
|
125
|
+
if (this.boundActivatedHandler) {
|
|
126
|
+
this.tg.offEvent("activated", this.boundActivatedHandler);
|
|
127
|
+
}
|
|
128
|
+
if (this.boundDeactivatedHandler) {
|
|
129
|
+
this.tg.offEvent("deactivated", this.boundDeactivatedHandler);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
this.lifecycleHandlers = [];
|
|
133
|
+
this.tg = null;
|
|
134
|
+
}
|
|
135
|
+
// -------------------------------------------------------------------------
|
|
136
|
+
// Private helpers
|
|
137
|
+
// -------------------------------------------------------------------------
|
|
138
|
+
buildHostContext() {
|
|
139
|
+
if (!this.tg) {
|
|
140
|
+
throw new Error("[TelegramPlatformAdapter] Not initialized.");
|
|
141
|
+
}
|
|
142
|
+
return {
|
|
143
|
+
theme: this.tg.colorScheme,
|
|
144
|
+
styles: this.buildStyles(this.tg.themeParams),
|
|
145
|
+
containerDimensions: this.getContainerDimensions(),
|
|
146
|
+
platform: "mobile",
|
|
147
|
+
locale: this.getLocale(),
|
|
148
|
+
timeZone: this.getTimeZone(),
|
|
149
|
+
safeAreaInsets: this.buildSafeAreaInsets(),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
buildStyles(params) {
|
|
153
|
+
const variables = {};
|
|
154
|
+
if (params.bg_color) {
|
|
155
|
+
variables["--color-background-primary"] = params.bg_color;
|
|
156
|
+
}
|
|
157
|
+
if (params.secondary_bg_color) {
|
|
158
|
+
variables["--color-background-secondary"] = params.secondary_bg_color;
|
|
159
|
+
}
|
|
160
|
+
if (params.text_color) {
|
|
161
|
+
variables["--color-text-primary"] = params.text_color;
|
|
162
|
+
}
|
|
163
|
+
if (params.subtitle_text_color) {
|
|
164
|
+
variables["--color-text-secondary"] = params.subtitle_text_color;
|
|
165
|
+
}
|
|
166
|
+
if (params.section_separator_color) {
|
|
167
|
+
variables["--color-border-primary"] = params.section_separator_color;
|
|
168
|
+
}
|
|
169
|
+
if (params.accent_text_color) {
|
|
170
|
+
variables["--color-ring-primary"] = params.accent_text_color;
|
|
171
|
+
}
|
|
172
|
+
if (params.hint_color) {
|
|
173
|
+
variables["--color-text-hint"] = params.hint_color;
|
|
174
|
+
}
|
|
175
|
+
if (params.link_color) {
|
|
176
|
+
variables["--color-text-link"] = params.link_color;
|
|
177
|
+
}
|
|
178
|
+
if (params.button_color) {
|
|
179
|
+
variables["--color-button-primary"] = params.button_color;
|
|
180
|
+
}
|
|
181
|
+
if (params.button_text_color) {
|
|
182
|
+
variables["--color-button-text"] = params.button_text_color;
|
|
183
|
+
}
|
|
184
|
+
if (params.header_bg_color) {
|
|
185
|
+
variables["--color-background-header"] = params.header_bg_color;
|
|
186
|
+
}
|
|
187
|
+
if (params.section_bg_color) {
|
|
188
|
+
variables["--color-background-section"] = params.section_bg_color;
|
|
189
|
+
}
|
|
190
|
+
return { variables };
|
|
191
|
+
}
|
|
192
|
+
buildSafeAreaInsets() {
|
|
193
|
+
const inset = this.tg?.safeAreaInset;
|
|
194
|
+
return {
|
|
195
|
+
top: inset?.top ?? 0,
|
|
196
|
+
right: inset?.right ?? 0,
|
|
197
|
+
bottom: inset?.bottom ?? 0,
|
|
198
|
+
left: inset?.left ?? 0,
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
getLocale() {
|
|
202
|
+
// deno-lint-ignore no-explicit-any
|
|
203
|
+
const nav = dntShim.dntGlobalThis.navigator;
|
|
204
|
+
return nav?.language ?? "en";
|
|
205
|
+
}
|
|
206
|
+
getTimeZone() {
|
|
207
|
+
try {
|
|
208
|
+
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
// Intl may not be available in all webview runtimes; UTC is safe default
|
|
212
|
+
return "UTC";
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
setupEventListeners() {
|
|
216
|
+
if (!this.tg)
|
|
217
|
+
return;
|
|
218
|
+
this.boundThemeHandler = () => {
|
|
219
|
+
this.emitLifecycleEvent({ type: "theme-changed" });
|
|
220
|
+
};
|
|
221
|
+
this.boundViewportHandler = () => {
|
|
222
|
+
this.emitLifecycleEvent({ type: "viewport-changed" });
|
|
223
|
+
};
|
|
224
|
+
this.boundActivatedHandler = () => {
|
|
225
|
+
this.emitLifecycleEvent({ type: "activated" });
|
|
226
|
+
};
|
|
227
|
+
this.boundDeactivatedHandler = () => {
|
|
228
|
+
this.emitLifecycleEvent({ type: "deactivated" });
|
|
229
|
+
};
|
|
230
|
+
this.tg.onEvent("themeChanged", this.boundThemeHandler);
|
|
231
|
+
this.tg.onEvent("viewportChanged", this.boundViewportHandler);
|
|
232
|
+
this.tg.onEvent("activated", this.boundActivatedHandler);
|
|
233
|
+
this.tg.onEvent("deactivated", this.boundDeactivatedHandler);
|
|
234
|
+
}
|
|
235
|
+
emitLifecycleEvent(event) {
|
|
236
|
+
for (const handler of this.lifecycleHandlers) {
|
|
237
|
+
handler(event);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
exports.TelegramPlatformAdapter = TelegramPlatformAdapter;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { TelegramWebApp } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Attempt to get the Telegram WebApp instance from the global scope.
|
|
4
|
+
*
|
|
5
|
+
* @throws {Error} if running outside a Telegram Mini App context.
|
|
6
|
+
*/
|
|
7
|
+
export declare function getTelegramWebApp(): TelegramWebApp;
|
|
8
|
+
//# sourceMappingURL=sdk-bridge.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sdk-bridge.d.ts","sourceRoot":"","sources":["../../../src/adapters/telegram/sdk-bridge.ts"],"names":[],"mappings":"AASA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAEjD;;;;GAIG;AACH,wBAAgB,iBAAiB,IAAI,cAAc,CAWlD"}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.getTelegramWebApp = getTelegramWebApp;
|
|
27
|
+
/**
|
|
28
|
+
* Bridge between the Telegram WebApp SDK and the MCP Apps protocol.
|
|
29
|
+
*
|
|
30
|
+
* Wraps `window.Telegram.WebApp` to provide send/receive capabilities
|
|
31
|
+
* for JSON-RPC messages.
|
|
32
|
+
*/
|
|
33
|
+
const dntShim = __importStar(require("../../_dnt.shims.js"));
|
|
34
|
+
/**
|
|
35
|
+
* Attempt to get the Telegram WebApp instance from the global scope.
|
|
36
|
+
*
|
|
37
|
+
* @throws {Error} if running outside a Telegram Mini App context.
|
|
38
|
+
*/
|
|
39
|
+
function getTelegramWebApp() {
|
|
40
|
+
// deno-lint-ignore no-explicit-any
|
|
41
|
+
const global = dntShim.dntGlobalThis;
|
|
42
|
+
const tg = global?.Telegram?.WebApp;
|
|
43
|
+
if (!tg) {
|
|
44
|
+
throw new Error("[TelegramSdkBridge] Telegram.WebApp not found. " +
|
|
45
|
+
"This code must run inside a Telegram Mini App WebView.");
|
|
46
|
+
}
|
|
47
|
+
return tg;
|
|
48
|
+
}
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Telegram Mini App specific types.
|
|
3
|
+
*
|
|
4
|
+
* @see https://core.telegram.org/bots/webapps
|
|
5
|
+
*/
|
|
6
|
+
import type { AdapterConfig } from "../../core/types.js";
|
|
7
|
+
/** Configuration for the Telegram Mini App adapter. */
|
|
8
|
+
export interface TelegramAdapterConfig extends AdapterConfig {
|
|
9
|
+
readonly platformOptions?: {
|
|
10
|
+
/** Telegram Bot token (used server-side for validation). */
|
|
11
|
+
readonly botToken?: string;
|
|
12
|
+
/** If true, validate `initData` signature from Telegram. Defaults to true. */
|
|
13
|
+
readonly validateInitData?: boolean;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Telegram WebApp theme parameters.
|
|
18
|
+
* @see https://core.telegram.org/bots/webapps#themeparams
|
|
19
|
+
*/
|
|
20
|
+
export interface TelegramThemeParams {
|
|
21
|
+
readonly bg_color?: string;
|
|
22
|
+
readonly text_color?: string;
|
|
23
|
+
readonly hint_color?: string;
|
|
24
|
+
readonly link_color?: string;
|
|
25
|
+
readonly button_color?: string;
|
|
26
|
+
readonly button_text_color?: string;
|
|
27
|
+
readonly secondary_bg_color?: string;
|
|
28
|
+
readonly header_bg_color?: string;
|
|
29
|
+
readonly bottom_bar_bg_color?: string;
|
|
30
|
+
readonly accent_text_color?: string;
|
|
31
|
+
readonly section_bg_color?: string;
|
|
32
|
+
readonly section_header_text_color?: string;
|
|
33
|
+
readonly section_separator_color?: string;
|
|
34
|
+
readonly subtitle_text_color?: string;
|
|
35
|
+
readonly destructive_text_color?: string;
|
|
36
|
+
readonly [key: string]: string | undefined;
|
|
37
|
+
}
|
|
38
|
+
/** Safe area inset values from Telegram. */
|
|
39
|
+
export interface TelegramSafeAreaInset {
|
|
40
|
+
readonly top?: number;
|
|
41
|
+
readonly bottom?: number;
|
|
42
|
+
readonly left?: number;
|
|
43
|
+
readonly right?: number;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Subset of the Telegram WebApp API exposed to Mini Apps.
|
|
47
|
+
* @see https://core.telegram.org/bots/webapps#initializing-mini-apps
|
|
48
|
+
*/
|
|
49
|
+
export interface TelegramWebApp {
|
|
50
|
+
readonly initData: string;
|
|
51
|
+
readonly initDataUnsafe: Record<string, unknown>;
|
|
52
|
+
readonly version: string;
|
|
53
|
+
readonly platform: string;
|
|
54
|
+
readonly colorScheme: "light" | "dark";
|
|
55
|
+
readonly themeParams: TelegramThemeParams;
|
|
56
|
+
readonly viewportHeight: number;
|
|
57
|
+
readonly viewportStableHeight: number;
|
|
58
|
+
readonly isExpanded: boolean;
|
|
59
|
+
readonly safeAreaInset?: TelegramSafeAreaInset;
|
|
60
|
+
readonly contentSafeAreaInset?: TelegramSafeAreaInset;
|
|
61
|
+
sendData(data: string): void;
|
|
62
|
+
ready(): void;
|
|
63
|
+
close(): void;
|
|
64
|
+
expand(): void;
|
|
65
|
+
requestFullscreen?(): void;
|
|
66
|
+
openLink(url: string, options?: {
|
|
67
|
+
try_instant_view?: boolean;
|
|
68
|
+
}): void;
|
|
69
|
+
openTelegramLink?(url: string): void;
|
|
70
|
+
onEvent(eventType: string, handler: () => void): void;
|
|
71
|
+
offEvent(eventType: string, handler: () => void): void;
|
|
72
|
+
MainButton?: {
|
|
73
|
+
text: string;
|
|
74
|
+
isVisible: boolean;
|
|
75
|
+
show(): void;
|
|
76
|
+
hide(): void;
|
|
77
|
+
onClick(handler: () => void): void;
|
|
78
|
+
offClick(handler: () => void): void;
|
|
79
|
+
};
|
|
80
|
+
BackButton?: {
|
|
81
|
+
isVisible: boolean;
|
|
82
|
+
show(): void;
|
|
83
|
+
hide(): void;
|
|
84
|
+
onClick(handler: () => void): void;
|
|
85
|
+
offClick(handler: () => void): void;
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Telegram event types.
|
|
90
|
+
* @see https://core.telegram.org/bots/webapps#events-available-for-mini-apps
|
|
91
|
+
*/
|
|
92
|
+
export type TelegramEventType = "themeChanged" | "viewportChanged" | "mainButtonClicked" | "backButtonClicked" | "settingsButtonClicked" | "invoiceClosed" | "popupClosed" | "qrTextReceived" | "clipboardTextReceived" | "writeAccessRequested" | "contactRequested" | "activated" | "deactivated" | "fullscreenChanged" | "fullscreenFailed" | "homeScreenAdded" | "homeScreenChecked" | "accelerometerStarted" | "accelerometerStopped" | "accelerometerChanged" | "deviceOrientationStarted" | "deviceOrientationStopped" | "deviceOrientationChanged" | "gyroscopeStarted" | "gyroscopeStopped" | "gyroscopeChanged" | "locationManagerUpdated" | "locationRequested" | "emojiStatusSet" | "emojiStatusFailed" | "emojiStatusAccessRequested" | "fileDownloadRequested" | "shareMessageSent" | "shareMessageFailed";
|
|
93
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/adapters/telegram/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,uDAAuD;AACvD,MAAM,WAAW,qBAAsB,SAAQ,aAAa;IAC1D,QAAQ,CAAC,eAAe,CAAC,EAAE;QACzB,4DAA4D;QAC5D,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;QAC3B,8EAA8E;QAC9E,QAAQ,CAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;KACrC,CAAC;CACH;AAED;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B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