Class: InstagramConnect::MessagingWindow

Inherits:
Object
  • Object
show all
Defined in:
lib/instagram_connect/messaging_window.rb

Overview

Encodes Meta's outbound messaging window. After a user's last inbound message a business may send standard replies for 24h; the HUMAN_AGENT tag extends that to 7 days (human replies only). Beyond that, nothing may be sent until the user messages again.

Constant Summary collapse

STANDARD_SECONDS =
24 * 60 * 60
HUMAN_AGENT_SECONDS =
7 * 24 * 60 * 60

Instance Method Summary collapse

Constructor Details

#initialize(last_inbound_at:, now: Time.now) ⇒ MessagingWindow

Returns a new instance of MessagingWindow.



10
11
12
13
# File 'lib/instagram_connect/messaging_window.rb', line 10

def initialize(last_inbound_at:, now: Time.now)
  @last_inbound_at = last_inbound_at
  @now = now
end

Instance Method Details

#open?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/instagram_connect/messaging_window.rb', line 25

def open?
  state != :closed
end

#send_tagObject

The message tag to send with, or :blocked when no send is permitted.



34
35
36
37
38
39
40
# File 'lib/instagram_connect/messaging_window.rb', line 34

def send_tag
  case state
  when :standard then nil
  when :human_agent then "HUMAN_AGENT"
  else :blocked
  end
end

#standard?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/instagram_connect/messaging_window.rb', line 29

def standard?
  state == :standard
end

#stateObject



15
16
17
18
19
20
21
22
23
# File 'lib/instagram_connect/messaging_window.rb', line 15

def state
  return :closed if @last_inbound_at.nil?

  elapsed = @now - @last_inbound_at
  return :standard if elapsed <= STANDARD_SECONDS
  return :human_agent if elapsed <= HUMAN_AGENT_SECONDS

  :closed
end