@telegram-web-app/core

0.0.1-beta.1 • Public • Published

@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:

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:

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

Package Sidebar

Install

npm i @telegram-web-app/core

Weekly Downloads

77

Version

0.0.1-beta.1

License

MIT

Unpacked Size

363 kB

Total Files

88

Last publish

Collaborators

  • jp0535861