How to Create a Mini-App for Telegram Using Ruby
Creating mini-apps for Telegram is a fun process. This guide explains how to start, write a basic bot, and make it share a mini-app link.
1. Initialize a Ruby Project
- Install Ruby on your computer. Check the installation by running:
    ruby -v
- Create a new folder for the project:
    mkdir telegram_miniapp && cd telegram_miniapp
- Initialize the project:
    bundle initThis will create a Gemfile.
2. Install Required Libraries
Add the telegram-bot-ruby gem to your Gemfile:
gem 'telegram-bot-ruby'
Install the dependencies:
bundle install
3. Get a Telegram Bot Token
- Open Telegram and find the bot @BotFather.
- Send the command /newbotand follow the instructions.
- After setup, you will get a token like this:
    123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11
Save this token. You will need it to run the bot.
4. Write the Bot Code
Create a file named bot.rb and paste this code:
require 'telegram/bot'
TOKEN = '<your-telegram-token>'
Telegram::Bot::Client.run(TOKEN) do |bot|
  bot.listen do |message|
    case message.text
    when '/start'
      bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name}! Click on the button to open the mini-application.")
      kb = [
        Telegram::Bot::Types::InlineKeyboardButton.new(
          text: 'Open App',
          web_app: { url: '<your-app-url>' }
        )
      ]
      markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: [kb])
      bot.api.send_message(chat_id: message.chat.id, text: 'Try our App:', reply_markup: markup)
    else
      bot.api.send_message(chat_id: message.chat.id, text: "Undefined command. Try /start.")
    end
  end
end
Replace:
- <your-telegram-token>with your bot token.
- <your-app-url>with the URL of your mini-app.
The URL can be any responsive website. It could be a simple front-end page, a Ruby on Rails application, or any other backend you choose. Ensure that the site is mobile-friendly for the best user experience.
5. Code Explanation
Import the Library
require 'telegram/bot'
This imports the telegram-bot-ruby library to work with Telegram Bot API.
Define the Token
TOKEN = '<your-telegram-token>'
This is your bot’s token for authentication.
Start the Bot
Telegram::Bot::Client.run(TOKEN) do |bot|
This starts the bot using the token. The bot object lets you interact with Telegram.
Listen for Messages
bot.listen do |message|
This waits for user messages and passes them to the message variable.
Respond to /start Command
case message.text
when '/start'
If the user sends /start, the bot replies with a greeting message.
Send a Button
kb = [
  Telegram::Bot::Types::InlineKeyboardButton.new(
    text: 'Open App',
    web_app: { url: '<your-app-url>' }
  )
]
markup = Telegram::Bot::Types::InlineKeyboardMarkup.new(inline_keyboard: [kb])
bot.api.send_message(chat_id: message.chat.id, text: 'Try our App:', reply_markup: markup)
This creates a button labeled “Open App.” Clicking it opens the mini-app URL. The URL should point to a responsive website or web application.
Handle Unknown Commands
else
  bot.api.send_message(chat_id: message.chat.id, text: "Undefined command. Try /start.")
If the user sends an unknown command, the bot tells them to try /start.
6. Run the Bot
Run the bot with:
ruby bot.rb
The bot will now listen for messages and respond.
7. Test the Bot
- Open Telegram.
- Find your bot by its username.
- Send the /startcommand.
- Check if the bot replies correctly and the button opens your mini-app.
Now you have a working Telegram bot that links to a mini-app!