Class: Sendly::WhatsAppTemplatesResource

Inherits:
Object
  • Object
show all
Defined in:
lib/sendly/whatsapp_resource.rb

Overview

Manage Meta-reviewed message templates.

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ WhatsAppTemplatesResource

Returns a new instance of WhatsAppTemplatesResource.



437
438
439
# File 'lib/sendly/whatsapp_resource.rb', line 437

def initialize(client)
  @client = client
end

Instance Method Details

#create(sender:, name:, language:, category:, body:, header: nil, footer: nil, buttons: nil, examples: nil) ⇒ WhatsAppTemplate

Create a template and submit it to Meta for review.

Review usually takes 24-48h; the template is usable once its status is "APPROVED". Requires a live API key.

Examples:

template = client.whatsapp.templates.create(
  sender: "+15559876543",
  name: "order_shipped",
  language: "en_US",
  category: "UTILITY",
  body: "Hi {{1}}, your order {{2}} has shipped!",
  examples: { "1" => "Sam", "2" => "#4821" }
)
puts template.status  # "PENDING"

Parameters:

  • sender (String)

    The WhatsApp-connected sending number this template belongs to, in E.164 format

  • name (String)

    Template name: lowercase letters, digits, and underscores (e.g. "order_shipped")

  • language (String)

    Template language code (e.g. "en_US")

  • category (String)

    "AUTHENTICATION", "UTILITY", or "MARKETING" — drives Meta review rules and pricing

  • body (String)

    Body text. Use {1}, {2}, ... for variables; every placeholder needs an example value in examples

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

    Optional text header

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

    Optional footer line

  • buttons (Array<Hash>, nil) (defaults to: nil)

    Optional buttons, each with :type ("url", "quick_reply", or "otp"), :text, and for url buttons a :url (may contain a {1} placeholder) plus :example values for review

  • examples (Hash, nil) (defaults to: nil)

    Example values for body placeholders, keyed by placeholder number: { "1" => "Acme Inc", "2" => "#4821" }. Required when the body has variables.

Returns:

  • (WhatsAppTemplate)

    The created template (status "PENDING"), with any submission warnings

Raises:



491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
# File 'lib/sendly/whatsapp_resource.rb', line 491

def create(sender:, name:, language:, category:, body:,
           header: nil, footer: nil, buttons: nil, examples: nil)
  raise ValidationError, "sender is required" if sender.nil? || sender.to_s.empty?
  raise ValidationError, "name is required" if name.nil? || name.to_s.empty?
  raise ValidationError, "language is required" if language.nil? || language.to_s.empty?
  raise ValidationError, "category is required" if category.nil? || category.to_s.empty?
  raise ValidationError, "body is required" if body.nil? || body.to_s.empty?

  payload = {
    sender: sender,
    name: name,
    language: language,
    category: category,
    body: body
  }
  payload[:header] = header unless header.nil?
  payload[:footer] = footer unless footer.nil?
  payload[:buttons] = buttons if buttons
  payload[:examples] = examples if examples

  response = @client.post("/whatsapp/templates", payload)
  WhatsAppTemplate.new(response)
end

#delete(id) ⇒ WhatsAppTemplateDeletion

Delete a template.

Meta locks a deleted template's name for ~30 days — re-creating it fails with template_name_locked until the lock lifts. To fix a rejected template, prefer #update. Requires a live API key.

Examples:

client.whatsapp.templates.delete("wat_xxx")

Parameters:

  • id (String)

    The template's id

Returns:

Raises:



563
564
565
566
567
568
569
# File 'lib/sendly/whatsapp_resource.rb', line 563

def delete(id)
  raise ValidationError, "id is required" if id.nil? || id.to_s.empty?

  encoded_id = URI.encode_www_form_component(id)
  response = @client.delete("/whatsapp/templates/#{encoded_id}")
  WhatsAppTemplateDeletion.new(response)
end

#listHash

List your WhatsApp templates.

Examples:

client.whatsapp.templates.list[:templates].each do |t|
  puts "#{t.name} (#{t.language}) — #{t.status}"
end

Returns:

  • (Hash)

    { templates: Array<WhatsAppTemplate> }



449
450
451
452
453
# File 'lib/sendly/whatsapp_resource.rb', line 449

def list
  response = @client.get("/whatsapp/templates")
  templates = (response["templates"] || []).map { |t| WhatsAppTemplate.new(t) }
  { templates: templates }
end

#update(id, body: nil, header: nil, footer: nil, buttons: nil, examples: nil) ⇒ WhatsAppTemplate

Edit an APPROVED or REJECTED template and resubmit it for review.

This is the recovery path for rejections: template names are locked for ~30 days after deletion, so editing a rejected template (rather than deleting and re-creating it) is the way to fix it. The updated template goes back to "PENDING" review. Requires a live API key.

Examples:

client.whatsapp.templates.update("wat_xxx",
  body: "Hi {{1}}, your order {{2}} is on its way!",
  examples: { "1" => "Sam", "2" => "#4821" }
)

Parameters:

  • id (String)

    The template's id

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

    Replacement body text

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

    Replacement text header

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

    Replacement footer

  • buttons (Array<Hash>, nil) (defaults to: nil)

    Replacement buttons

  • examples (Hash, nil) (defaults to: nil)

    Replacement example values for body placeholders

Returns:

Raises:



536
537
538
539
540
541
542
543
544
545
546
547
548
549
# File 'lib/sendly/whatsapp_resource.rb', line 536

def update(id, body: nil, header: nil, footer: nil, buttons: nil, examples: nil)
  raise ValidationError, "id is required" if id.nil? || id.to_s.empty?

  payload = {}
  payload[:body] = body unless body.nil?
  payload[:header] = header unless header.nil?
  payload[:footer] = footer unless footer.nil?
  payload[:buttons] = buttons unless buttons.nil?
  payload[:examples] = examples unless examples.nil?

  encoded_id = URI.encode_www_form_component(id)
  response = @client.patch("/whatsapp/templates/#{encoded_id}", payload)
  WhatsAppTemplate.new(response)
end