Class: InstagramConnect::Ingest::Handlers::Messages

Inherits:
Base
  • Object
show all
Defined in:
lib/instagram_connect/ingest/handlers/messages.rb

Overview

Inbound DMs, and echoes of messages the business sent from anywhere — this app, the Instagram app, or Meta Business Suite.

Instance Attribute Summary

Attributes inherited from Base

#notification

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from InstagramConnect::Ingest::Handlers::Base

Class Method Details

.dedupe_key(event) ⇒ Object



7
8
9
# File 'lib/instagram_connect/ingest/handlers/messages.rb', line 7

def self.dedupe_key(event)
  event.dig("message", "mid").presence
end

.requires_dedupe_key?Boolean

Without Meta's mid there is no way to dedupe this message against its own echo, so it is banked unparsed rather than stored twice.

Returns:

  • (Boolean)


13
14
15
# File 'lib/instagram_connect/ingest/handlers/messages.rb', line 13

def self.requires_dedupe_key?
  true
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/instagram_connect/ingest/handlers/messages.rb', line 17

def call
  # An echo of a message this system already sent is a CONFIRMATION,
  # not a new message: the send stored the mid first, so create! hits
  # the unique index on [account_id, ig_message_id] and the event is
  # banked failed — then every replay fails again, forever. Enrich the
  # row Meta is echoing rather than duplicating it, and do not
  # re-register it on the conversation (it was registered when sent).
  if (existing = existing_message)
    enrich(existing)
    return existing
  end

  message = InstagramConnect::Message.create!(
    conversation: conversation,
    direction: echo? ? "outbound" : "inbound",
    status: echo? ? "sent" : "received",
    source: echo? ? "operator_app" : "inbound",
    kind: "dm",
    body: payload["text"],
    ig_message_id: mid,
    media_status: media_status,
    media_kind: attachments.first&.dig("type"),
    sent_at: envelope.occurred_at,
    deleted_at: payload["is_deleted"] ? (envelope.occurred_at || Time.current) : nil,
    unsupported: payload["is_unsupported"] ? true : false,
    reply_to_mid: payload.dig("reply_to", "mid"),
    quick_reply_payload: payload.dig("quick_reply", "payload"),
    referral_ref: payload.dig("referral", "ref"),
    referral_source: payload.dig("referral", "source"),
    referral_type: payload.dig("referral", "type"),
    referral_product_id: payload.dig("referral", "product", "id"),
    story_id: story&.dig("id"),
    story_url: story&.dig("url")
  )
  store_attachments(message)
  conversation.register_message(message)
  record_legacy_claim

  # An echo is our own outbound message coming back; firing on_message
  # for it would have the host reply to itself.
  notify(config.on_message, message) unless echo?
  message
end