Skip to main content

Create Telegram Bot - NextJS and Hosting on Vercel - Free

Title: Building a Telegram Bot with Next.js, GitHub, and Vercel

Creating a Telegram bot with Next.js and deploying it on Vercel is a great way to build interactive bots quickly. In this tutorial, we'll take it a step further by organizing our project structure and enabling deployment through GitHub and Vercel's user-friendly UI.

Prerequisites:

  • Basic knowledge of JavaScript and Node.js
  • Understanding of REST APIs and webhook concepts
  • Telegram account
  • GitHub account
  • Vercel account

Step 1: Set up Telegram Bot

  1. Create a new bot on Telegram using BotFather and obtain the bot token.

Step 2: Initialize a Next.js Project

  1. Install Node.js if you haven't already.
  2. Initialize a new Next.js project:
    npx create-next-app my-telegram-bot
    
  3. Navigate to your project directory:
    cd my-telegram-bot
    

Step 3: Organize Project Structure

  1. Create a folder named src in your project directory.
  2. Inside the src folder, create a folder named api.
  3. Inside the api folder, create a file named telegram.js.

Step 4: Write Telegram Bot Code

  1. Open src/api/telegram.js and add the following code:

    import { Telegraf } from 'telegraf';
    
    const bot = new Telegraf(process.env.TELEGRAM_BOT_TOKEN);
    
    bot.on('text', (ctx) => {
      ctx.reply('Hello! You said: ' + ctx.message.text);
    });
    
    bot.telegram.setWebhook(`https://${process.env.VERCEL_URL}/api/telegram`);
    
    export default async (req, res) => {
      await bot.handleUpdate(req.body, res);
    };
    

Step 5: Deploy to GitHub

  1. Initialize a git repository in your project directory:
    git init
    
  2. Add your files to the repository and commit:
    git add .
    git commit -m "Initial commit"
    
  3. Create a new repository on GitHub.
  4. Link your local repository to the remote GitHub repository:
    git remote add origin <GitHub repository URL>
    
  5. Push your code to GitHub:
    git push -u origin main
    

Step 6: Deploy to Vercel

  1. Sign in to your Vercel account.
  2. Import your project from GitHub.
  3. Follow the prompts to deploy your project.
  4. Set the environment variable TELEGRAM_BOT_TOKEN to your bot token in the Vercel dashboard.

Step 7: Set Webhook URL

  1. Use curl or any other method to set the webhook URL to your current page api/telegram.js:
    curl -F "url=https://your-vercel-url.vercel.app/api/telegram" https://api.telegram.org/bot<YOUR_BOT_TOKEN>/setWebhook
    
    Replace your-vercel-url with your Vercel project URL and <YOUR_BOT_TOKEN> with your actual bot token.

Step 8: Test Your Telegram Bot

  1. Start a conversation with your bot on Telegram.
  2. Send a message, and you should receive a response from your bot echoing your message.

Congratulations! You've successfully created a Telegram bot using Next.js, organized your project structure, deployed it on GitHub and Vercel, and set up the webhook for message handling. You can now further customize your bot's functionality and interaction based on your requirements.

Comments

Popular posts from this blog

Top Free APIs Every Developer Should Know About

Top Free APIs Every Developer Should Know About In the world of software development, APIs (Application Programming Interfaces) are essential for integrating various functionalities into applications. Here’s a curated list of top free APIs categorized by their functionality: 1. Weather APIs OpenWeatherMap API : Provides current weather data, forecasts, and historical weather data for any location. Weatherstack API : Offers real-time weather information, including forecasts and historical data. 2. Maps and Geolocation APIs Google Maps API : Enables integration of interactive maps, geocoding, and route optimization. Mapbox API : Provides customizable maps, navigation, and location search capabilities. 3. Finance and Stock Market APIs Alpha Vantage API : Offers real-time and historical equity and cryptocurrency data. Yahoo Finance API : Provides access to financial news, stock market data, and por...

Google Drive Proxy Video Player - Bypass Limits - JW Player - Embed drive videos

GooDrive :- https://goodrive.stream/ Google Drive Proxy Player #1 :- https://youtu.be/9VQK8W2iUkg Dev.to Article

Plyr.io Video Player - Integration - Skin Customizing - Adding Download Button

Plyr.io Video Player - Integration - Skin Customizing - Adding Download Button See the Pen Plyr.io Video Player - Skin Customizing to pink by SH20RAJ ( @SH20RAJ ) on CodePen . Integration :-  or Get Plyr CDNS From CDNJS Plyr <!-- Docs styles --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/CDNSFree2/Plyr/plyr.css" /> or Use CDNJS for CDN <link rel="stylesheet" href=" https://cdnjs.cloudflare.com/ajax/libs/plyr/3.6.7/plyr.min.css" /> <!--Add a Simple HTML5 Video tag--> <video controls data-poster="https://cdn.plyr.io/static/demo/View_From_A_Blue_Moon_Trailer-HD.jpg" class="vid1"> <!-- Video files --> <source src="https://rebrand.ly/sample-video" type="video/mp4" size="576" /> </video> <script src="https://cdnjs.cloudflare.com/ajax/libs/plyr/3.6.7/plyr.min.js"...

Random Posts