DEV Community

phåńtøm šłîçk
phåńtøm šłîçk

Posted on

Building Telegram Bots in Ruby Just Got Easier with Telegem v2

If you're building Telegram bots in Ruby, you've probably faced:

  • Complex API patterns that don't feel like Ruby
  • Session management headaches
  • Deployment confusion
  • Outdated documentation

I was there too. After building several production bots, I extracted the patterns into Telegem - a framework that makes bot development actually enjoyable in Ruby.

The "Aha!" Moment

The breakthrough came when I realized most bot frameworks are ports from other languages. Ruby deserves better. So I built something that embraces Ruby idioms:

# Before: Fighting the API
bot.api.send_message(chat_id: message.chat.id, text: "Hello")

# After: Writing Ruby
ctx.reply("Hello!")  # Everything in context
Enter fullscreen mode Exit fullscreen mode

Three Patterns That Changed Everything

  1. Context-Oriented Design

Every handler gets a ctx object with everything you need:

· The message
· The user
· The chat
· Session storage
· Reply methods

No more passing around IDs or digging through nested objects.

  1. Scene System for Conversations

Multi-step interactions (surveys, orders, onboarding) are now declarative:

bot.scene(:onboarding) do |scene|
  scene.step(:ask_name) { |ctx| ctx.reply("What's your name?") }
  scene.step(:ask_age) { |ctx| ctx.reply("How old are you?") }
end
Enter fullscreen mode Exit fullscreen mode

The framework handles state, progression, and cleanup.

  1. Cloud-First Deployment
# One line for any cloud platform
bot.webhook.run  # Works on Render, Railway, Heroku, Fly.io
Enter fullscreen mode Exit fullscreen mode

No more nginx configs or manual webhook setup.

Try It Yourself

The best part? You can try it in 2 minutes:

gem install telegem
Enter fullscreen mode Exit fullscreen mode

Create bot.rb:

require 'telegem'

bot = Telegem.new(ENV['BOT_TOKEN'])
bot.command('start') { |ctx| ctx.reply("Hi!") }
bot.start_polling
Enter fullscreen mode Exit fullscreen mode

Check out the full documentation for more patterns and examples.

Why This Matters for Ruby

As Ruby developers, we care about developer experience. Our tools should:

· Get out of our way
· Follow Ruby conventions
· Scale when we need them to
· Have clear documentation

Telegem is my attempt to bring that philosophy to Telegram bot development. If you're building bots, give it a try and let me know what you think!

What patterns have you found helpful in bot development? Share in the comments!

Top comments (0)