Class: Whatsapp::Messages::Audio

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/messages/audio.rb

Overview

Audio messages allow you to send an audio file to a WhatsApp user. Source: https://developers.facebook.com/documentation/business-messaging/whatsapp/messages/audio-messages

Audio messages do not support captions. To send audio you must either:

- Upload the audio via Whatsapp::Media#upload to obtain a media_id, then pass it as `id`
- Pass a publicly hosted URL as `link` (not recommended by Meta)

Defined Under Namespace

Modules: Defaults

Instance Attribute Summary collapse

Attributes inherited from Base

#to

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, link: nil) ⇒ Audio

Returns a new instance of Audio.

Parameters:

  • id (String, nil) (defaults to: nil)

    ID of the uploaded media asset (from Media#upload).

  • link (String, nil) (defaults to: nil)

    URL of the audio file hosted on a public server.

  • kwargs (Hash)

    Additional keyword arguments passed to Base (:to). @raise [ActiveModel::ValidationError] if validation fails.



30
31
32
33
34
35
36
37
# File 'lib/ruby/whatsapp/messages/audio.rb', line 30

def initialize(id: nil, link: nil, **)
  super(**)

  @id = id
  @link = link

  validate!
end

Instance Attribute Details

#idString?

Returns:

  • (String, nil)


18
19
20
# File 'lib/ruby/whatsapp/messages/audio.rb', line 18

def id
  @id
end

Returns:

  • (String, nil)


22
23
24
# File 'lib/ruby/whatsapp/messages/audio.rb', line 22

def link
  @link
end

Instance Method Details

#serializeHash

Serializes the audio message to a hash format suitable for the WhatsApp API.

Returns:

  • (Hash)

    The serialized audio message.



41
42
43
# File 'lib/ruby/whatsapp/messages/audio.rb', line 41

def serialize
  envelope(type: Defaults::TYPE, audio: audio_payload)
end