paint-brush
How to Create a Telegram Bot Using Rubyby@peter-robert-ndungu
5,680 reads
5,680 reads

How to Create a Telegram Bot Using Ruby

by peter robert NdunguApril 27th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How to Create a Telegram Bot Using Ruby? How to create a telegram bot from scratch. How to use the Ruby programming language to write the code needed and so, you will need ruby installed in your machine. Every line of code that is going to use can be found here: telegram_repository. To create an account, follow this link: Telegram-signUp.com. We also need to obtain a code from telegram called an A.P.I token. To do this we need to create several ruby classes for our logic.

Company Mentioned

Mention Thumbnail
featured image - How to Create a Telegram Bot Using Ruby
peter robert Ndungu HackerNoon profile picture

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 documentation and follow the instructions. 

3) TELEGRAM BOT FATHER.

Having installed ruby and created an account, we also need to obtain a code from telegram called an A.P.I token. To do this we need the help of the bot father. Just type the command /new bot, and follow the instructions of the bot father.

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 Gemfile by running

bundle init 
in your terminal or command line. Make sure you are in your working directory.

Then you need to add these gems in your gem file since we are going to be using them

gem 'telegram-bot-ruby'
gem  'json'
gem 'net-http-persistent', '~> 2.9', '>= 2.9.4

Then

run bundle install 
to install the dependencies

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 bot class which we are going to create later on. So in your

main.rb 
file type this code.

require_relative '../lib/bot.rb'
require_relative '../lib/motivate.rb  
Bot.new

6) CREATING RUBY CLASSES.

We need to create several ruby classes for our logic. We shall create a BOT class to host all the telegram bot API logic and MOTIVE class to make requests to an API endpoint.

Create a lib directory and create a

bot.rb 
file. We need to use a Ruby wrapper for Telegram's Bot API. Inside the
bot.rb
file type this code. 

require 'telegram/bot'
require_relative 'motivate.rb'


class Bot
  def initialize
    token = '{enter your code}'

  Telegram::Bot::Client.run(token) do |bot|
    bot.listen do |message|
      case message.text
      when '/start'

        bot.api.send_message(chat_id: message.chat.id, text: "Hello, #{message.from.first_name} , 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")

      when '/stop'

        bot.api.send_message(chat_id: message.chat.id, text: "Bye, #{message.from.first_name}", date: message.date)
      when '/motivate'
        values = Motivate.new
        value = values.select_random
        bot.api.send_message(chat_id: message.chat.id, text: "#{value['text']}", date: message.date)
      else bot.api.send_message(chat_id: message.chat.id, text: "Invalid entry, #{message.from.first_name}, you need to use  /start,  /stop , /motivate or /joke")
      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 JSON response.

We are going to use type fit quotes to get our messages. First, let's create a class to host our logic. Inside the lib directory create a file called

motive.rb
and type in that code.

require 'telegram/bot'
require 'net/http'
require 'json'
require_relative 'bot.rb'

class Motivate
  @values = nil

  def initialize
    @values = make_the_request
  end

  def make_the_request
    url = 'https://type.fit/api/quotes'
    uri = URI(url)
    response = Net::HTTP.get(uri)
    response = JSON.parse(response)
    response
  end

  def select_random
    @values = @values.sample
    @values
  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