Class: Whatsapp::Messages::Document

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

Overview

Document messages allow you to send a document file with an optional caption and custom filename. Source: https://developers.facebook.com/documentation/business-messaging/whatsapp/messages/document-messages

To send a document you must either:

- Upload the document 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, filename: nil) ⇒ Document

Returns a new instance of Document.

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

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

    Document caption text (maximum 1024 characters).

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

    Custom filename displayed in the chat (e.g. "invoice.pdf").

  • kwargs (Hash)

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



41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby/whatsapp/messages/document.rb', line 41

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

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

  validate!
end

Instance Attribute Details

#captionString?

Returns:

  • (String, nil)


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

def caption
  @caption
end

#filenameString?

Returns:

  • (String, nil)


30
31
32
# File 'lib/ruby/whatsapp/messages/document.rb', line 30

def filename
  @filename
end

#idString?

Returns:

  • (String, nil)


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

def id
  @id
end

Returns:

  • (String, nil)


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

def link
  @link
end

Instance Method Details

#serializeHash

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

Returns:

  • (Hash)

    The serialized document message.



54
55
56
# File 'lib/ruby/whatsapp/messages/document.rb', line 54

def serialize
  envelope(type: Defaults::TYPE, document: document_payload)
end