Class: Whatsapp::MessageTemplates::Template

Inherits:
Object
  • Object
show all
Includes:
ValueObject
Defined in:
lib/ruby/whatsapp/message_templates/template.rb

Overview

The payload for creating a template, and for the multi-language upsert form.

Owns the template's identity — name, language, category and the flags that apply to the whole template. Everything about the components, including the rules that span several of them, is delegated to ComponentSet; this class only passes the category and parameter format down so those rules can be applied.

Supports two language forms, which is why they are validated as an exclusive choice: a single language for #create, or a languages array for #upsert, which creates or updates the same template across several locales in one call.

Cloning a pre-written template from Meta's library is a different payload with no components at all — see LibraryTemplate. Source: https://developers.facebook.com/docs/graph-api/reference/whats-app-business-account/message_templates/

Defined Under Namespace

Modules: Defaults, SubCategories

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ValueObject

included

Constructor Details

#initialize(name:, category:, components:, language: nil, languages: nil, parameter_format: ParameterFormats::POSITIONAL, sub_category: nil, message_send_ttl_seconds: nil, allow_category_change: nil, cta_url_link_tracking_opted_out: nil) ⇒ Template

Returns a new instance of Template.

Parameters:

  • name (String)

    The template name; lowercase alphanumerics and underscores.

  • category (String, Symbol)
  • components (Array<Hash, Component::Base>)

    The template's components.

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

    A single locale code. Mutually exclusive with languages.

  • languages (Array<String>, nil) (defaults to: nil)

    Locale codes, for the upsert form.

  • parameter_format (String, Symbol) (defaults to: ParameterFormats::POSITIONAL)

    POSITIONAL (default) or NAMED.

  • sub_category (String, nil) (defaults to: nil)
  • message_send_ttl_seconds (Integer, nil) (defaults to: nil)

    Delivery-retry TTL override.

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

    See the attribute note.

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

    Opt out of link tracking. @raise [ActiveModel::ValidationError] if validation fails. @raise [TemplateError] if a component or button type is unknown.



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 105

def initialize(name:, category:, components:, language: nil, languages: nil,
  parameter_format: ParameterFormats::POSITIONAL, sub_category: nil,
  message_send_ttl_seconds: nil, allow_category_change: nil,
  cta_url_link_tracking_opted_out: nil)
  @name = name
  @language = language
  @languages = languages
  @category = Categories.normalize(category)
  @parameter_format = ParameterFormats.normalize(parameter_format)
  @sub_category = sub_category
  @message_send_ttl_seconds = message_send_ttl_seconds
  @allow_category_change = allow_category_change
  @cta_url_link_tracking_opted_out = cta_url_link_tracking_opted_out
  @component_set = ComponentSet.new(components:, category: @category, parameter_format: @parameter_format)

  validate!
end

Instance Attribute Details

#allow_category_changeBoolean?

Effectively a no-op since 2025-04-09, when Meta made automatic recategorisation the default regardless of this flag. Kept because it is still a documented parameter.

Returns:

  • (Boolean, nil)


75
76
77
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 75

def allow_category_change
  @allow_category_change
end

#categoryString

Returns:

  • (String)


52
53
54
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 52

def category
  @category
end

#component_setComponentSet

Returns:



60
61
62
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 60

def component_set
  @component_set
end

Returns:

  • (Boolean, nil)


79
80
81
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 79

def cta_url_link_tracking_opted_out
  @cta_url_link_tracking_opted_out
end

#languageString?

Returns A single locale code, for create.

Returns:

  • (String, nil)

    A single locale code, for create.



44
45
46
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 44

def language
  @language
end

#languagesArray<String>?

Returns Several locale codes, for upsert.

Returns:

  • (Array<String>, nil)

    Several locale codes, for upsert.



48
49
50
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 48

def languages
  @languages
end

#message_send_ttl_secondsInteger?

Returns:

  • (Integer, nil)


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

def message_send_ttl_seconds
  @message_send_ttl_seconds
end

#nameString

Returns:

  • (String)


40
41
42
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 40

def name
  @name
end

#parameter_formatString

Returns:

  • (String)


56
57
58
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 56

def parameter_format
  @parameter_format
end

#sub_categoryString?

Returns:

  • (String, nil)


64
65
66
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 64

def sub_category
  @sub_category
end

Instance Method Details

#componentsArray<Component::Base>

Returns:



124
125
126
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 124

def components
  component_set.components
end

#serializeHash

Returns The create/upsert payload.

Returns:

  • (Hash)

    The create/upsert payload.



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ruby/whatsapp/message_templates/template.rb', line 129

def serialize
  {
    name:,
    **language_payload,
    category:,
    parameter_format:,
    components: component_set.serialize,
    sub_category:,
    message_send_ttl_seconds:,
    allow_category_change:,
    cta_url_link_tracking_opted_out:,
  }.compact
end