Class: GrubY::NTgCalls::MusicBot

Inherits:
Object
  • Object
show all
Defined in:
lib/gruubY/ntgcalls.rb

Constant Summary collapse

DEFAULT_AUTH_TIMEOUT =
180

Instance Method Summary collapse

Constructor Details

#initialize(td_user_session:, tdjson_path: nil, ntgcalls_path: nil, phone_number: nil, td_verbosity: 1) ⇒ MusicBot

Returns a new instance of MusicBot.



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'lib/gruubY/ntgcalls.rb', line 188

def initialize(
  td_user_session:,
  tdjson_path: nil,
  ntgcalls_path: nil,
  phone_number: nil,
  td_verbosity: 1
)
  td_cfg = GrubY::TDLib::UserSession.client_kwargs(td_user_session)
  @td = GrubY::TDLib::Client.new(
    **td_cfg,
    phone_number: phone_number,
    tdjson_path: tdjson_path,
    td_verbosity: td_verbosity,
    workers: 2
  )
  @ntg = Client.new(lib_path: ntgcalls_path)
  @joined_chat_id = nil
  @phone_number = phone_number
  @td.on("updateAuthorizationState") { |update| handle_auth_state(update) }
end

Instance Method Details

#cpu_usageObject



303
304
305
# File 'lib/gruubY/ntgcalls.rb', line 303

def cpu_usage
  @ntg.cpu_usage
end

#data_json(value) ⇒ Object



255
256
257
# File 'lib/gruubY/ntgcalls.rb', line 255

def data_json(value)
  GrubY::RawTypes.to_data_json(value)
end

#input_group_call(chat:) ⇒ Object



246
247
248
249
# File 'lib/gruubY/ntgcalls.rb', line 246

def input_group_call(chat:)
  chat_id = resolve_chat_id(chat)
  fetch_input_group_call(chat_id)
end

#input_peer_selfObject



251
252
253
# File 'lib/gruubY/ntgcalls.rb', line 251

def input_peer_self
  GrubY::RawTypes.input_peer_self
end

#join_and_play(chat:, audio:, invite_hash: "") ⇒ Object

Raises:



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/gruubY/ntgcalls.rb', line 220

def join_and_play(chat:, audio:, invite_hash: "")
  chat_id = resolve_chat_id(chat)
  input_group_call = fetch_input_group_call(chat_id)
  local_offer = @ntg.create(chat_id: chat_id)
  audio_source_id = deterministic_audio_source_id(chat_id)

  join_response = try_join_group_call(
    input_group_call: input_group_call,
    local_offer: local_offer,
    audio_source_id: audio_source_id,
    invite_hash: invite_hash
  )
  remote_params = extract_join_payload(join_response)
  raise Error, "joinGroupCall response did not contain tgcalls params" if remote_params.to_s.empty?

  @ntg.connect(chat_id: chat_id, params: remote_params)
  @ntg.play(chat_id: chat_id, stream: audio)
  @joined_chat_id = chat_id

  {
    chat_id: chat_id,
    offer: local_offer,
    remote_params: remote_params
  }
end

#join_group_call(chat:, payload:, audio_source_id:, muted: false, video_stopped: true, invite_hash: nil) ⇒ Object



259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/gruubY/ntgcalls.rb', line 259

def join_group_call(chat:, payload:, audio_source_id:, muted: false, video_stopped: true, invite_hash: nil)
  call = input_group_call(chat: chat)
  params = data_json(payload)
  query = GrubY::RawTypes.join_group_call(
    call: call,
    params: params,
    muted: muted,
    video_stopped: video_stopped,
    invite_hash: invite_hash,
    join_as: input_peer_self
  )
  GrubY::Raw.td_call!(@td, mtproto_to_td_join_query(query, audio_source_id: audio_source_id))
end

#leave_group_call(chat:, source:) ⇒ Object



273
274
275
276
277
# File 'lib/gruubY/ntgcalls.rb', line 273

def leave_group_call(chat:, source:)
  call = input_group_call(chat: chat)
  query = GrubY::RawTypes.leave_group_call(call: call, source: source)
  GrubY::Raw.td_call!(@td, mtproto_to_td_leave_query(query))
end

#muteObject



291
292
293
# File 'lib/gruubY/ntgcalls.rb', line 291

def mute
  @ntg.mute(chat_id: joined_chat_id!)
end

#pauseObject



283
284
285
# File 'lib/gruubY/ntgcalls.rb', line 283

def pause
  @ntg.pause(chat_id: joined_chat_id!)
end

#resumeObject



287
288
289
# File 'lib/gruubY/ntgcalls.rb', line 287

def resume
  @ntg.resume(chat_id: joined_chat_id!)
end

#start(timeout: DEFAULT_AUTH_TIMEOUT) ⇒ Object



209
210
211
212
213
# File 'lib/gruubY/ntgcalls.rb', line 209

def start(timeout: DEFAULT_AUTH_TIMEOUT)
  @td.start
  wait_until_authorized!(timeout: timeout)
  self
end

#stopObject



215
216
217
218
# File 'lib/gruubY/ntgcalls.rb', line 215

def stop
  @ntg.close
  @td.stop
end

#stop_callObject



299
300
301
# File 'lib/gruubY/ntgcalls.rb', line 299

def stop_call
  @ntg.stop_call(chat_id: joined_chat_id!)
end

#timeObject



307
308
309
# File 'lib/gruubY/ntgcalls.rb', line 307

def time
  @ntg.time(chat_id: joined_chat_id!)
end

#unmuteObject



295
296
297
# File 'lib/gruubY/ntgcalls.rb', line 295

def unmute
  @ntg.unmute(chat_id: joined_chat_id!)
end

#update_group_call(payload) ⇒ Object



279
280
281
# File 'lib/gruubY/ntgcalls.rb', line 279

def update_group_call(payload)
  GrubY::RawTypes.update_group_call(payload)
end