Class: Whatsapp::Notifier

Inherits:
Object
  • Object
show all
Defined in:
lib/whatsapp/notifier.rb

Overview

Simple notification service with template support and bulk sending.

Examples:

Simple notification

notifier = Whatsapp::Notifier.new
notifier.notify("+33612345678", "Your order has shipped!")

With template

notifier = Whatsapp::Notifier.new(
  template: "Hello %<name>s, your code is %<code>s."
)
notifier.notify("+33612345678", name: "Alice", code: "1234")

Bulk sending (rate-limit aware)

notifier.bulk([
  { to: "+33611111111", text: "Hello A" },
  { to: "+33622222222", text: "Hello B" },
])

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template: nil, async: false) ⇒ Notifier

Returns a new instance of Notifier.

Parameters:

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

    message template

  • async (Boolean) (defaults to: false)

    whether to send via ActiveJob



26
27
28
29
# File 'lib/whatsapp/notifier.rb', line 26

def initialize(template: nil, async: false)
  @template = template
  @async = async
end

Instance Attribute Details

#templateString? (readonly)

Returns message template with named placeholders.

Returns:

  • (String, nil)

    message template with named placeholders



22
23
24
# File 'lib/whatsapp/notifier.rb', line 22

def template
  @template
end

Instance Method Details

#bulk(messages) ⇒ Hash

Send notifications to multiple recipients.

Stops on rate limit errors and continues on other errors.

Parameters:

  • messages (Array<Hash>)

    array of +{ to:, text: }+ hashes

Returns:

  • (Hash)

    +{ sent:, failed:, errors: }+ summary



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/whatsapp/notifier.rb', line 53

def bulk(messages)
  results = {sent: 0, failed: 0, errors: []}

  messages.each do |msg|
    to = msg[:to]
    text = msg[:text] || build_text(nil, msg.except(:to))

    begin
      notify(to, text)
      results[:sent] += 1
    rescue Whatsapp::RateLimited => e
      results[:failed] += messages.size - results[:sent] - results[:failed]
      results[:errors] << {to: to, error: e.message}
      break
    rescue Whatsapp::Error => e
      results[:failed] += 1
      results[:errors] << {to: to, error: e.message}
    end
  end

  results
end

#notify(to, text_or_vars = nil, **vars) ⇒ Message, void

Send a notification to a single recipient.

Parameters:

  • to (String)

    recipient phone number

  • text_or_vars (String, Hash, nil) (defaults to: nil)

    text string or template variables

  • vars (Hash)

    template variables (when +text_or_vars+ is nil)

Returns:

  • (Message, void)

    the sent message (or void if async)



37
38
39
40
41
42
43
44
45
# File 'lib/whatsapp/notifier.rb', line 37

def notify(to, text_or_vars = nil, **vars)
  text = build_text(text_or_vars, vars)

  if @async && defined?(::WhatsappSendJob)
    WhatsappSendJob.perform_later(to: to, text: text)
  else
    Whatsapp.client.send_message(to: to, text: text)
  end
end