Rubyでテレグラムボットを作成するためのステップバイステップのチュートリアル(ネイティブ)

こんにちは、オムレット、どういうわけか最近 、特定のトピックについてRubyTelegramボットを作成するというアイデアが ありました。一言で言えば、このボットは、同じボットによってランダムなコンテキストでランダムな時間にチャットにスローされたエンターテインメントイベントによってオンラインチャットを発生させるはずでした。





そして、このボットを書いているときに、telegram-bot-rubyライブラリ(gem)に精通し、gem'sqlite3-ruby 'と一緒に使用することを学びました。さらに、このフォーラムの親愛なる読者と共有したいTelegramボットの機能の多くを身に付けました。いわば貢献する。





楽しくて簡単なので、多くの人がテレグラムボットを書きたいと思っています。





ボットを書いているとき、テレグラム用のルビーボットのトピックに関する良い資料を見つけるのが難しいという事実に直面しました。良いとは、Telegram APIにあるような、エレガントで美しい機能について説明するものを意味します。





すぐに、この投稿のリポジトリへのリンクをスローします。 ここで

テスト中にここに転送できないバグがあったため、突然、リポジトリを直接調べます。





このトピックを読んだ結果、読者がすでに書いたボットを改善するか、今すぐRuby、Telegramをダウンロードして、新しくて美しいものを作成できることを願っています。結局のところ、「サイバースペースの宣言」ですでに述べたように:





私たちの世界では、人間の心が作り出すことができるすべてのものを、費用をかけずに無期限に複製して広めることができます。あなたの工場はもはや世界的な思考の伝達に必要ではありません。





  • 開始することをお勧めします:





    私のRubyバージョンは2.7.2ですが、すべてが以前のバージョン/後のバージョンで動作する可能性があります。





  • アプリケーションのおおよその構造は次のようになります





  • 最初のステップは、Rubyでサードパーティのgemの主要な依存関係ホルダーであるGemfileを作成することです。





  • ファイル Gemfileを





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



    ファイルを保存し、端末で操作を行います





    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. 













あとがき: ここでは多くのことに触れていませんでしたが、結局のところ、すべての手とドキュメントがあります。私個人としては、GitHubにlibの十分な説明がありませんでした。私たちの時代には誰でもボットマンになることができると私は信じています、そして今この人は何をすべきかを知っています。みんなの平和。








All Articles