0.1.3 • Published 10 months ago

react-telegram-miniapp v0.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

React Telegram MiniApp

npm version license GitHub stars GitHub issues

Overview

React Telegram MiniApp is a developer-centric library tailored for seamless integration of Telegram Mini Apps into React and Next.js projects. It provides a rich set of hooks, components, and utilities to work with the Telegram WebApps API, enabling the creation of highly interactive experiences within Telegram.

Features

  • 🌟 TelegramProvider: Efficiently manage the Telegram WebApp context state across your React components.
  • 🔄 Custom Hooks: Simplified handling of Telegram WebApp events like theme changes, button interactions, and more.
  • 🔐 Data Validation: Securely validate data received from the Telegram WebApp on your backend.
  • 🛠️ TypeScript Support: Fully typed, ensuring a robust and type-safe development experience.
  • 🎛️ Custom Script Loader: Automatically loads the Telegram WebApp script based on the environment.

Installation

Install the package using your preferred package manager:

npm install react-telegram-miniapp
# or
yarn add react-telegram-miniapp
# or
pnpm install react-telegram-miniapp

Getting Started

1. Wrap Your Application with TelegramProvider

Wrap your main application component with TelegramProvider to initialize the Telegram WebApp context.

import React from 'react';
import { TelegramProvider } from 'react-telegram-miniapp';

const MyApp = ({ Component, pageProps }) => (
  <TelegramProvider>
    <Component {...pageProps} />
  </TelegramProvider>
);

export default MyApp;

2. Utilize Hooks to Handle Telegram Events

Leverage the provided hooks to manage various Telegram WebApp events:

import React from 'react';
import { useThemeChanged, useMainButtonClicked } from 'react-telegram-miniapp';

const MyComponent = () => {
  useThemeChanged(() => {
    console.log("Theme changed!");
  });

  useMainButtonClicked(() => {
    console.log("Main button clicked!");
  });

  return <h1>Hello, Telegram User!</h1>;
};

export default MyComponent;

3. Validate Telegram WebApp Data on the Backend

Securely validate the data received from the Telegram WebApp using the validateWebAppData utility:

import { validateWebAppData } from 'react-telegram-miniapp';

const botToken = 'YOUR_BOT_TOKEN';

app.post('/validate-webapp-data', (req, res) => {
  const { initData } = req.body;

  const validationResult = validateWebAppData(initData, botToken);

  if (validationResult.isValid) {
    res.status(200).send('Data is valid');
  } else {
    res.status(400).send(`Invalid data: ${validationResult.reason}`);
  }
});

Documentation

Components

ComponentDescriptionProps
TelegramProviderInitializes and manages the Telegram WebApp context.children: ReactNode - Components to wrap.

Hooks

HookDescriptionExample Usage
useThemeChangedHandles the themeChanged event.useThemeChanged(() => { console.log("Theme changed!"); });
useMainButtonClickedHandles the mainButtonClicked event.useMainButtonClicked(() => { console.log("Main button clicked!"); });
useViewportChangedHandles the viewportChanged event.useViewportChanged((event) => { console.log("Viewport changed:", event); });
useBackButtonClickedHandles the backButtonClicked event.useBackButtonClicked(() => { console.log("Back button clicked!"); });
useSettingsButtonClickedHandles the settingsButtonClicked event.useSettingsButtonClicked(() => { console.log("Settings button clicked!"); });
useInvoiceClosedHandles the invoiceClosed event.useInvoiceClosed((event) => { console.log("Invoice closed:", event); });
usePopupClosedHandles the popupClosed event.usePopupClosed((event) => { console.log("Popup closed:", event); });
useQrTextReceivedHandles the qrTextReceived event.useQrTextReceived((event) => { console.log("QR text received:", event.data); });
useScanQrPopupClosedHandles the scanQrPopupClosed event.useScanQrPopupClosed(() => { console.log("QR scan popup closed!"); });
useClipboardTextReceivedHandles the clipboardTextReceived event.useClipboardTextReceived((event) => { console.log("Clipboard text received:", event.data); });
useWriteAccessRequestedHandles the writeAccessRequested event.useWriteAccessRequested((event) => { console.log("Write access requested:", event.status); });
useContactRequestedHandles the contactRequested event.useContactRequested((event) => { console.log("Contact requested:", event.status); });
useBiometricManagerUpdatedHandles the biometricManagerUpdated event.useBiometricManagerUpdated(() => { console.log("Biometric manager updated!"); });
useBiometricAuthRequestedHandles the biometricAuthRequested event.useBiometricAuthRequested((event) => { console.log("Biometric auth requested:", event.isAuthenticated); });
useBiometricTokenUpdatedHandles the biometricTokenUpdated event.useBiometricTokenUpdated((event) => { console.log("Biometric token updated:", event.isUpdated); });

Utilities

validateWebAppData

  • Description: Validates the integrity of data received from the Telegram WebApp.
  • Parameters:
    • initData: string - The data received from the WebApp.
    • botToken: string - Your bot’s token.
  • Returns: { isValid: boolean, reason?: string }
  • Example Usage:
const validationResult = validateWebAppData(initData, botToken);

if (validationResult.isValid) {
  console.log("Data is valid");
} else {
  console.log("Invalid data:", validationResult.reason);
}

Advanced Usage

Centralized Event Management

To manage multiple Telegram WebApp events centrally, utilize a custom hook or context:

import { useGlobalEventManager } from 'react-telegram-miniapp';

const { registerEvent, unregisterEvent } = useGlobalEventManager();

useEffect(() => {
  const themeHandler = () => console.log('Theme changed!');
  registerEvent('themeChanged', themeHandler);

  return () => unregisterEvent('themeChanged', themeHandler);
}, [registerEvent, unregisterEvent]);

Customizing Event Handling

Tailor event handling with options like debounce, throttle, once, and debug:

useThemeChanged(() => {
  console.log('Theme changed!');
}, { debounce: 300, debug: true });

Debugging Tips

  • Enable Debug Mode: Pass { debug: true } to any hook to log detailed event information.
  • Inspect Validation Failures: When using validateWebAppData, check the reason to understand validation issues.

Contributing

We welcome contributions, issues, and feature requests! Check the issues page to see how you can help.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgements

Special thanks to the contributors and the Telegram team for their exceptional platform.


Developed by CodedPro


0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

11 months ago

0.1.0

11 months ago