Sha256: 2546b9c0ac22a2d62bd0feaf8e1534206ddb15b07040a3751ccb1f899b6715c7

Contents?: true

Size: 1.61 KB

Versions: 3

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Telegram
  module Bot
    class UpdatesController
      #  Support for parsing commands
      module Commands
        CMD_REGEX = %r{\A/([a-z\d_]{,31})(@(\S+))?(\s|$)}i.freeze

        class << self
          # Fetches command from text message. All subsequent words are returned
          # as arguments.
          # If command has mention (eg. `/test@SomeBot`), it returns commands only
          # for specified username. Set `username` to `true` to accept
          # any commands.
          def command_from_text(text, username = nil)
            return unless text
            match = text.match(CMD_REGEX)
            return unless match
            mention = match[3]
            [match[1], text.split.drop(1)] if username == true || !mention || mention == username
          end
        end

        # Override it to filter or transform commands.
        # Default implementation is to downcase and add `!` suffix.
        def action_for_command(cmd)
          "#{cmd.downcase}!"
        end

        # If payload is a message with command, then returned action is an
        # action for this command.
        # Separate method, so it can be easily overriden (ex. MessageContext).
        #
        # This is not used for edited messages/posts. It process them as basic updates.
        def action_for_message
          cmd, args = Commands.command_from_text(payload['text'], bot_username)
          return unless cmd
          [[action_for_command(cmd), {type: :command, command: cmd}], args]
        end

        alias_method :action_for_channel_post, :action_for_message
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
telegram-bot-0.16.5 lib/telegram/bot/updates_controller/commands.rb
telegram-bot-0.16.4 lib/telegram/bot/updates_controller/commands.rb
telegram-bot-0.16.3 lib/telegram/bot/updates_controller/commands.rb