<commit_before>require 'httmultiparty'
require 'json'

module Telegruby
  class Bot
    attr_accessor :id

    def initialize(api_token)
      @token = api_token
      @endpoint = "https://api.telegram.org/bot#{@token}/"
      @id = 0
    end

    # See https://core.telegram.org/bots/api for the full method list

    def get_updates(options = {}, error_callback = nil)
      if options == {}
        options = {
          :offset => @id
        }
      end

      request = self.get_request("getUpdates", options)
      if request.code != 200
        return error_callback request
      end
      update = Update.new(JSON.parse(request.body, object_class: OpenStruct))
      
      if !update.result.empty?
        # there are updates, so mark them as 'seen'
        @id = (update.result.last.update_id + 1)
      end
      
      return update
    end

    # Send a plaintext message to a chat id
    def send_message(id, text, reply: nil)
      options = {
        :chat_id => id,
        :text => text
      }

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end
      
      self.get_request("sendMessage", options)
    end

    def forward_message(id, from_id, message_id)
      options = {
        :chat_id => id,
        :from_chat_id => from_id,
        :message_id => message_id
      }

      self.get_request("forwardMessage", options)
    end
   
    # Sends a photo message to a chat id
    def send_photo(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:photo => file_id)
      else
        options.merge!(:photo => File.new(filename))
      end
     
      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendPhoto", options)
    end

    # Sends a photo from a byte string
    def send_photo_bytestring(id, str, reply: nil)
      Tempfile.open(["img", ".jpg"]) { |f|
        f.binmode
        f.write(str)
       
        self.send_photo(id, f.path, reply)
      }
    end

    def send_voice(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:audio => file_id)
      else
        options.merge!(:audio => File.new(filename))
      end
     
      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendVoice", options)
    end

    def send_sticker(id, file_id: nil, filename: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:sticker => file_id)
      else
        options.merge!(:sticker => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendSticker", options)
    end

    def send_audio(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      } 
      if file_id
        options.merge!(:audio => file_id)
      else
        options.merge!(:audio => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendAudio", options)
    end

    def send_video(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:video => file_id)
      else
        options.merge!(:video => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendVideo", options)
    end

    def send_location(id, lat, long, reply: nil)
      options = {
        :chat_id => id,
        :latitude => lat,
        :longitude => long
      }
      
      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendLocation", options)
    end
    def send_action(id, action)
      options = {
        :chat_id => id,
        :action => action
      }
      
      return self.post_request("sendChatAction", options)
    end

    # Sends a document by filename or file ID
    def send_document(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id
      }
      
      if file_id
        options.merge!(:document => file_id)
      else
        options.merge!(:document => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      return self.post_request("sendDocument", options)
    end
    def get_userphoto(id, offset: nil, limit: nil)
      options = {
        :user_id => id,
        :offset => offset,
        :limit => limit
      }
      
      return self.get_request("getUserProfilePhotos", options)
    end
    
    def set_webhook(url = nil, certificate = nil)
      options = {
        :url => nil,
      }
      if certificate
        options.merge!(:certificate => File.new(certificate))
      end
      
      return self.get_request("setWebhook", options)
    end

    protected

    # Provides a generic method for GET requests.
    def get_request(name, options = {})
      HTTMultiParty::get(@endpoint + name, query: options)
    end
<commit_msg>:eyes: installed gentoo<commit_after>require 'httmultiparty'
require 'json'

module Telegruby
  class Bot
    attr_accessor :id

    def initialize(api_token)
      @token = api_token
      @endpoint = "https://api.telegram.org/bot#{@token}/"
      @id = 0
    end

    # See https://core.telegram.org/bots/api for the full method list

    def get_updates(options = {}, error_callback = nil)
      if options == {}
        options = {
          :offset => @id
        }
      end

      request = self.get_request("getUpdates", options)
      if request.code != 200
        return error_callback request
      end
      update = Update.new(JSON.parse(request.body, object_class: OpenStruct))
      
      if !update.result.empty?
        # there are updates, so mark them as 'seen'
        @id = (update.result.last.update_id + 1)
      end
      
      return update
    end

    # Send a plaintext message to a chat id
    def send_message(id, text, reply: nil)
      options = {
        :chat_id => id,
        :text => text
      }

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end
      
      self.get_request("sendMessage", options)
    end

    def forward_message(id, from_id, message_id)
      options = {
        :chat_id => id,
        :from_chat_id => from_id,
        :message_id => message_id
      }

      self.get_request("forwardMessage", options)
    end
   
    # Sends a photo message to a chat id
    def send_photo(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:photo => file_id)
      else
        options.merge!(:photo => File.new(filename))
      end
     
      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendPhoto", options)
    end

    # Sends a photo from a byte string
    def send_photo_bytestring(id, str, reply: nil)
      Tempfile.open(["img", ".jpg"]) { |f|
        f.binmode
        f.write(str)
       
        self.send_photo(id, f.path, reply)
      }
    end

    def send_voice(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:audio => file_id)
      else
        options.merge!(:audio => File.new(filename))
      end
     
      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendVoice", options)
    end

    def send_sticker(id, file_id: nil, filename: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:sticker => file_id)
      else
        options.merge!(:sticker => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendSticker", options)
    end

    def send_audio(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      } 
      if file_id
        options.merge!(:audio => file_id)
      else
        options.merge!(:audio => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendAudio", options)
    end

    def send_video(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id,
      }
      if file_id
        options.merge!(:video => file_id)
      else
        options.merge!(:video => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendVideo", options)
    end

    def send_location(id, lat, long, reply: nil)
      options = {
        :chat_id => id,
        :latitude => lat,
        :longitude => long
      }
      
      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      self.post_request("sendLocation", options)
    end
    
    def send_action(id, action)
      options = {
        :chat_id => id,
        :action => action
      }
      
      return self.post_request("sendChatAction", options)
    end

    # Sends a document by filename or file ID
    def send_document(id, filename: nil, file_id: nil, reply: nil)
      options = {
        :chat_id => id
      }
      
      if file_id
        options.merge!(:document => file_id)
      else
        options.merge!(:document => File.new(filename))
      end

      if !reply.nil?
        options.merge!(:reply_to_message_id => reply)
      end

      return self.post_request("sendDocument", options)
    end
    
    def get_userphoto(id, offset: nil, limit: nil)
      options = {
        :user_id => id,
        :offset => offset,
        :limit => limit
      }
      
      return self.get_request("getUserProfilePhotos", options)
    end
    
    def set_webhook(url = nil, certificate = nil)
      options = {
        :url => nil,
      }
      if certificate
        options.merge!(:certificate => File.new(certificate))
      end
      
      return self.get_request("setWebhook", options)
    end

    protected

    # Provides a generic method for GET requests.
    def get_request(name, options = {})
      HTTMultiParty::get(@endpoint + name, query: options)
    end

{'fraction_non_alphanumeric': 0.11252371916508538, 'fraction_numerical': 0.0009487666034155598, 'mean_word_length': 1.9568022440392707, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 87, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4963119', 'n_tokens_mistral': 3362, 'n_tokens_neox': 3291, 'n_words': 782}