Module: Telegram::GetApis

Includes:
CoreApi
Included in:
AllApis, Client
Defined in:
lib/api/get_apis.rb

Overview

rubocop:disable Metrics/ModuleLength

Instance Method Summary collapse

Methods included from CoreApi

#http_get, #http_post

Instance Method Details

#get_chat(chat_id) ⇒ Object

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



137
138
139
140
141
142
143
144
145
146
147
148
# 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) ⇒ Object

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



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# 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) ⇒ Object

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



117
118
119
120
121
122
123
124
# 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) ⇒ Object

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



107
108
109
110
111
112
113
114
# 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) ⇒ Object

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



54
55
56
57
58
59
60
61
# 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_meObject

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



44
45
46
47
48
49
50
# 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_commandsObject

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



151
152
153
154
155
156
157
158
159
160
161
162
163
# 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 = {}) ⇒ Object

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



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# 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) ⇒ Object

Use this method to get a sticker set.



127
128
129
130
131
132
133
134
# 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) ⇒ Object

Use this method to receive incoming updates using long polling.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# 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