Class: Whatsapp::MessageTemplates::LibraryTemplate

Inherits:
Object
  • Object
show all
Includes:
ValueObject
Defined in:
lib/ruby/whatsapp/message_templates/library_template.rb,
lib/ruby/whatsapp/message_templates/library_template/body_inputs.rb,
lib/ruby/whatsapp/message_templates/library_template/button_inputs.rb

Overview

The payload for cloning one of Meta's pre-written library templates.

A separate class from Template rather than another mode on it, because this payload carries no components at all — the library template already defines them, and only the parts Meta cannot know (a URL, a phone number, whether to append an optional line) are supplied as inputs. Folding that into Template would mean defeating its central "exactly one BODY" invariant.

The payoff is instant approval: library templates are already categorised and reviewed, so the create response usually comes back APPROVED rather than PENDING. Source: https://developers.facebook.com/documentation/business-messaging/whatsapp/templates/template-library

Defined Under Namespace

Classes: BodyInputs, ButtonInputs

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueObject

included

Constructor Details

#initialize(name:, language:, category:, library_template_name:, library_template_body_inputs: nil, library_template_button_inputs: nil) ⇒ LibraryTemplate

@raise [ActiveModel::ValidationError] if validation fails.

Parameters:

  • name (String)

    The name for your copy; same rules as Template.

  • language (String)

    A locale code.

  • category (String, Symbol)
  • library_template_name (String)

    The library template to clone.

  • library_template_body_inputs (Hash, BodyInputs, nil) (defaults to: nil)

    Optional body toggles.

  • library_template_button_inputs (Array<Hash, ButtonInputs>, nil) (defaults to: nil)

    Optional per-button values.



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 60

def initialize(name:, language:, category:, library_template_name:,
  library_template_body_inputs: nil, library_template_button_inputs: nil)
  @name = name
  @language = language
  @category = Categories.normalize(category)
  @library_template_name = library_template_name
  @library_template_body_inputs = build_body_inputs(library_template_body_inputs)
  @library_template_button_inputs = build_button_inputs(library_template_button_inputs)

  validate!
end

Instance Attribute Details

#categoryString

Returns:

  • (String)


29
30
31
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 29

def category
  @category
end

#languageString

Returns:

  • (String)


25
26
27
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 25

def language
  @language
end

#library_template_body_inputsLibraryTemplate::BodyInputs?

Returns:



37
38
39
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 37

def library_template_body_inputs
  @library_template_body_inputs
end

#library_template_button_inputsArray<LibraryTemplate::ButtonInputs>?

Returns:



41
42
43
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 41

def library_template_button_inputs
  @library_template_button_inputs
end

#library_template_nameString

Returns The library template being cloned.

Returns:

  • (String)

    The library template being cloned.



33
34
35
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 33

def library_template_name
  @library_template_name
end

#nameString

Returns The name to give your copy of the template.

Returns:

  • (String)

    The name to give your copy of the template.



21
22
23
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 21

def name
  @name
end

Instance Method Details

#serializeHash

Note: Meta's docs show library_template_button_inputs as a JSON-stringified array while the Graph API reference types it array<JSON object>. A real array is emitted here, which matches the reference and how every other field on this edge behaves. If the API rejects it, this method is the single place to change.

Returns:

  • (Hash)

    The create-from-library payload.



78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby/whatsapp/message_templates/library_template.rb', line 78

def serialize
  {
    name:,
    language:,
    category:,
    library_template_name:,
    library_template_body_inputs: library_template_body_inputs&.serialize,
    library_template_button_inputs: library_template_button_inputs&.map(&:serialize),
  }.compact
end