Skip to content

codebam/cf-workers-telegram-bot

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2,586 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


CF Workers Telegram Bot

GitHub stars GitHub issues GitHub forks npm version

screenshot of cf-workers-telegram-bot

A lightweight, type-safe Telegram Bot framework for Cloudflare Workers.

Installation

npm install @codebam/cf-workers-telegram-bot

Quick Start

import TelegramBot, { TelegramExecutionContext } from '@codebam/cf-workers-telegram-bot';

export interface Env {
	SECRET_TELEGRAM_API_TOKEN: string;
}

export default {
	async fetch(request: Request, env: Env): Promise<Response> {
		const bot = new TelegramBot(env.SECRET_TELEGRAM_API_TOKEN);

		await bot
			.command('start', async (ctx) => {
				await ctx.reply('Hello! I am running on Cloudflare Workers.');
			})
			.onMessage(async (ctx) => {
				await ctx.reply(`You said: ${ctx.text}`);
			})
			.handle(request);

		return new Response('ok');
	},
};

Features

  • Type-safe: Built with TypeScript for a better developer experience.
  • Middleware support: Run logic before your handlers.
  • Built-in Webhook Management: Easily set your webhook with a simple URL.
  • Lightweight: Zero dependencies (other than type definitions).

Using the Consumer Template

The consumer directory in this repository serves as a template for new projects. It is included as a git submodule.

  1. Clone the repository with submodules:

    git clone --recursive https://github.com/codebam/cf-workers-telegram-bot.git

    Or, if you've already cloned it:

    git submodule update --init --recursive
  2. Copy the consumer directory:

    cp -r consumer my-new-bot
    cd my-new-bot
    npm install
  3. Configure your bot: Update wrangler.toml with your worker name.

  4. Set your Telegram Token: Get a token from @BotFather and add it to your worker:

    npx wrangler secret put SECRET_TELEGRAM_API_TOKEN
  5. Deploy:

    npm run deploy
  6. Set Webhook: Visit the following URL in your browser to register your worker with Telegram: https://<your-worker>.<your-subdomain>.workers.dev/<SECRET_TELEGRAM_API_TOKEN>/setWebhook

Deployment

Manual Deployment

Use Wrangler to deploy:

npx wrangler deploy

GitHub Actions

To automate deployments, use the Wrangler Action or Cloudflare's built-in GitHub integration.

API Documentation

Detailed API documentation is available at cf-workers-telegram-bot.codebam.ca.

License

Apache-2.0

Sponsor this project

 

Packages

 
 
 

Contributors

Languages

  • TypeScript 97.0%
  • JavaScript 1.7%
  • Other 1.3%