I was tasked with creating a telegram bot using ruby a while back. I had no idea where to start and just like any other programmer out there I turned to the internet for clues. I did not find a good article and hence experienced a lot of problems creating the bot. I have decided to write an article to explain how to create a telegram bot from scratch. I am going to teach you by creating a motivational telegram bot. Every line of code that is going to use can be found here: . telegram_repository 1) CREATE A TELEGRAM ACCOUNT. We need this to run our telegram bot and also initialize it. To create an account follow this link: . telegram-signUp 2) INSTALL RUBY. We are going to use the Ruby programming language to write the code needed and so, you will need ruby installed in your machine. To install ruby read this ruby installation and follow the instructions. documentation 3) TELEGRAM BOT FATHER. Having installed ruby and created an account, we also need to obtain a code from telegram called an token. To do this we need the help of the . Just type the command and follow the instructions of the bot father. A.P.I bot father /new bot, 4) FILES SET UP Now the fun part begins, you need to create a directory in your machine and open it using the code editor of your choice. Then initialize a ruby by running in your terminal or command line. Make sure you are in your working directory. Gemfile bundle init Then you need to add these gems in your gem file since we are going to be using them gem gem gem , , 'telegram-bot-ruby' 'json' 'net-http-persistent' '~> 2.9' '>= 2.9.4 Then to install the dependencies run bundle install 5) EXECUTABLE FILE We need to create an executable file that will be responsible for initializing our bot. To do this, create a bin directory and create a file inside it called main.rb To initialize our app we need an instance of the which we are going to create later on. So in your file type this code. bot class main.rb require_relative require_relative '../lib/bot.rb' '../lib/motivate.rb Bot.new 6) CREATING RUBY CLASSES. We need to create several ruby classes for our logic. We shall create class to host all the telegram bot API logic and class to make requests to an API endpoint. a BOT MOTIVE Create a lib directory and create a file. We need to use a Ruby for . Inside the file type this code. bot.rb wrapper Telegram's Bot API bot.rb require_relative token = Telegram::Bot::Client.run(token) bot.listen message.text bot.api.send_message( message.chat.id, ) bot.api.send_message( message.chat.id, , message.date) values = Motivate.new value = values.select_random bot.api.send_message( message.chat.id, , message.date) bot.api.send_message( message.chat.id, ) require 'telegram/bot' 'motivate.rb' class Bot def initialize '{enter your code}' do |bot| do |message| case when '/start' chat_id: text: "Hello, , welcome to motivation chat bot created by peter robert, the chat bot is to keep you motivated and entertained. Use /start to start the bot, /stop to end the bot, /motivate to get a diffrent motivational quote everytime you request for it or /joke to get a joke everytime you request for it" #{message.from.first_name} when '/stop' chat_id: text: "Bye, " #{message.from.first_name} date: when '/motivate' chat_id: text: " " #{value[ ]} 'text' date: else chat_id: text: "Invalid entry, , you need to use /start, /stop , /motivate or /joke" #{message.from.first_name} end end end end end You need to insert the API token that you got from step three above. 7) MAKING A REQUEST TO AN END POINT We are creating a motivational bot, to achieve this we need to make requests to an endpoint that returns motivational messages as response. JSON We are going to use to get our messages. First, let's create a class to host our logic. Inside the lib directory create a file called and type in that code. type fit quotes motive.rb require_relative @values = @values = make_the_request url = uri = URI(url) response = Net::HTTP.get(uri) response = JSON.parse(response) response @values = @values.sample @values require 'telegram/bot' require 'net/http' require 'json' 'bot.rb' class Motivate nil def initialize end def make_the_request 'https://type.fit/api/quotes' end def select_random end end 8) WE ARE DONE. We are done with the heavy lifting. Now to initialize our bot, just run this code in your command line ruby bin/main.rb Then navigate to your telegram account and search for the bot you created in step three above. ENJOY! https://github.com/peterrobert