0.0.1-beta.1 • Published 2 years ago

@telegram-web-app/core v0.0.1-beta.1

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@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/core

Usage

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:

  • WebView
  • WebApp
  • Utils
  • TelegramGameProxy_receiveEvent
  • TelegramGameProxy

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:

MethodTypeErrors
Telegram.WebApp.switchInlineQuerymethodWebAppMethodUnsupportedError, WebAppInlineModeDisabledError, WebAppInlineQueryInvalidError, WebAppInlineChooseChatTypeInvalidError
Telegram.WebApp.openInvoicemethodWebAppMethodUnsupportedError
Telegram.WebApp.showPopupmethodWebAppMethodUnsupportedError, WebAppPopupOpenedError, WebAppPopupParamInvalidError
Telegram.WebApp.showAlertmethodWebAppMethodUnsupportedError,WebAppPopupOpenedError, WebAppPopupParamInvalidError
Telegram.WebApp.showConfirmmethodWebAppMethodUnsupportedError,WebAppPopupOpenedError, WebAppPopupParamInvalidError
Telegram.WebApp.showScanQrPopupmethodWebAppMethodUnsupportedError, WebAppScanQrPopupOpenedError, WebAppScanQrPopupParamInvalidError
Telegram.WebApp.closeScanQrPopupmethodWebAppMethodUnsupportedError
Telegram.WebApp.readTextFromClipboardmethodWebAppMethodUnsupportedError
Telegram.WebApp.setHeaderColormethodWebAppHeaderColorKeyInvalidError
Telegram.WebApp.sendDatamethodWebAppDataInvalidError
Telegram.WebApp.openLinkmethodWebAppTelegramUrlInvalidError
Telegram.WebApp.openTelegramLinkmethodWebAppTelegramUrlInvalidError
Telegram.WebApp.backgroundColorsetterWebAppBackgroundColorInvalidError
Telegram.WebApp.HapticFeedback.impactOccurredmethodWebAppHapticFeedbackTypeInvalidError, WebAppHapticImpactStyleInvalidError
Telegram.WebApp.HapticFeedback.notificationOccurredmethodWebAppHapticFeedbackTypeInvalidError, WebAppHapticNotificationTypeInvalidError
Telegram.WebApp.HapticFeedback.selectionChangedmethodWebAppHapticFeedbackTypeInvalidError
Telegram.WebApp.MainButton.setParamsmethodWebAppMainButtonParamInvalidError
Telegram.WebApp.MainButton.setTextmethodWebAppMainButtonParamInvalidError
Telegram.WebApp.MainButton.isActivesetterWebAppMainButtonParamInvalidError
Telegram.WebApp.MainButton.isVisiblesetterWebAppMainButtonParamInvalidError
Telegram.WebApp.MainButton.textColorsetterWebAppMainButtonParamInvalidError
Telegram.WebApp.MainButton.colorsetterWebAppMainButtonParamInvalidError
Telegram.WebApp.MainButton.textsetterWebAppMainButtonParamInvalidError

Constants

Some constants available for your convenience. List of available constants:

Differences from the original library

  • uses modern syntax(dist code builded in es2020 syntax)
  • throws custom errors instead of generic Error in the original library
  • provides constants for your convenience