Class: WhatsappSdk::Api::Templates

Inherits:
Request
  • Object
show all
Defined in:
lib/whatsapp_sdk/api/templates.rb

Defined Under Namespace

Classes: InvalidCategoryError

Constant Summary collapse

DEFAULT_HEADERS =
{ 'Content-Type' => 'application/json' }.freeze

Instance Method Summary collapse

Methods inherited from Request

#download_file, #initialize, #send_request

Constructor Details

This class inherits a constructor from WhatsappSdk::Api::Request

Instance Method Details

#create(business_id:, name:, category:, language:, components_json: nil, allow_category_change: nil, parameter_format: nil) ⇒ Template

Create a template

Set to true to allow us to assign a category based on the template guidelines and the template's contents. This can prevent your template from being rejected for miscategorization.

Parameters:

Returns:

  • (Template)

    Template object.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/whatsapp_sdk/api/templates.rb', line 47

def create(
  business_id:, name:, category:, language:, components_json: nil, allow_category_change: nil,
  parameter_format: nil
)
  unless WhatsappSdk::Resource::Template::Category.valid?(category)
    raise InvalidCategoryError.new(category: category)
  end

  unless WhatsappSdk::Resource::Languages.available?(language)
    raise WhatsappSdk::Resource::Errors::InvalidLanguageError.new(language: language)
  end

  if parameter_format && !WhatsappSdk::Resource::ParameterObject::Format.valid?(parameter_format)
    raise WhatsappSdk::Resource::Errors::InvalidParameterFormatError.new(format: parameter_format)
  end

  params = {
    name: name,
    category: category,
    language: language,
    components: components_json
  }
  params["allow_category_change"] = allow_category_change if allow_category_change
  params["parameter_format"] = parameter_format if parameter_format

  response = send_request(
    endpoint: "#{business_id}/message_templates",
    http_method: "post",
    params: params,
    headers: DEFAULT_HEADERS
  )

  Resource::Template.from_hash(response)
end

#delete(business_id:, name:, hsm_id: nil) ⇒ Boolean

Delete Template

Deleting a template by name deletes all templates that match that name (meaning templates with the same name but different languages will also be deleted). To delete a template by ID, include the template's ID along with its name in your request; only the template with the matching template ID will be deleted.

Parameters:

  • business_id (Integer)

    Required. The business ID.

  • name (String)

    Required. The template's name.

  • hsm_id (String) (defaults to: nil)

    Optional. The template's id.

Returns:

  • (Boolean)

    Whether the deletion was successful.



165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/whatsapp_sdk/api/templates.rb', line 165

def delete(business_id:, name:, hsm_id: nil)
  params = { name: name }
  params[:hsm_id] = hsm_id if hsm_id

  response = send_request(
    endpoint: "#{business_id}/message_templates",
    http_method: "delete",
    params: params
  )

  Api::Responses::SuccessResponse.success_response?(response: response)
end

#get(template_id:) ⇒ Template

Get a template

Parameters:

  • template_id (String)

    Required. The template ID.

Returns:

  • (Template)

    Template object.



24
25
26
27
28
29
30
31
# File 'lib/whatsapp_sdk/api/templates.rb', line 24

def get(template_id:)
  response = send_request(
    endpoint: template_id,
    http_method: "get"
  )

  Resource::Template.from_hash(response)
end

#get_message_template_namespace(business_id:) ⇒ MessageTemplateNamespace

Get Message Template Namespace The message template namespace is required to send messages using the message templates.

Parameters:

  • business_id (Integer)

    The business ID.

Returns:

  • (MessageTemplateNamespace)

    MessageTemplateNamespace object.



114
115
116
117
118
119
120
121
122
# File 'lib/whatsapp_sdk/api/templates.rb', line 114

def get_message_template_namespace(business_id:)
  response = send_request(
    endpoint: business_id.to_s,
    http_method: "get",
    params: { "fields" => "message_template_namespace" }
  )

  WhatsappSdk::Resource::MessageTemplateNamespace.from_hash(response)
end

#list(business_id:, limit: 100) ⇒ Template

Get templates

Parameters:

  • business_id (Integer)

    The business ID.

  • limit (Integer) (defaults to: 100)

    Optional. Number of templates to return in a single page.

Returns:

  • (Template)

    Pagination Record containing an array of Template objects.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/whatsapp_sdk/api/templates.rb', line 87

def list(business_id:, limit: 100)
  params = {}
  params["limit"] = limit if limit

  response = send_request(
    endpoint: "#{business_id}/message_templates",
    http_method: "get",
    params: params
  )

  Api::Responses::PaginationRecords.new(
    records: parse_templates(response['data']),
    before: response.dig('paging', 'cursors', 'before'),
    after: response.dig('paging', 'cursors', 'after')
  )
end

#template_analytics(business_id:, start_timestamp:, end_timestamp:, template_ids:, metric_types: [], granularity: WhatsappSdk::Resource::TemplateAnalytic::Granularity::DAILY, after: nil) ⇒ Object

Get Template Analytics

Get analytics data for message templates over a specified time range.

Parameters:

  • business_id (Integer)

    The business ID.

  • start_timestamp (Integer)

    The start of the time range to retrieve analytics data, in Unix timestamp.

  • end_timestamp (Integer)

    The end of the time range to retrieve analytics data, in Unix timestamp.

  • template_ids (Array<String>)

    An array of template IDs for which to retrieve analytics data.

  • metric_types (Array<String>) (defaults to: [])

    An array of metric types to retrieve.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/whatsapp_sdk/api/templates.rb', line 186

def template_analytics(
  business_id:, start_timestamp:, end_timestamp:, template_ids:, metric_types: [],
  granularity: WhatsappSdk::Resource::TemplateAnalytic::Granularity::DAILY,
  after: nil
)
  if !metric_types.empty? && !valid_metric_types?(metric_types)
    valid_types = WhatsappSdk::Resource::TemplateAnalytic::MetricType::METRIC_TYPES.join(', ')

    raise ArgumentError, "Invalid metric type. Valid types are: #{valid_types}."
  end

  if granularity != WhatsappSdk::Resource::TemplateAnalytic::Granularity::DAILY
    raise ArgumentError, "Invalid granularity. The only supported granularity is DAILY."
  end

  query_params = {
    start: start_timestamp,
    end: end_timestamp,
    template_ids: template_ids.join(","),
    metric_types: metric_types.join(","),
    granularity: granularity
  }
  query_params[:after] = after if after

  response = send_request(
    endpoint: "#{business_id}/template_analytics?#{URI.encode_www_form(query_params)}",
    http_method: "get"
  )

  Api::Responses::PaginationRecords.new(
    records: parse_template_analytics(response['data']),
    before: response.dig('paging', 'cursors', 'before'),
    after: response.dig('paging', 'cursors', 'after')
  )
end

#templates(business_id:) ⇒ Object



104
105
106
107
# File 'lib/whatsapp_sdk/api/templates.rb', line 104

def templates(business_id:)
  warn "[DEPRECATION] `templates` is deprecated. Please use `list` instead."
  list(business_id: business_id)
end

#update(template_id:, category: nil, components_json: nil) ⇒ Boolean

Edit Template

Editing a template replaces its old contents entirely, so include any components you wish to preserve as well as components you wish to update using the components parameter.

Message templates can only be edited when in an Approved, Rejected, or Paused state.

Parameters:

  • id (String)

    Required. The message_template-id.

  • components_json (Json) (defaults to: nil)

    Components that make up the template..

Returns:

  • (Boolean)

    Whether the update was successful.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/whatsapp_sdk/api/templates.rb', line 134

def update(template_id:, category: nil, components_json: nil)
  if category && !WhatsappSdk::Resource::Template::Category.valid?(category)
    raise InvalidCategoryError.new(category: category)
  end

  params = {}
  params[:components] = components_json if components_json
  params[:category] = category if category

  response = send_request(
    endpoint: template_id.to_s,
    http_method: "post",
    params: params,
    headers: { "Content-Type" => "application/json" }
  )

  Api::Responses::SuccessResponse.success_response?(response: response)
end