This monorepo contains three packages designed to create a flexible and easy-to-use settings menu for Telegram bots using Telegraf:
- telegram-settings-menu – A library for managing settings menus in Telegram bots.
- telegram-settings-menu-generator – A CLI tool for generating a settings schema from TypeScript types.
- tele-menu-demo-bot – A demo bot deployed on Yandex Cloud Functions that demonstrates the usage of
telegram-settings-menu.
You can install each package separately depending on your needs.
npm install telegram-settings-menuyarn add telegram-settings-menunpm install -g telegram-settings-menu-generatoryarn global add telegram-settings-menu-generator- Easily create and manage a settings menu for your Telegram bot
- Supports inline keyboards
- Supports nested settings
- Uses TSDoc notation for detailed descriptions
- TypeScript support
- Compatible with Yandex Cloud Functions
Here's a basic example of how to integrate telegram-settings-menu with a Telegraf bot:
import { SettingsMenu, UserContext } from 'telegram-settings-menu';
import settingsSchema from './settings.json'; // Generated using telegram-settings-menu-generator
import { Telegraf } from 'telegraf';
const bot = new Telegraf(process.env.BOT_TOKEN);
const userStateDB: Record<number, UserContext<Settings>> = {};
const menu = new SettingsMenu<Settings>(settingsSchema as Schema, bot, {
getUserContext: async (id: number) => {
return userStateDB[id];
},
updateUserContext: async (userContext) => {
userStateDB[userContext.id] = userContext;
return true;
}
});
bot.command('settings', async (ctx) => {
await menu.show(ctx);
});
export const handler: Handler.Http = async (event: Http.Event) => {
const message = JSON.parse(event.body);
await bot.handleUpdate(message);
return {
statusCode: 200,
body: '',
};
};For more details, see the full documentation: telegram-settings-menu
This package generates a JSON schema based on TypeScript types using TSDoc notation. Example usage:
telegram-settings-menu-generator --path ./settings.ts --to ./settings.jsonExample TypeScript type for menu configuration:
/**
* @title Settings
*/
export type Settings = {
/**
* @title Subscription to News
*/
subscriptionToNews?: boolean;
/**
* @default 'Daily'
* @title Period
* @format inline
*/
period: 'Every Minute' | 'Every Hour' | 'Daily';
/**
* @title Boolean
*/
boolean: {
/**
* @title Without Default
*/
withoutDefault?: boolean;
/**
* @default false
* @title Default false
*/
defaultFalse: boolean;
/**
* @default true
* @title Default true
*/
defaultTrue: boolean;
}
};For more details, see the full documentation: telegram-settings-menu-generator
This is a demo bot designed to run on Yandex Cloud Functions, demonstrating how to use telegram-settings-menu in a serverless environment.
Try the Demo Bot: @tele_menu_demo_bot
This project is licensed under the MIT License.