Class: MaxApiClient::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/max_api_client/api.rb,
sig/max_api_client.rbs

Overview

High-level convenience wrapper over grouped Max Bot API endpoints.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token:, base_url: Client::DEFAULT_BASE_URL, adapter: nil, open_timeout: nil, read_timeout: nil, ca_file: Client::DEFAULT_CA_FILE, verify_ssl: true, logger: nil) ⇒ Api

rubocop:disable Metrics/ParameterLists

Parameters:

  • token: (String)
  • base_url: (String) (defaults to: Client::DEFAULT_BASE_URL)
  • adapter: (Object) (defaults to: nil)
  • open_timeout: (Numeric, nil) (defaults to: nil)
  • read_timeout: (Numeric, nil) (defaults to: nil)
  • ca_file: (String, nil) (defaults to: Client::DEFAULT_CA_FILE)
  • verify_ssl: (Boolean) (defaults to: true)
  • logger: (Object) (defaults to: nil)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/max_api_client/api.rb', line 9

def initialize(token:, base_url: Client::DEFAULT_BASE_URL, adapter: nil, open_timeout: nil, read_timeout: nil,
               ca_file: Client::DEFAULT_CA_FILE, verify_ssl: true, logger: nil)
  @client = Client.new(
    token:,
    base_url:,
    adapter:,
    open_timeout:,
    read_timeout:,
    ca_file:,
    verify_ssl:,
    logger:
  )
  @raw = RawApi.new(client)
  @upload = Upload.new(self)
end

Instance Attribute Details

#clientClient (readonly)

Returns the value of attribute client.

Returns:



6
7
8
# File 'lib/max_api_client/api.rb', line 6

def client
  @client
end

#rawRawApi (readonly)

Returns the value of attribute raw.

Returns:



6
7
8
# File 'lib/max_api_client/api.rb', line 6

def raw
  @raw
end

#uploadUpload (readonly)

Returns the value of attribute upload.

Returns:



6
7
8
# File 'lib/max_api_client/api.rb', line 6

def upload
  @upload
end

Instance Method Details

#add_chat_members(chat_id, user_ids) ⇒ Object

Parameters:

  • chat_id (Integer)
  • user_ids (Array[Integer])

Returns:

  • (Object)


70
71
72
# File 'lib/max_api_client/api.rb', line 70

def add_chat_members(chat_id, user_ids)
  raw.chats.add_chat_members(chat_id:, user_ids:)
end

#answer_on_callback(callback_id, **extra) ⇒ Object

Parameters:

  • callback_id (String)
  • extra (Object)

Returns:

  • (Object)


126
127
128
# File 'lib/max_api_client/api.rb', line 126

def answer_on_callback(callback_id, **extra)
  raw.messages.answer_on_callback(callback_id:, **extra)
end

#delete_message(message_id, **extra) ⇒ Object

Parameters:

  • message_id (String)
  • extra (Object)

Returns:

  • (Object)


122
123
124
# File 'lib/max_api_client/api.rb', line 122

def delete_message(message_id, **extra)
  raw.messages.delete(message_id:, **extra)
end

#delete_my_commandsObject

rubocop:enable Naming/AccessorMethodName

Returns:

  • (Object)


42
43
44
# File 'lib/max_api_client/api.rb', line 42

def delete_my_commands
  edit_my_info(commands: [])
end

#edit_chat_info(chat_id, **extra) ⇒ Object

Parameters:

  • chat_id (Integer)
  • extra (Object)

Returns:

  • (Object)


58
59
60
# File 'lib/max_api_client/api.rb', line 58

def edit_chat_info(chat_id, **extra)
  raw.chats.edit(chat_id:, **extra)
end

#edit_message(message_id, **extra) ⇒ Object

Parameters:

  • message_id (String)
  • extra (Object)

Returns:

  • (Object)


118
119
120
# File 'lib/max_api_client/api.rb', line 118

def edit_message(message_id, **extra)
  raw.messages.edit(message_id:, **extra)
end

#edit_my_info(**extra) ⇒ Object

rubocop:enable Naming/AccessorMethodName

Parameters:

  • extra (Object)

Returns:

  • (Object)


32
33
34
# File 'lib/max_api_client/api.rb', line 32

def edit_my_info(**extra)
  raw.bots.edit_my_info(**extra)
end

#get_all_chats(**extra) ⇒ Object

Parameters:

  • extra (Object)

Returns:

  • (Object)


46
47
48
# File 'lib/max_api_client/api.rb', line 46

def get_all_chats(**extra)
  raw.chats.get_all(**extra)
end

#get_chat(chat_id) ⇒ Object

Parameters:

  • chat_id (Integer)

Returns:

  • (Object)


50
51
52
# File 'lib/max_api_client/api.rb', line 50

def get_chat(chat_id)
  raw.chats.get_by_id(chat_id:)
end

#get_chat_admins(chat_id) ⇒ Object

Parameters:

  • chat_id (Integer)

Returns:

  • (Object)


66
67
68
# File 'lib/max_api_client/api.rb', line 66

def get_chat_admins(chat_id)
  raw.chats.get_chat_admins(chat_id:)
end

Parameters:

  • chat_link (String)

Returns:

  • (Object)


54
55
56
# File 'lib/max_api_client/api.rb', line 54

def get_chat_by_link(chat_link)
  raw.chats.get_by_link(chat_link:)
end

#get_chat_members(chat_id, **extra) ⇒ Object

Parameters:

  • chat_id (Integer)
  • extra (Object)

Returns:

  • (Object)


74
75
76
# File 'lib/max_api_client/api.rb', line 74

def get_chat_members(chat_id, **extra)
  raw.chats.get_chat_members(chat_id:, **csv_query(extra, :user_ids))
end

#get_chat_membership(chat_id) ⇒ Object

Parameters:

  • chat_id (Integer)

Returns:

  • (Object)


62
63
64
# File 'lib/max_api_client/api.rb', line 62

def get_chat_membership(chat_id)
  raw.chats.get_chat_membership(chat_id:)
end

#get_message(message_id) ⇒ Object

Parameters:

  • message_id (String)

Returns:

  • (Object)


114
115
116
# File 'lib/max_api_client/api.rb', line 114

def get_message(message_id)
  raw.messages.get_by_id(message_id:)
end

#get_messages(chat_id, **extra) ⇒ Object

Parameters:

  • chat_id (Integer)
  • extra (Object)

Returns:

  • (Object)


110
111
112
# File 'lib/max_api_client/api.rb', line 110

def get_messages(chat_id, **extra)
  raw.messages.get(chat_id:, **csv_query(extra, :message_ids))
end

#get_my_infoObject

rubocop:disable Naming/AccessorMethodName

Returns:

  • (Object)


27
28
29
# File 'lib/max_api_client/api.rb', line 27

def get_my_info
  raw.bots.get_my_info
end

#get_pinned_message(chat_id) ⇒ Object

Parameters:

  • chat_id (Integer)

Returns:

  • (Object)


82
83
84
# File 'lib/max_api_client/api.rb', line 82

def get_pinned_message(chat_id)
  raw.chats.get_pinned_message(chat_id:)
end

#get_subscriptionsObject

rubocop:disable Naming/AccessorMethodName

Returns:

  • (Object)


131
132
133
# File 'lib/max_api_client/api.rb', line 131

def get_subscriptions
  raw.subscriptions.get_subscriptions
end

#leave_chat(chat_id) ⇒ Object

Parameters:

  • chat_id (Integer)

Returns:

  • (Object)


98
99
100
# File 'lib/max_api_client/api.rb', line 98

def leave_chat(chat_id)
  raw.chats.leave_chat(chat_id:)
end

#pin_message(chat_id, message_id, **extra) ⇒ Object

Parameters:

  • chat_id (Integer)
  • message_id (String)
  • extra (Object)

Returns:

  • (Object)


86
87
88
# File 'lib/max_api_client/api.rb', line 86

def pin_message(chat_id, message_id, **extra)
  raw.chats.pin_message(chat_id:, message_id:, notify: extra[:notify])
end

#poll_updates(types = [], marker: nil, timeout: Polling::DEFAULT_TIMEOUT, retry_interval: Polling::DEFAULT_RETRY_INTERVAL, read_timeout: nil) {|update| ... } ⇒ Polling

Parameters:

  • types (Array[String], String) (defaults to: [])
  • marker: (Integer, nil) (defaults to: nil)
  • timeout: (Integer) (defaults to: Polling::DEFAULT_TIMEOUT)
  • retry_interval: (Integer) (defaults to: Polling::DEFAULT_RETRY_INTERVAL)
  • read_timeout: (Numeric, nil) (defaults to: nil)

Yields:

Yield Parameters:

  • update (Object)

Yield Returns:

  • (void)

Returns:



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/max_api_client/api.rb', line 144

def poll_updates(types = [], marker: nil, timeout: Polling::DEFAULT_TIMEOUT,
                 retry_interval: Polling::DEFAULT_RETRY_INTERVAL, read_timeout: nil, &block)
  poller = Polling.new(
    self,
    types:,
    marker:,
    timeout:,
    retry_interval:,
    read_timeout:
  )

  return poller.each unless block

  poller.each(&block)
end

#remove_chat_member(chat_id, user_id, block: nil) ⇒ Object

Parameters:

  • chat_id (Integer)
  • user_id (Integer)
  • block: (Boolean, nil) (defaults to: nil)

Returns:

  • (Object)


78
79
80
# File 'lib/max_api_client/api.rb', line 78

def remove_chat_member(chat_id, user_id, block: nil)
  raw.chats.remove_chat_member(chat_id:, user_id:, block:)
end

#send_action(chat_id, action) ⇒ Object

Parameters:

  • chat_id (Integer)
  • action (String, Symbol)

Returns:

  • (Object)


94
95
96
# File 'lib/max_api_client/api.rb', line 94

def send_action(chat_id, action)
  raw.chats.send_action(chat_id:, action:)
end

#send_message_to_chat(chat_id, text, **extra) ⇒ Object

Parameters:

  • chat_id (Integer)
  • text (String)
  • extra (Object)

Returns:

  • (Object)


102
103
104
# File 'lib/max_api_client/api.rb', line 102

def send_message_to_chat(chat_id, text, **extra)
  message_from(raw.messages.send(chat_id:, text:, **extra))
end

#send_message_to_user(user_id, text, **extra) ⇒ Object

Parameters:

  • user_id (Integer)
  • text (String)
  • extra (Object)

Returns:

  • (Object)


106
107
108
# File 'lib/max_api_client/api.rb', line 106

def send_message_to_user(user_id, text, **extra)
  message_from(raw.messages.send(user_id:, text:, **extra))
end

#set_my_commands(commands) ⇒ Object

rubocop:disable Naming/AccessorMethodName

Parameters:

  • commands (Array[bot_command])

Returns:

  • (Object)


37
38
39
# File 'lib/max_api_client/api.rb', line 37

def set_my_commands(commands)
  edit_my_info(commands:)
end

#subscribe(url, update_types: nil, secret: nil) ⇒ Object

rubocop:enable Naming/AccessorMethodName

Parameters:

  • url (String)
  • update_types: (Array[String], String, nil) (defaults to: nil)
  • secret: (String, nil) (defaults to: nil)

Returns:

  • (Object)


136
137
138
# File 'lib/max_api_client/api.rb', line 136

def subscribe(url, update_types: nil, secret: nil)
  raw.subscriptions.subscribe(url:, update_types:, secret:)
end

#unpin_message(chat_id) ⇒ Object

Parameters:

  • chat_id (Integer)

Returns:

  • (Object)


90
91
92
# File 'lib/max_api_client/api.rb', line 90

def unpin_message(chat_id)
  raw.chats.unpin_message(chat_id:)
end

#unsubscribe(url) ⇒ Object

Parameters:

  • url (String)

Returns:

  • (Object)


140
141
142
# File 'lib/max_api_client/api.rb', line 140

def unsubscribe(url)
  raw.subscriptions.unsubscribe(url:)
end

#upload_audio(options) ⇒ AudioAttachment

Parameters:

  • options (Hash[Symbol, untyped])

Returns:



170
171
172
173
# File 'lib/max_api_client/api.rb', line 170

def upload_audio(options)
  data = upload.audio(**options)
  AudioAttachment.new(token: data[:token] || data["token"])
end

#upload_file(options) ⇒ FileAttachment

Parameters:

  • options (Hash[Symbol, untyped])

Returns:



175
176
177
178
# File 'lib/max_api_client/api.rb', line 175

def upload_file(options)
  data = upload.file(**options)
  FileAttachment.new(token: data[:token] || data["token"])
end

#upload_image(options) ⇒ ImageAttachment

Parameters:

  • options (Hash[Symbol, untyped])

Returns:



160
161
162
163
# File 'lib/max_api_client/api.rb', line 160

def upload_image(options)
  data = upload.image(**options)
  ImageAttachment.new(token: data[:token], photos: data[:photos], url: data[:url] || data["url"])
end

#upload_video(options) ⇒ VideoAttachment

Parameters:

  • options (Hash[Symbol, untyped])

Returns:



165
166
167
168
# File 'lib/max_api_client/api.rb', line 165

def upload_video(options)
  data = upload.video(**options)
  VideoAttachment.new(token: data[:token] || data["token"])
end