DEV Community

Sh Raj
Sh Raj

Posted on

How to Create a Telegram Bot Step by Step with Node.js and Deploy it for Free on Vercel

How to Create a Telegram Bot Step by Step with Node.js and Deploy it for Free on Vercel

Telegram bots are a popular way to automate tasks, interact with users, and provide real-time updates on the Telegram platform. In this step-by-step guide, we will learn how to create a Telegram bot using Node.js and deploy it for free using Vercel.

Step 1: Create a Telegram Bot and Obtain Token

  1. Open the Telegram app and search for the "BotFather" bot.
  2. Start a chat with BotFather by sending the command /start.
  3. To create a new bot, use the command /newbot and follow the instructions. BotFather will provide you with a unique token for your bot.

Step 2: Set up Node.js Environment

  1. Make sure you have Node.js installed on your machine. You can download it from the official Node.js website.
  2. Create a new directory for your project and navigate to it using the terminal.

Step 3: Install Required Packages

  1. Initialize a new Node.js project using npm init and follow the prompts.
  2. Install the node-telegram-bot-api package, which provides an easy-to-use interface for working with Telegram bots.
npm install node-telegram-bot-api
Enter fullscreen mode Exit fullscreen mode

Step 4: Write Bot Code

Create a new file, e.g., index.js, and set up your Telegram bot using the following code:

const TelegramBot = require('node-telegram-bot-api');

// Replace 'YOUR_TELEGRAM_BOT_TOKEN' with the token you obtained from BotFather.
const token = 'YOUR_TELEGRAM_BOT_TOKEN';

// Create a new Telegram bot instance
const bot = new TelegramBot(token, { polling: true });

// Listen for incoming messages
bot.on('message', (msg) => {
  const chatId = msg.chat.id;
  const message = msg.text;

  // Handle incoming messages here
  // You can add your custom logic and reply to users.
});
Enter fullscreen mode Exit fullscreen mode

Step 5: Customize Your Bot

Enhance your bot by adding custom logic to handle user commands, respond to messages, or perform specific actions based on user input.

Step 6: Test Your Bot Locally

  1. Open your terminal, navigate to your project directory, and run the following command to start your bot:
node index.js
Enter fullscreen mode Exit fullscreen mode
  1. Now, test your bot by sending messages to it from your Telegram app.

Step 7: Deploy Your Bot on Vercel

Vercel is a platform that allows you to deploy your applications easily, and they offer a free tier for hosting projects.

  1. If you haven't already, sign up for a Vercel account at vercel.com.
  2. Install the Vercel CLI globally on your machine:
npm install -g vercel
Enter fullscreen mode Exit fullscreen mode
  1. In your project directory, run the following command to deploy your bot:
vercel
Enter fullscreen mode Exit fullscreen mode
  1. Vercel will guide you through the deployment process. Make sure to set the entry point to index.js.

Step 8: Set Webhook (Optional)

To make your bot more efficient and responsive, you can set up a webhook instead of using polling. This allows Telegram to send messages directly to your server.

// After setting up your Express server, add the following code to set the webhook:
const express = require('express');
const app = express();

// ...

// Replace 'YOUR_VERCEL_DEPLOYED_URL' with the actual URL of your deployed Vercel app.
const webhookUrl = 'https://YOUR_VERCEL_DEPLOYED_URL/api/telegram-bot';

bot.setWebHook(webhookUrl);

// ...
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You have successfully created a Telegram bot using Node.js and deployed it for free on Vercel. You can now expand your bot's functionality and interact with users seamlessly. Enjoy building amazing bots and automating tasks on the Telegram platform!

Top comments (2)

Collapse
 
sudityashrivastav profile image
Info Comment hidden by post author - thread only accessible via permalink
sudityashrivastav

never seen suck a shitt article

Collapse
 
sh20raj profile image
Sh Raj • Edited

....

Some comments have been hidden by the post's author - find out more