Class: Whatsapp::MessageTemplates::Component::Body

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/message_templates/component/body.rb

Overview

The template's message text. The only component Meta requires.

Two mutually exclusive shapes, hence the XOR validation:

standard       — `text` (1024 chars) with any number of placeholders, plus a
               matching `example`
authentication — no text at all. Meta supplies the fixed, localised string
               "<VERIFICATION_CODE> is your verification code." and
               `add_security_recommendation` only decides whether the
               "do not share this code" line is appended.

The tighter 600-character limit that applies inside a limited-time-offer template is enforced by Template, which is the only object that can see whether such a component is present. Source: https://developers.facebook.com/docs/whatsapp/business-management-api/message-templates/components/

Defined Under Namespace

Modules: Defaults

Instance Attribute Summary collapse

Attributes inherited from Base

#example_error, #parameter_format

Instance Method Summary collapse

Methods inherited from Base

#api_type

Methods included from ValueObject

included

Constructor Details

#initialize(text: nil, example: nil, add_security_recommendation: nil) ⇒ Body

Returns a new instance of Body.

Parameters:

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

    The body text (max 1024 characters).

  • example (Array, Hash, String, nil) (defaults to: nil)

    Sample values for the placeholders, or a pre-built example payload.

  • add_security_recommendation (Boolean, nil) (defaults to: nil)

    Authentication templates only; mutually exclusive with text.

  • kwargs (Hash)

    Forwarded to Whatsapp::MessageTemplates::Component::Base (parameter_format). @raise [ActiveModel::ValidationError] if validation fails.



56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby/whatsapp/message_templates/component/body.rb', line 56

def initialize(text: nil, example: nil, add_security_recommendation: nil, **)
  super(**)

  @text = text
  @add_security_recommendation = add_security_recommendation
  @example_values = example
  @example = build_example_payload(role: :body, values: example)

  validate!
end

Instance Attribute Details

#add_security_recommendationBoolean?

Returns Authentication templates only.

Returns:

  • (Boolean, nil)

    Authentication templates only.



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

def add_security_recommendation
  @add_security_recommendation
end

#exampleHash?

Returns The serialized example payload.

Returns:

  • (Hash, nil)

    The serialized example payload.



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

def example
  @example
end

#example_valuesArray, ...

The raw example input, kept so the placeholder/example counts can be compared without re-parsing the serialized payload.

Returns:

  • (Array, Hash, String, nil)


43
44
45
# File 'lib/ruby/whatsapp/message_templates/component/body.rb', line 43

def example_values
  @example_values
end

#textString?

Returns:

  • (String, nil)


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

def text
  @text
end

Instance Method Details

#authentication?Boolean

Returns Whether this is the flag-based authentication shape.

Returns:

  • (Boolean)

    Whether this is the flag-based authentication shape.



68
69
70
# File 'lib/ruby/whatsapp/message_templates/component/body.rb', line 68

def authentication?
  !add_security_recommendation.nil?
end

#serializeHash

Returns The serialized component.

Returns:

  • (Hash)

    The serialized component.



73
74
75
76
77
78
79
80
81
# File 'lib/ruby/whatsapp/message_templates/component/body.rb', line 73

def serialize
  return { type: Defaults::TYPE, add_security_recommendation: } if authentication?

  {
    type: Defaults::TYPE,
    text:,
    example:,
  }.compact
end