tinytg
v1.0.1
Published
A tiny set of tools to communicate with Telegram Mini Apps API.
Readme
tinytg
A tiny set of tools to communicate with Telegram Mini Apps API.
Add to your project:
npm i tinytgwhy?
This repo is intended for those who want a small (a few kilobytes) and unabstracted way to use Mini Apps API. Everything is stripped down to bare functions without any components or signals.
usage
receiving events
Events are the messages sent by Telegram app to the webpage.
Subscribe to the event:
import { events } from "tinytg";
events.on("eventName", (data) => {
//do something with the data
});Unsubscribe:
import { events } from "tinytg";
const listener = (data) => {
//do something with the data
};
events.off("eventName", listener);sending methods
Methods are the messages that are sent by the webpage to the Telegram app:
import { methods } from "tinytg";
methods.post("method", { data: "data" });examples
check for TMA
Check if the webpage is running as Telegram Mini App:
import { isTMA } from "tinytg";
if (isTMA()) console.log("Running in TMA");launch params
Get the launch parameters:
import { getLaunchParameters } from "tinytg";
const params = getLaunchParameters();
console.log(params);theming
Get theme parameters:
import { getLaunchParameters } from "tinytg";
const params = getLaunchParameters();
const theme_params = params.tgWebAppThemeParams;Bind theme parameters to css variables:
import { applyThemeCss } from "tinytg";
applyThemeCss();Rebind theme parameters when the user changes theme:
import { applyThemeCss, events } from "tinytg";
applyThemeCss();
events.on("theme_changed", ({ theme_params }) =>
applyThemeCss(undefined, theme_params)
);