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) ⇒ 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.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/whatsapp_sdk/api/templates.rb', line 34

def create(
  business_id:, name:, category:, language:, components_json: nil, allow_category_change: 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

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

  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.



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/whatsapp_sdk/api/templates.rb', line 146

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_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.



95
96
97
98
99
100
101
102
103
# File 'lib/whatsapp_sdk/api/templates.rb', line 95

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/whatsapp_sdk/api/templates.rb', line 68

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['paging']['cursors']['before'],
    after: response['paging']['cursors']['after']
  )
end

#templates(business_id:) ⇒ Object



85
86
87
88
# File 'lib/whatsapp_sdk/api/templates.rb', line 85

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.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/whatsapp_sdk/api/templates.rb', line 115

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