Class: Whatsapp::Webhook::Message::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby/whatsapp/webhook/message/base.rb

Overview

Common envelope shared by every inbound message type: who sent it, its wamid, when it arrived, and (optionally) what it's replying to.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from:, id:, timestamp:, type:, context: nil, referral: nil) ⇒ Base

Returns a new instance of Base.



33
34
35
36
37
38
39
40
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 33

def initialize(from:, id:, timestamp:, type:, context: nil, referral: nil)
  @from = from
  @id = id
  @timestamp = timestamp
  @type = type
  @context = context
  @referral = referral
end

Instance Attribute Details

#contextContext?

Returns:



27
28
29
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 27

def context
  @context
end

#fromString?

Returns:

  • (String, nil)


11
12
13
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 11

def from
  @from
end

#idString?

Returns The message's wamid.

Returns:

  • (String, nil)

    The message's wamid.



15
16
17
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 15

def id
  @id
end

#referralReferral?

Returns:



31
32
33
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 31

def referral
  @referral
end

#timestampString?

Returns:

  • (String, nil)


19
20
21
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 19

def timestamp
  @timestamp
end

#typeString?

Returns:

  • (String, nil)


23
24
25
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 23

def type
  @type
end

Class Method Details

.common_attributes(data) ⇒ Hash

Extracts the fields every message type shares, for subclasses to merge with their own type-specific payload before calling new.

Parameters:

  • data (Hash)

    The raw message hash.

Returns:

  • (Hash)


47
48
49
50
51
52
53
54
55
56
# File 'lib/ruby/whatsapp/webhook/message/base.rb', line 47

def common_attributes(data)
  {
    from: data["from"],
    id: data["id"],
    timestamp: data["timestamp"],
    type: data["type"],
    context: Context.deserialize(data["context"]),
    referral: Referral.deserialize(data["referral"]),
  }
end