Quick Start
Installation
# Add to Gemfile
gem 'telegem'
# Or install directly
$ gem install telegem
Basic Usage
require 'telegem'
bot = Telegem.new("YOUR_BOT_TOKEN")
bot.command('start') do |ctx|
ctx.reply("Welcome to Telegem!")
end
bot.command('help') do |ctx|
ctx.reply("Available commands: /start, /help")
end
# Start the bot
bot.start_polling
Replace YOUR_BOT_TOKEN with your actual Telegram Bot token from @BotFather
Context API
The context object provides access to the current update and response methods.
Properties
Methods
# Send message
ctx.reply("Hello!")
# Send photo
ctx.photo("photo.jpg", caption: "My photo")
# Edit message
ctx.edit_message_text("Updated text")
# Delete message
ctx.delete_message(message_id)
Bot API
Configure handlers and control bot lifecycle.
# Handle commands
bot.command('start') { |ctx| ctx.reply("Welcome") }
# Handle text patterns
bot.hears(/hello/i) { |ctx| ctx.reply("Hi there!") }
# Handle specific update types
bot.on(:photo) { |ctx| ctx.reply("Nice photo!") }
bot.on(:callback_query) { |ctx| ctx.answer_callback_query }
# Start bot
bot.start_polling
# or
bot.webhook.run
Scenes
Multi-step conversations for complex interactions.
bot.scene :registration do
step :ask_name do |ctx|
ctx.reply("What is your name?")
end
step :save_name do |ctx|
ctx.session[:name] = ctx.message.text
ctx.reply("Hello #{ctx.session[:name]}!")
ctx.leave_scene
end
end
# Enter scene
bot.command('register') { |ctx| ctx.enter_scene(:registration) }
Webhook Deployment
Production deployment with webhooks.
# Production setup
bot = Telegem.new("TOKEN")
# Configure webhook
bot.webhook(
url: 'https://your-domain.com/webhook',
port: 3000
).run
# For cloud platforms
bot.webhook.run # Auto-detects platform
Webhook mode is recommended for production. Uses less resources than polling.