This guide will walk you through creating your first Telegram bot with Telegem.
/newbot and follow the instructions123456:ABC-DEF1234ghIkl-zyx57W2v1u123ew11)gem install telegem
Or add to your Gemfile:
source 'https://rubygems.org'
gem 'telegem'
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
ruby bot.rb
/start and see the responsebot = Telegem.new('YOUR_BOT_TOKEN')
This creates a new bot instance with your token.
bot.command('start') do |ctx|
# Handle /start command
end
Commands are messages starting with /. The ctx object contains information about the update.
bot.hears(/.+/) do |ctx|
# Handle any text message
end
hears matches messages using regular expressions.
The ctx (context) object provides access to:
ctx.message - The message objectctx.from - The user who sent the messagectx.chat - The chat where the message was sentctx.reply(text) - Send a replyruby bot.rb)puts statements for debuggingFor production use, consider: