module Telegram::GetApis

Public Instance Methods

get_chat(chat_id) click to toggle source

Use this method to get up to date information about the chat.

# File lib/api/get_apis.rb, line 137
def get_chat(chat_id) # rubocop:disable Metrics/MethodLength
  hash = { chat_id: chat_id }
  response = http_get('getChat', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail IdError, %{incorrect chat id}
  end

  if chat_id.negative?
    return GetChat.new(response.result)
  end
  PrivateChat.new(response.result)
end
get_chat_admins(chat_id) click to toggle source

Use this method to get a list of administrators in a chat.

# File lib/api/get_apis.rb, line 87
def get_chat_admins(chat_id)
  unless chat_id.to_i.negative? # rubocop:disable Style/IfUnlessModifier
    fail IdError, 'chat id must be supergroup id'
  end
  hash = { chat_id: chat_id }
  response = http_get('getChatAdministrators', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail FatalError, 'fatal error'
  end

  result = response.result
  users = []
  result.each do |user|
    users << ChatMember.new(user)
  end
  users
end
get_chat_member(chat_id, user_id) click to toggle source

Use this method to get information about a member of a chat.

# File lib/api/get_apis.rb, line 117
def get_chat_member(chat_id, user_id)
  hash = { chat_id: chat_id, user_id: user_id }
  response = http_get('getChatMember', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail FatalError, response.description
  end
  ChatMember.new(response.result)
end
get_chat_members_count(chat_id) click to toggle source

Use this method to get the number of members in a chat.

# File lib/api/get_apis.rb, line 107
def get_chat_members_count(chat_id)
  hash = { chat_id: chat_id }
  response = http_get('getChatMemebersCount', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    raise IdError, %{don't use private chat id}
  end
  response.result
end
get_file(file_id) click to toggle source

Use this method to get basic info about a file. Returns TFile object

# File lib/api/get_apis.rb, line 54
def get_file(file_id)
  hash = { file_id: file_id }
  response = http_get('getFile', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail IdError, 'incorrect file id'
  end
  TFile.new(response.result)
end
get_me() click to toggle source

A simple method for testing your bot's authentication token.

# File lib/api/get_apis.rb, line 44
def get_me
  response = http_get('getMe')
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail TokenError, 'incorrect bot token'
  end
  BotUser.new(response.result)
end
get_my_commands() click to toggle source

Use this method to get the current list of the bot's commands.

# File lib/api/get_apis.rb, line 151
def get_my_commands # rubocop:disable Metrics/MethodLength
  response = http_get('getMyCommands')
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail TokenError, %{seems bot token error}
  end

  commands = []
  result = response.result
  result.each do |command|
    commands << BotCommand.new(command)
  end
  commands
end
get_profile_photos(user_id, params = {}) click to toggle source

Use this method to get a list of profile pictures for a user.

# File lib/api/get_apis.rb, line 64
def get_profile_photos(user_id, params = {}) # rubocop:disable Metrics/MethodLength
  if user_id.to_i.negative? # rubocop:disable Style/IfUnlessModifier
    fail IdError, 'id must be private chat\'s id'
  end

  hash = { uesr_id: user_id }.merge!(params)
  response = http_get('getProfilePhotos', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fails TelegramError, response.description
  end

  result = response.result
  if result.instance_of? Array
    profile = []
    result.each do |e|
      profile << ProfilePhoto.new(e)
    end
    return profile
  end
  ProfilePhoto.new(result)
end
get_sticker_set_name(name) click to toggle source

Use this method to get a sticker set.

# File lib/api/get_apis.rb, line 127
def get_sticker_set_name(name)
  hash = { name: name }
  response = http_get('getStickerSetName', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail Error, %{incorrect sticker set name}
  end
  StickerSet.new(response.result)
end
get_updates(limit = 10, &block) click to toggle source

Use this method to receive incoming updates using long polling.

# File lib/api/get_apis.rb, line 14
def get_updates(limit = 10, &block) # rubocop:disable Metrics/MethodLength
  blok = {}
  if block_given?
    blok = block.call
    unless blok.instance_of? Hash # rubocop:disable Style/IfUnlessModifier
      fail ArgumentError, 'expected object is hash'
    end
  end

  hash = { 'timeout': 0, 'limit': limit, 'offset': @last_update }
  hash.merge!(blok)
  response = http_get('getUpdates', hash)
  unless response.ok # rubocop:disable Style/IfUnlessModifier
    fail TelegramError, response.desciption
  end

  result = response.result
  if result.instance_of? Array
    @last_update = result.last.update_id + 1 if result.last
  end

  updates = []
  result.each do |update|
    updates << Update.new(update)
  end
  updates
end