Class: KapsoClientRuby::Resources::Templates

Inherits:
Object
  • Object
show all
Defined in:
lib/kapso_client_ruby/resources/templates.rb

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Templates

Returns a new instance of Templates.



6
7
8
# File 'lib/kapso_client_ruby/resources/templates.rb', line 6

def initialize(client)
  @client = client
end

Instance Method Details

#build_authentication_template(name:, language:, ttl_seconds: 60, add_security_recommendation: true, code_expiration_minutes: 10, otp_type: 'COPY_CODE') ⇒ Object

Authentication template builder



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/kapso_client_ruby/resources/templates.rb', line 161

def build_authentication_template(name:, language:, ttl_seconds: 60, 
                                  add_security_recommendation: true,
                                  code_expiration_minutes: 10,
                                  otp_type: 'COPY_CODE')
  components = []
  
  # Body component with security recommendation

  body_component = { type: 'BODY' }
  body_component[:add_security_recommendation] = add_security_recommendation
  components << body_component
  
  # Footer component with expiration

  if code_expiration_minutes
    components << build_footer_component(code_expiration_minutes: code_expiration_minutes)
  end
  
  # OTP button

  components << build_buttons_component(
    buttons: [build_button(type: 'OTP', otp_type: otp_type)]
  )
  
  {
    name: name,
    language: language,
    category: 'AUTHENTICATION',
    message_send_ttl_seconds: ttl_seconds,
    components: components
  }
end

#build_button(type:, text: nil, url: nil, phone_number: nil, otp_type: nil, autofill_text: nil, package_name: nil, signature_hash: nil) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/kapso_client_ruby/resources/templates.rb', line 135

def build_button(type:, text: nil, url: nil, phone_number: nil, 
                 otp_type: nil, autofill_text: nil, package_name: nil, 
                 signature_hash: nil)
  button = { type: type.upcase }
  
  case type.upcase
  when 'QUICK_REPLY'
    button[:text] = text if text
  when 'URL'
    button[:text] = text if text
    button[:url] = url if url
  when 'PHONE_NUMBER'
    button[:text] = text if text
    button[:phone_number] = phone_number if phone_number
  when 'OTP'
    button[:otp_type] = otp_type if otp_type
    button[:text] = text if text
    button[:autofill_text] = autofill_text if autofill_text
    button[:package_name] = package_name if package_name
    button[:signature_hash] = signature_hash if signature_hash
  end
  
  button
end

#build_buttons_component(buttons:) ⇒ Object



128
129
130
131
132
133
# File 'lib/kapso_client_ruby/resources/templates.rb', line 128

def build_buttons_component(buttons:)
  {
    type: 'BUTTONS',
    buttons: buttons.map { |btn| normalize_button(btn) }
  }
end


121
122
123
124
125
126
# File 'lib/kapso_client_ruby/resources/templates.rb', line 121

def build_footer_component(text: nil, code_expiration_minutes: nil)
  component = { type: 'FOOTER' }
  component[:text] = text if text
  component[:code_expiration_minutes] = code_expiration_minutes if code_expiration_minutes
  component
end

#build_header_component(type:, text: nil, image: nil, video: nil, document: nil, example: nil) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/kapso_client_ruby/resources/templates.rb', line 102

def build_header_component(type:, text: nil, image: nil, video: nil, 
                           document: nil, example: nil)
  component = { type: 'HEADER', format: type.upcase }
  
  case type.upcase
  when 'TEXT'
    component[:text] = text if text
  when 'IMAGE'
    component[:example] = { header_handle: [image] } if image
  when 'VIDEO'
    component[:example] = { header_handle: [video] } if video
  when 'DOCUMENT'
    component[:example] = { header_handle: [document] } if document
  end
  
  component[:example] = example if example
  component
end

#build_marketing_template(name:, language:, header: nil, body:, footer: nil, buttons: nil, body_example: nil) ⇒ Object

Marketing template builder



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/kapso_client_ruby/resources/templates.rb', line 192

def build_marketing_template(name:, language:, header: nil, body:, footer: nil, 
                             buttons: nil, body_example: nil)
  components = []
  
  # Header component

  components << header if header
  
  # Body component

  body_component = build_text_component(text: body, example: body_example)
  components << body_component
  
  # Footer component

  components << build_footer_component(text: footer) if footer
  
  # Buttons component

  components << build_buttons_component(buttons: buttons) if buttons
  
  {
    name: name,
    language: language,
    category: 'MARKETING',
    components: components
  }
end

#build_text_component(text:, example: nil) ⇒ Object

Template builder helpers



96
97
98
99
100
# File 'lib/kapso_client_ruby/resources/templates.rb', line 96

def build_text_component(text:, example: nil)
  component = { type: 'BODY', text: text }
  component[:example] = example if example
  component
end

#build_utility_template(name:, language:, body:, header: nil, footer: nil, buttons: nil, body_example: nil) ⇒ Object

Utility template builder



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/kapso_client_ruby/resources/templates.rb', line 218

def build_utility_template(name:, language:, body:, header: nil, footer: nil, 
                           buttons: nil, body_example: nil)
  components = []
  
  # Header component

  components << header if header
  
  # Body component

  body_component = build_text_component(text: body, example: body_example)
  components << body_component
  
  # Footer component

  components << build_footer_component(text: footer) if footer
  
  # Buttons component

  components << build_buttons_component(buttons: buttons) if buttons
  
  {
    name: name,
    language: language,
    category: 'UTILITY',
    components: components
  }
end

#create(business_account_id:, name:, language:, category:, components:, allow_category_change: nil, message_send_ttl_seconds: nil) ⇒ Object

Create a new template



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/kapso_client_ruby/resources/templates.rb', line 42

def create(business_account_id:, name:, language:, category:, components:, 
           allow_category_change: nil, message_send_ttl_seconds: nil)
  validate_template_data(name: name, language: language, category: category, components: components)
  
  payload = {
    name: name,
    language: language,
    category: category,
    components: normalize_components(components)
  }
  
  payload[:allow_category_change] = allow_category_change unless allow_category_change.nil?
  payload[:message_send_ttl_seconds] = message_send_ttl_seconds if message_send_ttl_seconds
  
  response = @client.request(:post, "#{business_account_id}/message_templates", 
                             body: payload.to_json, response_type: :json)
  Types::TemplateCreateResponse.new(response)
end

#delete(business_account_id:, name: nil, template_id: nil, hsm_id: nil, language: nil) ⇒ Object

Delete a template



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/kapso_client_ruby/resources/templates.rb', line 75

def delete(business_account_id:, name: nil, template_id: nil, hsm_id: nil, language: nil)
  if template_id
    # Delete by template ID

    response = @client.request(:delete, "#{business_account_id}/message_templates/#{template_id}", 
                               response_type: :json)
  elsif name
    # Delete by name and language

    query_params = { name: name }
    query_params[:language] = language if language
    query_params[:hsm_id] = hsm_id if hsm_id
    
    response = @client.request(:delete, "#{business_account_id}/message_templates", 
                               query: query_params, response_type: :json)
  else
    raise ArgumentError, 'Must provide either template_id or name'
  end
  
  Types::GraphSuccessResponse.new(response)
end

#get(business_account_id:, template_id:, fields: nil) ⇒ Object

Get a specific template



32
33
34
35
36
37
38
39
# File 'lib/kapso_client_ruby/resources/templates.rb', line 32

def get(business_account_id:, template_id:, fields: nil)
  query_params = {}
  query_params[:fields] = fields if fields
  
  response = @client.request(:get, "#{business_account_id}/message_templates/#{template_id}", 
                             query: query_params, response_type: :json)
  Types::MessageTemplate.new(response)
end

#list(business_account_id:, limit: nil, after: nil, before: nil, name: nil, status: nil, category: nil, language: nil, name_or_content: nil, quality_score: nil) ⇒ Object

List templates for a business account



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/kapso_client_ruby/resources/templates.rb', line 11

def list(business_account_id:, limit: nil, after: nil, before: nil, 
         name: nil, status: nil, category: nil, language: nil, 
         name_or_content: nil, quality_score: nil)
  query_params = {
    limit: limit,
    after: after,
    before: before,
    name: name,
    status: status,
    category: category,
    language: language,
    name_or_content: name_or_content,
    quality_score: quality_score
  }.compact
  
  response = @client.request(:get, "#{business_account_id}/message_templates", 
                             query: query_params, response_type: :json)
  Types::PagedResponse.new(response, Types::MessageTemplate)
end

#update(business_account_id:, template_id:, category: nil, components: nil) ⇒ Object

Update a template



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/kapso_client_ruby/resources/templates.rb', line 62

def update(business_account_id:, template_id:, category: nil, components: nil)
  payload = {}
  payload[:category] = category if category
  payload[:components] = normalize_components(components) if components
  
  return if payload.empty?
  
  response = @client.request(:post, "#{business_account_id}/message_templates/#{template_id}", 
                             body: payload.to_json, response_type: :json)
  Types::GraphSuccessResponse.new(response)
end