DEV Community

Cover image for Why should we choose telegem over telegram-bot-ruby a established gem
phåńtøm šłîçk
phåńtøm šłîçk

Posted on

Why should we choose telegem over telegram-bot-ruby a established gem

In this post we are going to be two giants

providing ways to create telegram bots

Let’s take a deep dive into telegram-bot-ruby

telegram-bot-ruby

telegram-bot-ruby as I will call it tbr is a very good gem for creating telegram bots
it was quickly adopted by rubyist with many tutorials on sitepoint medium…
example code of tbr

  require 'telegram/bot'

# Get your token from @BotFather on Telegram
token = 'YOUR_BOT_TOKEN_HERE'

Telegram::Bot::Client.run(token) do |bot|
  bot.logger.info('Bot has been started...')

  bot.listen do |message|
    case message.text
    when '/start'
      bot.api.send_message(
        chat_id: message.chat.id,
        text: "Hello, #{message.from.first_name}! I'm a simple Ruby bot. Try /time or /roll"
      )

    when '/time'
      current_time = Time.now.strftime("%Y-%m-%d %H:%M:%S")
      bot.api.send_message(
        chat_id: message.chat.id,
        text: "Current time: #{current_time}"
      )

    when '/roll'
      # Roll a random number between 1 and 100
      number = rand(1..100)
      bot.api.send_message(
        chat_id: message.chat.id,
        text: "🎲 You rolled: #{number}"
      )

    when '/photo'
      # Send a cat photo (using a placeholder service)
      bot.api.send_photo(
        chat_id: message.chat.id,
        photo: 'https://cataas.com/cat'
      )

    when /hello|hi|hey/i
      bot.api.send_message(
        chat_id: message.chat.id,
        text: "👋 Hi there!"
      )

    else
      bot.api.send_message(
        chat_id: message.chat.id,
        text: "I don't understand. Try /start to see commands."
      )
    end
  end
end
Enter fullscreen mode Exit fullscreen mode
  • what you notice

tbr is quite nice very nice it works out of the box but as you can see above it’s verbose Raw json raw api method nested arrays …

handling state conversions is very verbose and explicit

  • let’s picture a bot that does many functions The developers will have to handle spaghetti codes hard for a contributor without reading tbr docs to contribute

Then this is where telegem comes in
Telegem isn’t to replace tbr
instead it offers what tbr doesn’t and offers a clean dsl

  • no more verbose codes
  • no more raw json
  • no complexity or complex docs

Telegem superpowers

  • scene: one of Telegem great features
    what scenes
    Let’s take it as a system that automatically gets the text moves to the next step handles multiple conversations

  • psuedo code
    signup
    with scene it’s easy

  • What’s your name

  • Your email

  • your password
    reply your account has been saved this is your api key

implementation

bot.scene :signup do
  step :ask_name do |ctx|
    ctx.ask "👤 What's your full name?"
  end

  step :ask_email do |ctx, name|
    ctx.with_scene_data(name: name)
    ctx.ask "📧 What's your email address?"
  end

  step :ask_password do |ctx, email|
    ctx.with_scene_data(email: email)
    ctx.ask "🔐 Choose a password:"
  end

  step :complete do |ctx, password|
    ctx.with_scene_data(password: password)

    # Generate fake token (in real app, use JWT or similar)
    token = SecureRandom.hex(8)

    # Get all data
    data = ctx.scene_data

    ctx.reply <<~TEXT
      ✅ Account Created!

      📋 Details:
      Name: #{data[:name]}
      Email: #{data[:email]}
      Password: #{data[:password].gsub(/./, '*')}

      🔑 Your Token: #{token}

      Save this token for future access!
    TEXT

    ctx.leave_scene
  end
end
Enter fullscreen mode Exit fullscreen mode

usage: bot.command 'signup' do |ctx|
ctx.enter_scene(:signup)
end

telegem scene just works out of the box automatically moves the next step

Another method is ctx
ctx is an army you have different units and methods to use
ctx.reply
ctx.from.id
ctx.from.full_name
we won’t be talking about all this as it will take lot of time
our main focus will be ctx.reply
I find this method very easy to use
Basically let’s compare with tbr

telegem:

bot.command("start") do |ctx| 
ctx.reply("welcome to the world")
Enter fullscreen mode Exit fullscreen mode

this is very easy unlike
tbr:

bot.api.send_message(
        chat_id: message.chat.id,
        text: "welcome to the world"
      )
Enter fullscreen mode Exit fullscreen mode

digging up for chat.id
you just write ctx.reply

what’s more cool
you can use ctx.reply with options

keyboard = Telegem.keyboard do 
row "dev.to", "hashnode"
end 
ctx.reply("welcome to the world", parse_mode: 'Markdown', reply_markup: keyboard)
Enter fullscreen mode Exit fullscreen mode

I am trying to keep this as short as possible

TL;DR
tbr is an old battle-tested gem with lots of tutorials and answers to most questions
telegem is a new async first gem focus on non thread and modern features such as auto ssl using a one line command telegem-ssl

use

use tbr if you want answers to most questions you could possible get
use telegem if you want a modern day gem with cool features that remove complex api methods

Top comments (0)