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

How to Add a VS Code Editor to Your Website

How to Add a VS Code Editor to Your Website The Monaco editor by Microsoft provides a code editor component that can be easily integrated into websites. With just a few lines of code, you can add a full-featured editor similar to VS Code in your web app. In this tutorial, we'll see how to do just that. Getting Started To use Monaco, we need to include it in our page. We can get it from a CDN: < script src = "https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs/loader.min.js" > </ script > This will load the Monaco library asynchronously. Next, we need a <div> in our HTML where we can instantiate the editor: < div id = "editor" ></ div > Now in our JavaScript code, we can initialize Monaco and create the editor: require .config({ paths : { 'vs' : 'https://cdnjs.cloudflare.com/ajax/libs/monaco-editor/0.23.0/min/vs' }}); require ([ "vs/editor/editor.main" ], function ( ) { const ...

10 Free GitHub Copilot Alternatives for VS Code in 2024

10 Free GitHub Copilot Alternatives for VS Code in 2024 As developers, we're always on the lookout for tools that can help us write code more efficiently. GitHub Copilot has been a game-changer in this regard, but its premium pricing may be a deterrent for some. Fortunately, there are several free alternatives available that offer similar functionality. In this article, we'll explore 10 of the best free GitHub Copilot alternatives for Visual Studio Code in 2024. Comparison of Free GitHub Copilot Alternatives Tool Language Support Auto-Completion Code Generation Code Explanation Bito Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✓ Tabnine Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP, Swift, Kotlin, Rust, Scala ✓ ✓ ✗ Amazon CodeWhisperer Python, JavaScript, TypeScript, Java, C#, C++, Go, Ruby, PHP ✓ ✓ ✗ Codeium Python, JavaScript, TypeScript, Java, C#, C...

Top React UI Libraries ๐ŸŒŸ

๐ŸŒŸ The Ultimate Roundup of Top React UI Libraries for Your Next Project! ๐Ÿš€๐ŸŽจ Hey there, React wizards! ๐Ÿช„✨ Ready to take your frontend game to the next level? Let's dive into an even broader spectrum of incredible React UI libraries that'll make your interfaces shine like never before! ๐Ÿ’ป๐ŸŒˆ 1. Tremor UI ๐ŸŒŠ ๐ŸŒŸ Tremor UI is a rising star in the React UI galaxy! ✨ It offers a sleek and modern design language, perfect for crafting stylish buttons and more. ๐Ÿ”˜๐ŸŽจ With Tremor, you can effortlessly create eye-catching user interfaces with its intuitive API and customizable components. ๐Ÿช„✨ Key Features : Modern Design Aesthetic Easy Customization Focus on User Experience 2. Radix UI ๐ŸŒฑ ๐ŸŒŸ Radix UI is all about building accessible, powerful components for React. ๐Ÿ› ️๐Ÿ”ฉ From modals to tooltips, Radix UI provides a solid foundation for creating interactive and user-friendly interfaces. ๐ŸŒ๐Ÿงก Dive into Radix ...

Random Posts