DEV Community

Cover image for How to create telegram bots in Ruby
phåńtøm šłîçk
phåńtøm šłîçk

Posted on

How to create telegram bots in Ruby

You must have heard or used telegram bots

now the deep question is how are they created

We are going to see to that get your ide ready cause we gonna create a bot in 5 minutes

Requirements

  • A ready good ide with Ruby installed
  • basic or advanced knowledge in Ruby
  • bot token from @botfather

If you don’t know how to get your bot token check out this post 👉 telegram_token

After getting you token
Create a GEMFILE

source 'https://rubygems.org'

gem 'telegem', '~> 3.0.4'

gem 'dotenv', '~> 2.8'

#add any other gem you wish httparty nethttp json e.t.c
Enter fullscreen mode Exit fullscreen mode

if using bundle use

$ bundle install 
Enter fullscreen mode Exit fullscreen mode

or gem if using gem

$ gem install 
Enter fullscreen mode Exit fullscreen mode

it’s pretty much recommended to use bundle but gem still works

  • next let’s create our file create a .env file with your token
BOT_TOKEN=11234:445849489449
replace with your actual token
Enter fullscreen mode Exit fullscreen mode
#bot.rb 

require 'telegem'
require 'dotenv/load'

bot = Telegem.new(ENV['BOT_TOKEN']) 

bot.command("start") do |ctx| 
  ctx.reply("welcome to test2bot") 
 end 

bot.command("help") do |ctx| 
 ctx.reply("**u need help**", parse_mode: 'Markdown') 
end 

bot.start_polling 
puts "bot has started"
Enter fullscreen mode Exit fullscreen mode
$ bundle exec ruby bot.rb
Enter fullscreen mode Exit fullscreen mode

this is all to a simple bot with /start command
/help
for more in-depth tutorial
check 👉 more-rut

Top comments (0)