Class: Sendly::WhatsAppResource

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

Overview

WhatsApp resource — connect senders, manage their business profiles and templates, and check 24-hour windows.

WhatsApp is a first-class Sendly channel: connect a number you own, create Meta-reviewed message templates, and send via client.messages.send(channel: "whatsapp", ...).

Connecting a number is a one-time $19 setup (no monthly fee) and always ends with a human step: Sendly::WhatsAppSignupResource#create returns a connect_url that a person must open in a browser and log in with Facebook to link their WhatsApp Business Account.

Two ways to reach a recipient:

  • Inside a 24-hour window (the recipient messaged you in the last 24h): free-form text and media are allowed. Check with #window.
  • Anytime: an approved template. Templates are reviewed by Meta (typically 24-48h) and categorized as authentication, utility, or marketing — pricing follows the category and destination country. Note: Meta has paused marketing template delivery to US (+1) numbers.

Examples:

Connect a number, create a template, then send

# 1. Connect ($19 one-time; a human must open the connect URL)
 = client.whatsapp..create(phone_number: "+15559876543")
puts "Have your user open: #{.connect_url}"

# 2. Poll until active
status = client.whatsapp..get(.id)

# 3. Create a template (Meta reviews it, usually 24-48h)
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" }
)

# 4. Send — free-form inside an open 24h window, template anytime
window = client.whatsapp.window(from: "+15559876543", to: "+15551234567")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ WhatsAppResource

Returns a new instance of WhatsAppResource.



624
625
626
627
628
629
# File 'lib/sendly/whatsapp_resource.rb', line 624

def initialize(client)
  @client = client
  @signup = WhatsAppSignupResource.new(client)
  @senders = WhatsAppSendersResource.new(client)
  @templates = WhatsAppTemplatesResource.new(client)
end

Instance Attribute Details

#sendersWhatsAppSendersResource (readonly)

Returns List connected numbers and manage their business profiles.

Returns:



619
620
621
# File 'lib/sendly/whatsapp_resource.rb', line 619

def senders
  @senders
end

#signupWhatsAppSignupResource (readonly)

Returns Connect numbers to WhatsApp.

Returns:



615
616
617
# File 'lib/sendly/whatsapp_resource.rb', line 615

def 
  @signup
end

#templatesWhatsAppTemplatesResource (readonly)

Returns Manage Meta-reviewed templates.

Returns:



622
623
624
# File 'lib/sendly/whatsapp_resource.rb', line 622

def templates
  @templates
end

Instance Method Details

#window(from:, to:) ⇒ WhatsAppWindow

Check whether a 24-hour customer-service window is open between one of your WhatsApp senders and a recipient.

Free-form text and media only deliver while a window is open (it opens when the recipient messages you and lasts 24h from their last inbound message). Outside a window, send an approved template.

Examples:

window = client.whatsapp.window(from: "+15559876543", to: "+15551234567")
unless window.open?
  # Use a template send instead of free-form text
end

Parameters:

  • from (String)

    Your WhatsApp-connected sending number, in E.164 format

  • to (String)

    The recipient's number, in E.164 format

Returns:

Raises:



647
648
649
650
651
652
653
# File 'lib/sendly/whatsapp_resource.rb', line 647

def window(from:, to:)
  raise ValidationError, "from is required" if from.nil? || from.to_s.empty?
  raise ValidationError, "to is required" if to.nil? || to.to_s.empty?

  response = @client.get("/whatsapp/window", { from: from, to: to })
  WhatsAppWindow.new(response)
end