Class: Whatsapp::Messages::Interactive

Inherits:
Base
  • Object
show all
Defined in:
lib/ruby/whatsapp/messages/interactive.rb,
lib/ruby/whatsapp/messages/interactive/base.rb,
lib/ruby/whatsapp/messages/interactive/body.rb,
lib/ruby/whatsapp/messages/interactive/footer.rb,
lib/ruby/whatsapp/messages/interactive/header.rb,
lib/ruby/whatsapp/messages/interactive/url_button.rb,
lib/ruby/whatsapp/messages/interactive/list_buttons.rb,
lib/ruby/whatsapp/messages/interactive/reply_buttons.rb,
lib/ruby/whatsapp/messages/interactive/media_carousel.rb,
lib/ruby/whatsapp/messages/interactive/product_carousel.rb,
lib/ruby/whatsapp/messages/interactive/media_carousel/card.rb,
lib/ruby/whatsapp/messages/interactive/list_buttons/section.rb,
lib/ruby/whatsapp/messages/interactive/reply_buttons/button.rb,
lib/ruby/whatsapp/messages/interactive/product_carousel/card.rb,
lib/ruby/whatsapp/messages/interactive/list_buttons/section/row.rb,
lib/ruby/whatsapp/messages/interactive/media_carousel/card/button.rb,
lib/ruby/whatsapp/messages/interactive/media_carousel/card/button/quick_reply.rb

Defined Under Namespace

Modules: Defaults Classes: Base, Body, Footer, Header, ListButtons, MediaCarousel, ProductCarousel, ReplyButtons, UrlButton

Constant Summary collapse

ACTION_TYPES =

Maps a public action kind to its implementing class and the WhatsApp interactive.type value emitted in the payload. Resolution goes through this frozen whitelist — never const_get on caller input.

NOTE: the carousel api_type values ("carousel"/"product_list") should be verified against the Meta docs for your API version before relying on them.

{
  reply_buttons: { klass: ReplyButtons, api_type: "button" },
  list_buttons: { klass: ListButtons, api_type: "list" },
  url_button: { klass: UrlButton, api_type: "cta_url" },
  media_carousel: { klass: MediaCarousel, api_type: "carousel" },
  product_carousel: { klass: ProductCarousel, api_type: "product_list" },
}.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#to

Instance Method Summary collapse

Constructor Details

#initialize(type:, body:, action:, header: nil, footer: nil) ⇒ Interactive

Returns a new instance of Interactive.

Parameters:

  • type (Symbol, String)

    The action kind (see ACTION_TYPES).

  • body (String)

    The body text of the interactive message.

  • action (Hash)

    The action content, forwarded to the action class.

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

    The header content (optional).

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

    The footer content (optional).

  • kwargs (Hash)

    Additional keyword arguments (e.g. :to). @raise [ActiveModel::ValidationError] if validation fails.



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 55

def initialize(type:, body:, action:, header: nil, footer: nil, **)
  super(**)

  @type = type.to_sym
  @body = body
  @action = action
  @header = header
  @footer = footer

  validate!
end

Instance Attribute Details

#actionHash

Returns:

  • (Hash)


42
43
44
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 42

def action
  @action
end

#bodyString

Returns:

  • (String)


34
35
36
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 34

def body
  @body
end

Returns:

  • (Hash, nil)


38
39
40
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 38

def footer
  @footer
end

#headerHash?

Returns:

  • (Hash, nil)


30
31
32
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 30

def header
  @header
end

#typeSymbol

Returns The action kind (e.g. :reply_buttons, :list_buttons).

Returns:

  • (Symbol)

    The action kind (e.g. :reply_buttons, :list_buttons).



26
27
28
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 26

def type
  @type
end

Instance Method Details

#serializeHash

Returns Serialized representation of the Interactive message.

Returns:

  • (Hash)

    Serialized representation of the Interactive message.



68
69
70
# File 'lib/ruby/whatsapp/messages/interactive.rb', line 68

def serialize
  envelope(type: Defaults::TYPE, interactive: interactive_payload)
end