Class: InstagramConnect::SendMessageJob

Inherits:
ApplicationJob
  • Object
show all
Defined in:
app/jobs/instagram_connect/send_message_job.rb

Overview

Sends one pending outbound Message via the Graph API, exactly once. Claims the row (pending -> sending) with an atomic UPDATE so a duplicate enqueue can't double-send, then enforces Meta's messaging window — auto-applying the HUMAN_AGENT tag once the standard 24h window has passed.

Instance Method Summary collapse

Instance Method Details

#perform(message_id) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/jobs/instagram_connect/send_message_job.rb', line 9

def perform(message_id)
  claimed = Message.where(id: message_id, status: "pending")
                   .update_all(status: "sending", updated_at: Time.current)
  return unless claimed == 1

  message = Message.find(message_id)
  tag = MessagingWindow.new(last_inbound_at: message.conversation.last_inbound_at).send_tag

  if tag == :blocked
    return fail_message(message, "outside_messaging_window",
                        "The reply window has closed; the customer must message again.")
  end

  deliver(message, tag)
end