A step-by-step tutorial on writing a Telegram bot in Ruby (native)

Hello-omeletics , somehow recently I had the idea to write a Telegram bot in  Ruby  on a specific topic, in a nutshell, this bot was supposed to raise online chats through entertainment events that were thrown into the chat by the same bot at random time with a random context.





And while I was writing this bot, I got acquainted with the telegram-bot-ruby library (gem), learned how to use it together with the gem 'sqlite3-ruby' and besides, I was imbued with the many capabilities of Telegram bots, which I want to share with the dear readers of this forum, contribute so to speak.





A lot of people want to write Telegram bots, because it's fun and easy.





At the time of writing my bot, I was faced with the fact that it was difficult to find good material on the topic of Ruby bot for Telegram. By good, I mean one that tells about elegant and beautiful functionality, such as it is in the Telegram API.





Immediately I throw a link to my repository for this post:  here ,

For during testing there were bugs that I could not transfer here, suddenly why look directly into the repository.





As a result of reading this topic, I hope the reader can improve his already written bot, or right now download Ruby, Telegram and create something new and beautiful. After all, as already stated in the "Declaration of Cyberspace":





In our world, everything that the human mind is capable of creating can be reproduced and spread indefinitely without any cost. Your factories are no longer required for the global transmission of thought.





  • I suggest to start:





    My Ruby version is 2.7.2, but it is possible that everything will work with earlier / later versions.





  • The approximate structure of the application will look like this





  • The first step is to create a Gemfile - the main dependency holder for third-party gem's in Ruby.





  • File  the Gemfile :





    source 'https://rubygems.org'
    gem 'json'
    gem 'net-http-persistent', '~> 2.9'
    gem 'sqlite3'#gem  
    gem 'telegram-bot-ruby'#      Telegram 
    
          
          



    We save the file and perform the operation in the terminal





    bundle install
          
          



    ( Ruby)  Gemfile  .





  • ( ) GitHub’a, .gitignore  , JetBrains :





  • .gitignore:





    /.idea/
          
          



  • , ,  FishSocket:





  •  FishSocket.rb :





    require 'telegram/bot'
    require './library/mac-shake'
    require './library/database'
    require './modules/listener'
    require './modules/security'
    require './modules/standart_messages'
    require './modules/response'
    Entry point class
    class FishSocket
      include Database
      def initialize
        super
        # Initialize BD
        Database.setup
        # Establishing webhook via @gem telegram/bot, using API-KEY
        Telegram::Bot::Client.run(TelegramOrientedInfo::APIKEY) do |bot|
          # Start time variable, for exclude message what was sends before bot starts
          startbottime = Time.now.toi
          # Active socket listener
          bot.listen do |message|
            # Processing the new income message    #if that message sent after bot run.
            Listener.catchnewmessage(message,bot) if Listener::Security.messageisnew(startbottime,message)
          end
        end
      end
    end
    Bot start
    FishSocket.new
    
          
          



    5 :Gem telegram/bot, mac-shake, listener, security, database.





  • :





  •  mac-shake.rb:





    # frozenstringliteral: true
    module TelegramOrientedInfo
    APIKEY = ''
    end
    
          
          



  • API-KEY , , Telegram API : @BotFather





      API-Key , API-Key, .





  •  security.rb





    class FishSocket
      module Listener
        # Module for checks
        module Security
          def messageisnew(starttime, message)
            messagetime = (defined? message.date) ? message.date : message.message.date
            messagetime.toi > starttime
          end
      def message_too_far
        message_date = (defined? Listener.message.date) ? Listener.message.date : Listener.message.message.date
        message_delay = Time.now.to_i - message_date.to_i
        # if message delay less then 5 min then processing message, else ignore
        message_delay > (5 * 60)
      end
      module_function :message_is_new, :message_too_far
    end
    
    end
    end
    
          
          



    : , ( ). , 5 ( , )





  •  listener.rb:





    class FishSocket
      # Sorting new message module
      module Listener
        attr_accessor :message, :bot
    def catch_new_message(message,bot)
      self.message = message
      self.bot = bot
    
      return false if Security.message_too_far
    
      case self.message
      when Telegram::Bot::Types::CallbackQuery
        CallbackMessages.process
      when Telegram::Bot::Types::Message
        StandartMessages.process
      end
    end
    
    module_function(
      :catch_new_message,
      :message,
      :message=,
      :bot,
      :bot=
    )
    
    end
    end
    
          
          



    , callback , .  callback . Telegram API 2.0 InlineMessages. , UI ,  InlineKeyboardMarkup  , ,  CallbackMessage, , , Telegram API. .





  •  Database.rb 





    # This module assigned to all database operations
    module Database
      attr_accessor :db
    require 'sqlite3'
      # This module assigned to create table action
      module Create
        def steamaccountlist
          Database.db.execute <<-SQL
        CREATE TABLE steamaccountlist (
        accesses VARCHAR (128),
        used INTEGER (1))
          SQL
          true
        rescue SQLite3::SQLException
          false
        end
        modulefunction(
            :steamaccount_list
        )
      end
    def setup
        # Initializing database file
        self.db = SQLite3::Database.open 'autosteam.db'
        # Try to get custom table, if table not exists - create this one
        unless gettable('steamaccountlist')
          Create.steamaccount_list
        end
      end
    # Get all from the selected table
      # @var tablename
      def gettable(tablename)
        db.execute <<-SQL
        Select * from #{tablename}
        SQL
      rescue SQLite3::SQLException
        false
      end
    modulefunction(
        :gettable,
        :setup,
        :db,
        :db=
      )
    end
    
          
          



    / .





  • ,  fishsocket.rb , , Active Socket Telegram API. - Webhook Telegram API, .





  • -  





     standartmessages.rb, () . : Standart Callback. 





     standartmessages.rb :





    class FishSocket
      module Listener
        # This module assigned to processing all standart messages
        module StandartMessages
          def process
            case Listener.message.text
            when '/getaccount'
              Response.stdmessage 'Very sorry,     '
            else
              Response.stdmessage '   ,   '
            end
          end
      module_function(
          :process
      )
    end
    
    end
    end
    
          
          



    /getaccount, , . 





  • ,  Response,





     response.rb





    class FishSocket
      module Listener
        # This module assigned to responses from bot
        module Response
          def stdmessage(message, chatid = false )
            chat = (defined?Listener.message.chat.id) ? Listener.message.chat.id : Listener.message.message.chat.id
            chat = chatid if chatid
            Listener.bot.api.sendmessage(
              parsemode: 'html',
              chatid: chat,
              text: message
            )
          end
      module_function(
        :std_message
      )
    end
    
    end
    end
    
          
          



    API Telegrama , gem telegram-ruby,  api.sendmessage. Telegram API , .





  • : ( BotFather, API .





    
          
          



    /getaccount
          
          



    .





  • Inline , , Inline .





  • assets/ inlinebutton. inlinebutton.rb : 





    class FishSocket
      # This module assigned to creating InlineKeyboardButton
      module InlineButton
        GETACCOUNT = Telegram::Bot::Types::InlineKeyboardButton.new(text: ' account', callbackdata: 'getaccount')
      end
    end
    
          
          



     telegram-ruby-gem  InlineKeyboardButton.





  •  Reponse 





    def inlinemessage(message, inlinemarkup,editless = false, chatid = false)
      chat = (defined?Listener.message.chat.id) ? Listener.message.chat.id : Listener.message.message.chat.id
      chat = chatid if chatid
      Listener.bot.api.sendmessage(
        chatid: chat,
        parsemode: 'html',
        text: message,
        replymarkup: inlinemarkup)
    end
    def generateinlinemarkup(kb, force = false)
      Telegram::Bot::Types::InlineKeyboardMarkup.new(
        inlinekeyboard: kb
      )
    end
    
          
          



    modulefunction() :





    modulefunction(
      :stdmessage,
      :generateinlinemarkup,
      :inlinemessage
    )
    
          
          



  •  





    /start
          
          



    , ,  StandartMessages





    def process
      case Listener.message.text
      when '/getaccount'
        Response.stdmessage 'Very sorry,     '
      when '/start'
        Response.inlinemessage ',    ', Response::generateinlinemarkup(
            InlineButton::GETACCOUNT
        )
      else
        Response.stdmessage '   ,   '
      end
    end
    
          
          



  •  callbackmessages.rb  Callback : callbackmessages.rb





    class FishSocket
      module Listener
        # This module assigned to processing all callback messages
        module CallbackMessages
          attraccessor :callback_message
      def process
        self.callback_message = Listener.message.message
        case Listener.message.data
        when 'get_account'
          Listener::Response.std_message('    ')
        end
      end
    
      module_function(
          :process,
          :callback_message,
          :callback_message=
      )
    end
    
    end
    end
    
          
          



    StandartMessages , Telegram ,   .





  • , . fishsocket.rb





    require 'telegram/bot'
    require './library/mac-shake'
    require './library/database'
    require './modules/listener'
    require './modules/security'
    require './modules/standartmessages'
    require './modules/response'
    require './modules/callbackmessages'
    require './modules/assets/inlinebutton'
    Entry point class
    class FishSocket
      include Database
      def initialize
        super
    
          
          



  •  





    /start
          
          



    - .





  • , - 2 ForceReply , EditInlineMessage











  • ForceReply,  Response 





    def forcereplymessage(text, chatid = false)
      chat = (defined?Listener.message.chat.id) ? Listener.message.chat.id : Listener.message.message.chat.id
      chat = chatid if chatid
      Listener.bot.api.sendmessage(
        parsemode: 'html',
        chatid: chat,
        text: text,
        replymarkup: Telegram::Bot::Types::ForceReply.new(
          forcereply: true,
          selective: true
        )
      )
    end
    
          
          



    modulefunction - .





    ( , )









  • module InlineButton
      GETACCOUNT = Telegram::Bot::Types::InlineKeyboardButton.new(text: ' account', callbackdata: 'getaccount')
      HAVEPROMO = Telegram::Bot::Types::InlineKeyboardButton.new(text: ' ?', callbackdata: 'forcepromo')
    end
    
          
          



  •  





    /start
          
          



     StandartMessages





    when '/start'
      Response.inlinemessage ',    ', Response::generateinlinemarkup(
        [
            InlineButton::GETACCOUNT,
            InlineButton::HAVEPROMO
        ]
      )
    
          
          



    , .





  • ,  ForceReply: CallbackMessages





    def process
      self.callbackmessage = Listener.message.message
      case Listener.message.data
      when 'getaccount'
        Listener::Response.stdmessage('    ')
      when 'forcepromo'
        Listener::Response.forcereplymessage(' ')
      end
    end
    
          
          







  • ForceReply, : (Reply) , .     .





  • " ." , StandartMessages :  StandartMessages





    def process
      case Listener.message.text
      when '/getaccount'
        Response.stdmessage 'Very sorry,     '
      when '/start'
        Response.inlinemessage ',    ', Response::generateinlinemarkup(
          [
              InlineButton::GETACCOUNT,
              InlineButton::HAVEPROMO
          ]
        )
      else
        unless Listener.message.replytomessage.nil?
          case Listener.message.replytomessage.text
          when / /
            return Listener::Response.std_message ' ,    :' if Promos::validate Listener.message.text
        return Listener::Response.std_message '  '
      end
    end
    Response.std_message '   ,   '
    
    end
    end
    
          
          



  •  promos.rb   promos.rb





    class FishSocket
      module Listener
        # This module assigned to processing all promo-codes
        module Promos
          def validate(code)
            return true if code =~ /^1[a-zA-Z]*0$/
            false
          end
      module_function(
          :validate
      )
    end
    
    end
    end
    
          
          



    . FishSocket :  FishSocket





    require 'telegram/bot'
    require './library/mac-shake'
    require './library/database'
    require './modules/listener'
    require './modules/security'
    require './modules/standartmessages'
    require './modules/response'
    require './modules/callbackmessages'
    require './modules/assets/inline_button'
    require './modules/promos'
    Entry point class
    class FishSocket
      include Database
      def initialize
    
          
          



  • , :





    , : InlineMessages: 





  • "",  





    /start
          
          



    " ?" InlineButton





    module InlineButton
      GETACCOUNT = Telegram::Bot::Types::InlineKeyboardButton.new(text: ' account', callbackdata: 'getaccount')
      HAVEPROMO = Telegram::Bot::Types::InlineKeyboardButton.new(text: ' ?', callbackdata: 'forcepromo')
      ADDITIONMENU = Telegram::Bot::Types::InlineKeyboardButton.new(text: '', callbackdata: 'advancedmenu')
    end
    
          
          



    StandartMessages





    when '/start'
      Response.inlinemessage ',    ', Response::generateinlinemarkup(
        [
            InlineButton::GETACCOUNT,
            InlineButton::ADDITIONMENU
        ]
      )
    
          
          







  • allbackMessages:  CallbackMessages





    def process
      self.callbackmessage = Listener.message.message
      case Listener.message.data
      when 'getaccount'
        Listener::Response.stdmessage('    ')
      when 'forcepromo'
        Listener::Response.forcereply¨C222Cmenu'
        Listener::Response.inline¨C223Cinline¨C224CButton::HAVE¨C225Cmessage
          
          



  •  Response,  inlinemessage Response





    def inlinemessage(message, inlinemarkup, editless = false, chatid = false)
      chat = (defined?Listener.message.chat.id) ? Listener.message.chat.id : Listener.message.message.chat.id
      chat = chatid if chatid
      if editless
        return Listener.bot.api.editmessagetext(
          chatid: chat,
          parsemode: 'html',
          messageid: Listener.message.message.messageid,
          text: message,
          replymarkup: inlinemarkup
        )
      end
      Listener.bot.api.sendmessage(
        chatid: chat,
        parsemode: 'html',
        text: message,
        replymarkup: inline_markup
      )
    end
    
          
          



    ? - , , , - , , , .





  • , :





      





    , , ReplyKeyboard. 













Afterword:  A lot of things have not been touched upon here, but after all, there are hands and documentation for everything, for me personally, the description on GitHub was not enough. I believe that in our time anyone can become a botman, and now this person knows what to do. Peace for everyone.








All Articles