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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@tg-tokens-vending-machine/telegraf-bot-adapter

v0.0.1

Published

Telegraf bot adapter for the Telegram token vending machine - connects the core functionality with Telegraf framework

Readme

@tg-tokens-vending-machine/telegraf-bot-adapter

Telegraf bot adapter for the Telegram token vending machine. This package provides seamless integration between the core token vending machine functionality and the Telegraf framework for Telegram bots.

📋 Features

  • Telegraf Integration: Complete Telegraf framework support
  • Message Handling: Automatic message parsing and routing
  • Error Handling: Robust error handling for bot operations
  • Type Safety: Full TypeScript support with proper typing
  • Context Management: Proper Telegram context handling
  • Middleware Support: Compatible with Telegraf middleware system

🚀 Installation

npm install @tg-tokens-vending-machine/telegraf-bot-adapter telegraf

Peer Dependencies

npm install @tg-tokens-vending-machine/core telegraf

📖 Usage

Basic Setup

import { Telegraf } from 'telegraf';
import { TelegrafBotAdapter } from '@tg-tokens-vending-machine/telegraf-bot-adapter';
import { TokenVendingMachine } from '@tg-tokens-vending-machine/core';

// Create Telegraf instance
const telegraf = new Telegraf(process.env.BOT_TOKEN);

// Create adapter
const botAdapter = new TelegrafBotAdapter(telegraf);

// Use with TokenVendingMachine
const vendingMachine = new TokenVendingMachine(
  botAdapter,      // Telegraf adapter
  allowedUsers,    // Your access control implementation
  repository       // Your storage implementation
);

// Start the bot
telegraf.launch();

// Graceful shutdown
process.once('SIGINT', () => telegraf.stop('SIGINT'));
process.once('SIGTERM', () => telegraf.stop('SIGTERM'));

Advanced Configuration

import { Telegraf } from 'telegraf';
import { TelegrafBotAdapter } from '@tg-tokens-vending-machine/telegraf-bot-adapter';

const telegraf = new Telegraf(process.env.BOT_TOKEN);

// Add middleware
telegraf.use(async (ctx, next) => {
  console.log(`Message from ${ctx.from?.username}: ${ctx.message?.text}`);
  await next();
});

// Enable logging
telegraf.use(Telegraf.log());

// Create adapter
const botAdapter = new TelegrafBotAdapter(telegraf);

// Configure bot settings
telegraf.telegram.setMyCommands([
  { command: 'start', description: 'Start the bot' },
  { command: 'token', description: 'Generate new API token' },
  { command: 'tokens', description: 'List all your tokens' },
  { command: 'revoke', description: 'Revoke a token' }
]);

Environment Variables

# Required
BOT_TOKEN=your_telegram_bot_token_here

# Optional
NODE_ENV=production
PORT=3000

🏗️ Architecture

TelegrafBotAdapter Class

The adapter implements the TelegramBot interface from the core package:

import type { TelegramBot, BotMessage } from '@tg-tokens-vending-machine/core';

class TelegrafBotAdapter implements TelegramBot {
  constructor(private telegraf: Telegraf) {}

  start(handler: (message: BotMessage) => Promise<void>): void {
    // Handle /start command
  }

  command(command: string, handler: (message: BotMessage) => Promise<void>): void {
    // Handle specific commands
  }

  on(event: string, handler: (message: BotMessage) => Promise<void>): void {
    // Handle general messages
  }
}

Message Transformation

The adapter transforms Telegraf contexts into standardized BotMessage objects:

type BotMessage = {
  fromId: number;           // User ID
  text: string;             // Message text
  reply: (text: string, options?: {
    isMarkdown?: boolean;
  }) => Promise<void>;      // Reply function
}

🔧 API Reference

Constructor

new TelegrafBotAdapter(telegraf: Telegraf)

Parameters:

  • telegraf - Configured Telegraf instance

Methods

start(handler: (message: BotMessage) => Promise<void>): void

Sets up handler for the /start command.

Parameters:

  • handler - Function to handle start command messages

command(command: string, handler: (message: BotMessage) => Promise<void>): void

Sets up handler for a specific command.

Parameters:

  • command - Command name (without /)
  • handler - Function to handle command messages

on(event: string, handler: (message: BotMessage) => Promise<void>): void

Sets up handler for general message events.

Parameters:

  • event - Event type (typically 'message')
  • handler - Function to handle messages

🧪 Testing

The package includes tests for the adapter functionality:

🔍 Error Handling

The adapter includes comprehensive error handling:

// Error handling in command handlers
telegraf.command('token', async (ctx) => {
  try {
    await handler(transformedMessage);
  } catch (error) {
    console.error('Command error:', error);
    await ctx.reply('An error occurred. Please try again later.');
  }
});

// Global error handling
telegraf.catch((err, ctx) => {
  console.error('Telegraf error:', err);
  return ctx.reply('An unexpected error occurred.');
});

🔗 Related Packages

📄 Dependencies

  • telegraf: Modern Telegram Bot API framework
  • @tg-tokens-vending-machine/core: Core interfaces and types

📄 License

MIT