Getting Started with Telegem

This guide will walk you through creating your first Telegram bot with Telegem.

Prerequisites

Step 1: Create a Telegram Bot

  1. Open Telegram and search for @BotFather
  2. Send /newbot and follow the instructions
  3. Save your bot token (something like 123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11)

Step 2: Install Telegem

gem install telegem

Or add to your Gemfile:

source 'https://rubygems.org'

gem 'telegem'

Step 3: Create Your First Bot

Create a file called bot.rb:

require 'telegem'

# Initialize bot with your token
bot = Telegem.new('YOUR_BOT_TOKEN')

# Handle /start command
bot.command('start') do |ctx|
  ctx.reply("Hello, #{ctx.from.first_name}! 👋")
end

# Handle /help command
bot.command('help') do |ctx|
  ctx.reply("I'm your friendly Telegem bot! Send /start to begin.")
end

# Handle any text message
bot.hears(/.+/) do |ctx|
  ctx.reply("You said: #{ctx.message.text}")
end

# Start the bot
puts "🤖 Bot is running..."
bot.start_polling

Step 4: Run Your Bot

ruby bot.rb

Step 5: Test Your Bot

  1. Open Telegram
  2. Find your bot by username
  3. Send /start and see the response

Understanding the Code

Bot Initialization

bot = Telegem.new('YOUR_BOT_TOKEN')

This creates a new bot instance with your token.

Command Handlers

bot.command('start') do |ctx|
  # Handle /start command
end

Commands are messages starting with /. The ctx object contains information about the update.

Text Message Handlers

bot.hears(/.+/) do |ctx|
  # Handle any text message
end

hears matches messages using regular expressions.

Context Object

The ctx (context) object provides access to:

Next Steps

Common Issues

"Bot token is invalid"

"Connection refused"

Bot doesn't respond

Development Tips

Production Deployment

For production use, consider: