Class: Whatsapp::Messages::Image

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

Overview

Image messages display an image with an optional caption. Source: https://developers.facebook.com/documentation/business-messaging/whatsapp/messages/image-messages

To send an image you must either:

- Upload the image 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, caption: nil) ⇒ Image

Returns a new instance of Image.

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 image hosted on a public server.

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

    Image caption text (maximum 1024 characters).

  • kwargs (Hash)

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



36
37
38
39
40
41
42
43
44
# File 'lib/ruby/whatsapp/messages/image.rb', line 36

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

  @id = id
  @link = link
  @caption = caption

  validate!
end

Instance Attribute Details

#captionString?

Returns:

  • (String, nil)


26
27
28
# File 'lib/ruby/whatsapp/messages/image.rb', line 26

def caption
  @caption
end

#idString?

Returns:

  • (String, nil)


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

def id
  @id
end

Returns:

  • (String, nil)


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

def link
  @link
end

Instance Method Details

#serializeHash

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

Returns:

  • (Hash)

    The serialized image message.



48
49
50
# File 'lib/ruby/whatsapp/messages/image.rb', line 48

def serialize
  envelope(type: Defaults::TYPE, image: image_payload)
end