@telegram-web-app/core v0.0.1-beta.1
@telegram-web-app/core
Unofficial telegram web app for bots npm
package. Based on https://telegram.org/js/telegram-web-app.js
⚠️ Package is in beta release at the moment. It may contain some bugs and has lack of tests. Be careful and open issue if you find that something is wrong.
Table of contents
Installation
npm install @telegram-web-app/coreUsage
If you already followed original instruction and place https://telegram.org/js/telegram-web-app.js script in the
<head>tag, be sure that you remove it before using this library.
TL;DR
import { TelegramWebAppContainer } from '@telegram-web-app/core';
const telegram = new TelegramWebAppContainer();
// When yor app is ready
telegram.WebApp.ready();
// You can use any properties/methods described in https://core.telegram.org/bots/webapps#initializing-web-apps
telegram.WebApp.version; // for example: '6.7'Library exports TelegramWebAppContainer class which returns instance of
Telegram interface. It's contains two getters: WebApp
and WebView, their interfaces fully compatible with original telegram library
You can pass optional object with option exposeInMainWorld(default to false) to decide
whether to add or not Telegram object to window like the original library does.
import { TelegramWebAppContainer } from '@telegram-web-app/core';
const telegram = new TelegramWebAppContainer({ exposeInMainWorld: true });Makes available window.Telegram object with the following properties:
WebViewWebAppUtilsTelegramGameProxy_receiveEventTelegramGameProxy
Imports
Library use nodejs subpath exports. Because of that minimal required nodejs version is
16.10.0
Available subpath imports:
errors
@telegram-web-app/core/errors
Contains custom errors throwed by the library.
Example:
import { WebAppBackgroundColorInvalidError } from '@telegram-web-app/core/errors';types
@telegram-web-app/core/types
Contains all typescript types.
Example:
import { ThemeParams } from '@telegram-web-app/core/types';Exceptions
In the original library when exception happens, code throws generic Error class without some
useful information. For example:
if (!text.length) {
console.error('[Telegram.WebApp] Main button text is required', params.text);
throw Error('WebAppMainButtonParamInvalid');
}Library throws custom errors instead of generic Error in the original library.
All custom errors available through import from @telegram-web-app/core/errors
For example:
import { WebAppMainButtonParamInvalidError } from '@telegram-web-app/core/errors';
...
try {
telegram.WebApp.MainButton.setText(null) // only valid value type is string, so it's throws
} catch (e) {
if (e instanceof WebAppMainButtonParamInvalidError) {
// do something here
}
}Table of all methods and setters that can throw custom errors:
| Method | Type | Errors |
|---|---|---|
Telegram.WebApp.switchInlineQuery | method | WebAppMethodUnsupportedError, WebAppInlineModeDisabledError, WebAppInlineQueryInvalidError, WebAppInlineChooseChatTypeInvalidError |
Telegram.WebApp.openInvoice | method | WebAppMethodUnsupportedError |
Telegram.WebApp.showPopup | method | WebAppMethodUnsupportedError, WebAppPopupOpenedError, WebAppPopupParamInvalidError |
Telegram.WebApp.showAlert | method | WebAppMethodUnsupportedError,WebAppPopupOpenedError, WebAppPopupParamInvalidError |
Telegram.WebApp.showConfirm | method | WebAppMethodUnsupportedError,WebAppPopupOpenedError, WebAppPopupParamInvalidError |
Telegram.WebApp.showScanQrPopup | method | WebAppMethodUnsupportedError, WebAppScanQrPopupOpenedError, WebAppScanQrPopupParamInvalidError |
Telegram.WebApp.closeScanQrPopup | method | WebAppMethodUnsupportedError |
Telegram.WebApp.readTextFromClipboard | method | WebAppMethodUnsupportedError |
Telegram.WebApp.setHeaderColor | method | WebAppHeaderColorKeyInvalidError |
Telegram.WebApp.sendData | method | WebAppDataInvalidError |
Telegram.WebApp.openLink | method | WebAppTelegramUrlInvalidError |
Telegram.WebApp.openTelegramLink | method | WebAppTelegramUrlInvalidError |
Telegram.WebApp.backgroundColor | setter | WebAppBackgroundColorInvalidError |
Telegram.WebApp.HapticFeedback.impactOccurred | method | WebAppHapticFeedbackTypeInvalidError, WebAppHapticImpactStyleInvalidError |
Telegram.WebApp.HapticFeedback.notificationOccurred | method | WebAppHapticFeedbackTypeInvalidError, WebAppHapticNotificationTypeInvalidError |
Telegram.WebApp.HapticFeedback.selectionChanged | method | WebAppHapticFeedbackTypeInvalidError |
Telegram.WebApp.MainButton.setParams | method | WebAppMainButtonParamInvalidError |
Telegram.WebApp.MainButton.setText | method | WebAppMainButtonParamInvalidError |
Telegram.WebApp.MainButton.isActive | setter | WebAppMainButtonParamInvalidError |
Telegram.WebApp.MainButton.isVisible | setter | WebAppMainButtonParamInvalidError |
Telegram.WebApp.MainButton.textColor | setter | WebAppMainButtonParamInvalidError |
Telegram.WebApp.MainButton.color | setter | WebAppMainButtonParamInvalidError |
Telegram.WebApp.MainButton.text | setter | WebAppMainButtonParamInvalidError |
Constants
Some constants available for your convenience. List of available constants:
TELEGRAM_HAPTIC_FEEDBACKcontains constants related toHapticFeedbackimport { TELEGRAM_HAPTIC_FEEDBACK } from '@telegram-web-app/core';TELEGRAM_MAIN_BUTTONcontains constants related toMainButtonimport { TELEGRAM_MAIN_BUTTON } from '@telegram-web-app/core';TELEGRAM_POPUPcontains constants related toPopupimport { TELEGRAM_POPUP } from '@telegram-web-app/core';TELEGRAM_POPUP_BUTTONcontains constants related toPopupButtonimport { TELEGRAM_POPUP_BUTTON } from '@telegram-web-app/core';TELEGRAM_SCAN_QR_POPUPcontains constants related toPopupButtonimport { TELEGRAM_SCAN_QR_POPUP } from '@telegram-web-app/core';TELEGRAM_THEMEcontains constants related toThemeimport { TELEGRAM_THEME } from '@telegram-web-app/core';TELEGRAM_WEB_APPcontains constants related toWebAppimport { TELEGRAM_WEB_APP } from '@telegram-web-app/core';TELEGRAM_WEB_VIEWcontains constants related toWebView(no official docs)import { TELEGRAM_WEB_VIEW } from '@telegram-web-app/core';
Differences from the original library
- uses modern syntax(dist code builded in es2020 syntax)
- throws custom errors instead of generic
Errorin the original library - provides constants for your convenience
2 years ago
3 years ago