Skip to content

jasurlive/TelegramBotTS

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Telegram Bot made with TypeScript

image image

Full-demo (how far you can go with the bot)

My live bot: Telegram Bot Demo

Overview

This is a Simple Telegram Bot built in TypeScript.

It can:

  • Respond to /start with a welcome message.
  • Respond to /ping with Pong!.
  • Respond to /image with a photo of Dua Lipa and caption.
  • Reply to unknown commands with a default message.
  • Optionally, admins can use an index.html page to reset the bot, check webhook status, and see basic info.

Project Structure

/project-root

β”œβ”€ public/
β”‚  └─ index.html      # Admin page for reset, webhook status
β”œβ”€ src/
β”‚  └─ index.ts        # Main webhook handler
β”œβ”€ package.json
β”œβ”€ tsconfig.json
└─ .env               # Environment variables (you create your own)

Setup Instructions

1. Create a Telegram Bot

  1. Open Telegram and search for @BotFather.
  2. Type /newbot and follow the instructions.
  3. Copy the Bot Token.

2. Set Environment Variables

Create a .env file (also add them on Vercel or Netlify):

TELEGRAM_BOT_TOKEN=YOUR_BOT_TOKEN_HERE
ADMIN_ID=YOUR_TELEGRAM_USER_ID

3. Install Dependencies

npm install

# or

yarn

4. Build TypeScript to JavaScript

If you want to deploy to Vercel or Netlify, compile TypeScript:

npx tsc
  • This generates index.js (or the folder structure according to tsconfig.json).
  • Vercel supports TS directly, but Netlify usually requires JS build.

5. Run Locally

node index.js
  • The bot will run on your local server.
  • Use ngrok if you want Telegram to reach your local server:
ngrok http 3000
  • Then set the webhook to the ngrok URL.

6. Deploy to Vercel or Netlify

Vercel

  1. Connect your project to Vercel.
  2. Make sure .env variables are set in the Vercel Dashboard.
  3. Deploy.
  4. Set your webhook:
https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook?url=https://<your-vercel-domain>/api/index

Netlify

  1. Build the TypeScript code (npx tsc) so Netlify can serve index.js.
  2. Deploy the folder containing the JS output as a serverless function.
  3. Set the Telegram webhook pointing to your Netlify function URL.

Commands

Command Response
/start Welcome message with image
/ping Pong!
/image Sends a photo of Dua Lipa with caption
anything else "Unknown command."

Admin Controls

  • public/index.html can be used to reset the bot, check webhook status, and monitor basic info.
  • This is optional but useful for non-coding admins.
  • Admin bot commands can also respond to messages differently from regular users.

How It Works

  1. Telegram sends messages to the bot webhook.
  2. The bot checks the message text.
  3. Depending on the command, it calls send or sendPhoto.
  4. Replies are sent back as replies to the original message.
  5. For menu buttons (if implemented), inline buttons can send callbacks with images, links, and descriptions.

Adding New Commands

  1. Open index.ts.
  2. Add a new else if condition inside the handler:
else if (text === "/hello") {
  await send(chatId, "Hello there!", messageId);
}
  1. Rebuild TS to JS if deploying to Vercel, Netlify:
npx tsc
  1. Redeploy.

Notes

  • This bot is stateless and minimal, ideal for learning.
  • Can be extended with menus, inline buttons, and admin panels.
  • Works flawlessly with Vercel or Netlify.

βœ… That’s it! You now have a fully working simple Telegram bot with admin support and easy deployment.


Licence

Apache 2.0

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/

About

Simple Telegram Bot made with TypeScript πŸ€–

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published