Create a Telegram bot quote bot in Ruby
Source: Dev.to
Requirements
- Must have an IDE
- Project structure:
project_folder/
├── bot.rb
├── quote.rb
├── help.rb
├── start.rb
└── Gemfile
Note: No .env file is committed to git.
start.rb
# start.rb
class Start
def initialize(bot)
@bot = bot
start_handler
end
def start_handler
@bot.command("start") do |ctx|
ctx.reply("welcome to quote_bot use /quote to get astonishing quotes")
end
end
end
telegem provides the .command method for handling / commands (e.g., /help, /start, /quote).
ctx.reply sends a reply to the current chat and accepts options such as reply_markup and parse_mode.
help.rb
# help.rb
class Help
def initialize(bot)
@bot = bot
send_help_message
end
def send_help_message
@bot.command("help") do |ctx|
help_message = 3.0.4"
Gemfile
gem "httparty", "~> 0.21.0"
gem "dotenv", "~> 2.8"
.env file
Create a .env file (do not commit it) with your token:
BOT_TOKEN=
Installation & Running
# Install bundler if needed
gem install bundler
# Install dependencies
bundle install
# Run the bot
bundle exec ruby bot.rb
For more detailed examples, see the repository.