npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@telegram-web-app/core

v0.0.1-beta.1

Published

Unofficial telegram web app for bots npm package. Based on https://telegram.org/js/telegram-web-app.js.

Downloads

269

Readme

@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