instagram_connect 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ci.yml +36 -0
- data/CHANGELOG.md +89 -0
- data/Gemfile +4 -0
- data/README.md +328 -73
- data/app/controllers/instagram_connect/comments_controller.rb +1 -1
- data/app/controllers/instagram_connect/posts_controller.rb +1 -1
- data/app/jobs/instagram_connect/account_readiness_job.rb +148 -0
- data/app/jobs/instagram_connect/fetch_media_job.rb +110 -0
- data/app/jobs/instagram_connect/profile_sync_job.rb +78 -0
- data/app/jobs/instagram_connect/replay_events_job.rb +57 -0
- data/app/jobs/instagram_connect/send_message_job.rb +44 -16
- data/app/models/instagram_connect/account.rb +27 -1
- data/app/models/instagram_connect/api_budget.rb +22 -0
- data/app/models/instagram_connect/conversation.rb +99 -11
- data/app/models/instagram_connect/inbound_message.rb +11 -3
- data/app/models/instagram_connect/insight_snapshot.rb +43 -0
- data/app/models/instagram_connect/media_item.rb +38 -0
- data/app/models/instagram_connect/mention.rb +37 -0
- data/app/models/instagram_connect/message.rb +34 -2
- data/app/models/instagram_connect/message_attachment.rb +53 -0
- data/app/models/instagram_connect/message_reaction.rb +33 -0
- data/app/models/instagram_connect/webhook_event.rb +67 -0
- data/db/migrate/20260715072548_create_instagram_connect_accounts.rb +2 -2
- data/db/migrate/20260715072549_create_instagram_connect_conversations.rb +2 -2
- data/db/migrate/20260715072550_create_instagram_connect_messages.rb +3 -3
- data/db/migrate/20260715072551_create_instagram_connect_inbound_messages.rb +2 -2
- data/db/migrate/20260715072552_create_instagram_connect_comments.rb +3 -3
- data/db/migrate/20260725194732_create_instagram_connect_webhook_events.rb +52 -0
- data/db/migrate/20260725200216_add_messaging_event_fields.rb +89 -0
- data/db/migrate/20260725200250_create_instagram_connect_message_reactions.rb +30 -0
- data/db/migrate/20260725201320_create_instagram_connect_message_attachments.rb +33 -0
- data/db/migrate/20260725202616_add_outbound_send_fields.rb +27 -0
- data/db/migrate/20260725203213_create_instagram_connect_media.rb +30 -0
- data/db/migrate/20260725203214_create_instagram_connect_mentions.rb +30 -0
- data/db/migrate/20260725203215_create_instagram_connect_insight_snapshots.rb +45 -0
- data/db/migrate/20260725203306_add_comment_moderation_fields.rb +29 -0
- data/db/migrate/20260725204022_add_per_account_token_fields.rb +76 -0
- data/db/migrate/20260725204815_create_instagram_connect_api_budgets.rb +29 -0
- data/db/migrate/20260725205417_add_conversation_profile_fields.rb +21 -0
- data/docs/CONFIGURATION.md +107 -0
- data/lib/instagram_connect/client.rb +109 -3
- data/lib/instagram_connect/configuration.rb +10 -0
- data/lib/instagram_connect/engine.rb +1 -0
- data/lib/instagram_connect/ingest/dispatcher.rb +151 -0
- data/lib/instagram_connect/ingest/envelope.rb +31 -0
- data/lib/instagram_connect/ingest/handlers/base.rb +55 -0
- data/lib/instagram_connect/ingest/handlers/comments.rb +59 -0
- data/lib/instagram_connect/ingest/handlers/mentions.rb +48 -0
- data/lib/instagram_connect/ingest/handlers/message_reactions.rb +91 -0
- data/lib/instagram_connect/ingest/handlers/messages.rb +135 -0
- data/lib/instagram_connect/ingest/handlers/messaging_handover.rb +53 -0
- data/lib/instagram_connect/ingest/handlers/messaging_optins.rb +37 -0
- data/lib/instagram_connect/ingest/handlers/messaging_postbacks.rb +24 -0
- data/lib/instagram_connect/ingest/handlers/messaging_referral.rb +42 -0
- data/lib/instagram_connect/ingest/handlers/messaging_seen.rb +37 -0
- data/lib/instagram_connect/ingest/handlers/story_insights.rb +62 -0
- data/lib/instagram_connect/ingest/handlers/unknown.rb +19 -0
- data/lib/instagram_connect/ingest/registry.rb +93 -0
- data/lib/instagram_connect/ingest/summary.rb +75 -0
- data/lib/instagram_connect/ingest.rb +27 -105
- data/lib/instagram_connect/media/attaching.rb +57 -0
- data/lib/instagram_connect/media/limits.rb +46 -0
- data/lib/instagram_connect/rate_limiter.rb +88 -0
- data/lib/instagram_connect/result.rb +26 -6
- data/lib/instagram_connect/text_splitter.rb +51 -0
- data/lib/instagram_connect/usage.rb +68 -0
- data/lib/instagram_connect/version.rb +1 -1
- data/lib/instagram_connect.rb +23 -0
- metadata +60 -2
- data/MIT-LICENSE +0 -20
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# Inbound DMs, and echoes of messages the business sent from anywhere —
|
|
5
|
+
# this app, the Instagram app, or Meta Business Suite.
|
|
6
|
+
class Messages < Base
|
|
7
|
+
def self.dedupe_key(event)
|
|
8
|
+
event.dig("message", "mid").presence
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# Without Meta's mid there is no way to dedupe this message against its
|
|
12
|
+
# own echo, so it is banked unparsed rather than stored twice.
|
|
13
|
+
def self.requires_dedupe_key?
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call
|
|
18
|
+
message = InstagramConnect::Message.create!(
|
|
19
|
+
conversation: conversation,
|
|
20
|
+
direction: echo? ? "outbound" : "inbound",
|
|
21
|
+
status: echo? ? "sent" : "received",
|
|
22
|
+
source: echo? ? "operator_app" : "inbound",
|
|
23
|
+
kind: "dm",
|
|
24
|
+
body: payload["text"],
|
|
25
|
+
ig_message_id: mid,
|
|
26
|
+
media_status: media_status,
|
|
27
|
+
media_kind: attachments.first&.dig("type"),
|
|
28
|
+
sent_at: envelope.occurred_at,
|
|
29
|
+
deleted_at: payload["is_deleted"] ? (envelope.occurred_at || Time.current) : nil,
|
|
30
|
+
unsupported: payload["is_unsupported"] ? true : false,
|
|
31
|
+
reply_to_mid: payload.dig("reply_to", "mid"),
|
|
32
|
+
quick_reply_payload: payload.dig("quick_reply", "payload"),
|
|
33
|
+
referral_ref: payload.dig("referral", "ref"),
|
|
34
|
+
referral_source: payload.dig("referral", "source"),
|
|
35
|
+
referral_type: payload.dig("referral", "type"),
|
|
36
|
+
referral_product_id: payload.dig("referral", "product", "id"),
|
|
37
|
+
story_id: story&.dig("id"),
|
|
38
|
+
story_url: story&.dig("url")
|
|
39
|
+
)
|
|
40
|
+
store_attachments(message)
|
|
41
|
+
conversation.register_message(message)
|
|
42
|
+
record_legacy_claim
|
|
43
|
+
|
|
44
|
+
# An echo is our own outbound message coming back; firing on_message
|
|
45
|
+
# for it would have the host reply to itself.
|
|
46
|
+
notify(config.on_message, message) unless echo?
|
|
47
|
+
message
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
private
|
|
51
|
+
|
|
52
|
+
def payload
|
|
53
|
+
event["message"] || {}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def mid
|
|
57
|
+
payload["mid"].to_s
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def echo?
|
|
61
|
+
payload["is_echo"] ? true : false
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def attachments
|
|
65
|
+
Array(payload["attachments"])
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# An unsupported message has nothing fetchable behind it, and Instagram
|
|
69
|
+
# CDN links expire with no way to ask again — so the verdict is decided
|
|
70
|
+
# here, before the insert, and the row is born in its final state.
|
|
71
|
+
def media_status
|
|
72
|
+
return "unavailable" if payload["is_unsupported"]
|
|
73
|
+
|
|
74
|
+
attachments.any? { |a| a.dig("payload", "url").present? } ? "pending" : "none"
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Rows are written in one insert_all so the bubble is born in its final
|
|
78
|
+
# state: one create, one broadcast, no flicker as files resolve. Each
|
|
79
|
+
# fetch is its own job, so a ten-image message does not serialise
|
|
80
|
+
# behind whichever file is slowest.
|
|
81
|
+
def store_attachments(message)
|
|
82
|
+
return if attachments.empty?
|
|
83
|
+
|
|
84
|
+
rows = attachments.each_with_index.map do |attachment, index|
|
|
85
|
+
url = attachment.dig("payload", "url")
|
|
86
|
+
{
|
|
87
|
+
message_id: message.id, position: index,
|
|
88
|
+
kind: attachment["type"].presence || "file",
|
|
89
|
+
source_url: url,
|
|
90
|
+
state: url.present? ? "pending" : "skipped",
|
|
91
|
+
created_at: Time.current, updated_at: Time.current
|
|
92
|
+
}
|
|
93
|
+
end
|
|
94
|
+
InstagramConnect::MessageAttachment.insert_all(rows)
|
|
95
|
+
|
|
96
|
+
message.attachments.reload.select(&:fetchable?).each do |attachment|
|
|
97
|
+
InstagramConnect::FetchMediaJob.perform_later(attachment.id)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# A story reply and a story mention both point at the story they came
|
|
102
|
+
# from; Meta nests it under the attachment payload.
|
|
103
|
+
def story
|
|
104
|
+
@story ||= attachments.filter_map { |a| a.dig("payload", "story") }.first
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# On an echo the customer is the recipient; on an inbound message they
|
|
108
|
+
# are the sender. Either way the conversation is keyed by their IGSID.
|
|
109
|
+
def igsid
|
|
110
|
+
(echo? ? event.dig("recipient", "id") : event.dig("sender", "id")).to_s
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def conversation
|
|
114
|
+
@conversation ||= InstagramConnect::Conversation.locate(account: account, igsid: igsid).tap do |thread|
|
|
115
|
+
# Meta sends the profile on a separate call from the message, so a
|
|
116
|
+
# thread starts life knowing only an opaque id. Fetch it once, when
|
|
117
|
+
# the thread first appears, rather than on every message.
|
|
118
|
+
enqueue_profile_sync(thread) if thread.profile_synced_at.nil?
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def enqueue_profile_sync(thread)
|
|
123
|
+
InstagramConnect::ProfileSyncJob.perform_later(conversation_id: thread.id)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# The webhook ledger is the dedupe claim now. InboundMessage is kept in
|
|
127
|
+
# step so a host querying it against 0.2.x behaviour still sees every
|
|
128
|
+
# message; it is deprecated and will go at 1.0.
|
|
129
|
+
def record_legacy_claim
|
|
130
|
+
InstagramConnect::InboundMessage.claim(ig_message_id: mid, account_id: account.id)
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# Thread control moving between apps — typically between this app and the
|
|
5
|
+
# Page Inbox a human operator uses.
|
|
6
|
+
#
|
|
7
|
+
# This matters beyond bookkeeping: the Send API rejects a message on a
|
|
8
|
+
# thread another app owns, so a send has to read thread_owner_app_id
|
|
9
|
+
# first rather than discovering it as an opaque API error.
|
|
10
|
+
class MessagingHandover < Base
|
|
11
|
+
CONTROL_KEYS = %w[pass_thread_control take_thread_control request_thread_control].freeze
|
|
12
|
+
|
|
13
|
+
def self.dedupe_key(event)
|
|
14
|
+
sender = event.dig("sender", "id")
|
|
15
|
+
key = CONTROL_KEYS.find { |k| event[k] }
|
|
16
|
+
return nil if key.nil?
|
|
17
|
+
|
|
18
|
+
[ sender, event["timestamp"], key, event.dig(key, "new_owner_app_id") ].compact.join(":")
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def call
|
|
22
|
+
conversation.register_thread_control(owner_app_id: new_owner_app_id, at: handed_over_at)
|
|
23
|
+
conversation
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
def control_key
|
|
29
|
+
@control_key ||= CONTROL_KEYS.find { |key| event[key] }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# A pass names the app receiving control. A take is the Page Inbox
|
|
33
|
+
# seizing it, so control lands with Meta's own app rather than a named
|
|
34
|
+
# one; recording nil is honest — we know we no longer own the thread.
|
|
35
|
+
def new_owner_app_id
|
|
36
|
+
event.dig(control_key.to_s, "new_owner_app_id")
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def igsid
|
|
40
|
+
event.dig("sender", "id").to_s
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def handed_over_at
|
|
44
|
+
envelope.occurred_at || Time.current
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def conversation
|
|
48
|
+
@conversation ||= InstagramConnect::Conversation.locate(account: account, igsid: igsid)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# The customer opted in to notification messages. There is nothing to
|
|
5
|
+
# store on the thread beyond the fact that they interacted — the opt-in
|
|
6
|
+
# token itself lives in the banked event, where a host that implements
|
|
7
|
+
# recurring notifications can read it without the gem taking a position
|
|
8
|
+
# on how that should work.
|
|
9
|
+
class MessagingOptins < Base
|
|
10
|
+
def self.dedupe_key(event)
|
|
11
|
+
sender = event.dig("sender", "id").presence or return nil
|
|
12
|
+
|
|
13
|
+
"#{sender}:#{event['timestamp']}"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def call
|
|
17
|
+
conversation.register_event(opted_in_at)
|
|
18
|
+
conversation
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
def igsid
|
|
24
|
+
event.dig("sender", "id").to_s
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def opted_in_at
|
|
28
|
+
envelope.occurred_at || Time.current
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def conversation
|
|
32
|
+
@conversation ||= InstagramConnect::Conversation.locate(account: account, igsid: igsid)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# A tap on an ice breaker, a persistent-menu item, or a template button.
|
|
5
|
+
# Postbacks carry no message of their own, so there is nothing to persist
|
|
6
|
+
# yet — the host callback is the whole point.
|
|
7
|
+
class MessagingPostbacks < Base
|
|
8
|
+
def self.dedupe_key(event)
|
|
9
|
+
event.dig("postback", "mid").presence
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call
|
|
13
|
+
notify(config.on_postback, {
|
|
14
|
+
account_id: account.id,
|
|
15
|
+
sender_id: event.dig("sender", "id"),
|
|
16
|
+
payload: event.dig("postback", "payload"),
|
|
17
|
+
title: event.dig("postback", "title")
|
|
18
|
+
})
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# The customer arrived through an ig.me link, an ad, or a product tag.
|
|
5
|
+
# A referral opens the 24-hour messaging window even before they type
|
|
6
|
+
# anything, so it has to bump the thread's activity.
|
|
7
|
+
#
|
|
8
|
+
# Only a standalone referral reaches here — one nested inside a message
|
|
9
|
+
# or postback stays with its parent event (see Registry).
|
|
10
|
+
class MessagingReferral < Base
|
|
11
|
+
def self.dedupe_key(event)
|
|
12
|
+
ref = event.dig("referral", "ref").presence or return nil
|
|
13
|
+
|
|
14
|
+
"#{event.dig('sender', 'id')}:#{ref}:#{event['timestamp']}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def call
|
|
18
|
+
conversation.register_referral(ref: payload["ref"], at: referred_at)
|
|
19
|
+
conversation
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def payload
|
|
25
|
+
event["referral"] || {}
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def igsid
|
|
29
|
+
event.dig("sender", "id").to_s
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def referred_at
|
|
33
|
+
envelope.occurred_at || Time.current
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def conversation
|
|
37
|
+
@conversation ||= InstagramConnect::Conversation.locate(account: account, igsid: igsid)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# A read receipt: the customer has seen everything up to this message.
|
|
5
|
+
class MessagingSeen < Base
|
|
6
|
+
def self.dedupe_key(event)
|
|
7
|
+
mid = event.dig("read", "mid").presence or return nil
|
|
8
|
+
|
|
9
|
+
"#{event.dig('sender', 'id')}:#{mid}"
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def call
|
|
13
|
+
conversation.mark_read_by_customer(at: seen_at, mid: mid)
|
|
14
|
+
conversation
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def mid
|
|
20
|
+
event.dig("read", "mid").to_s
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def igsid
|
|
24
|
+
event.dig("sender", "id").to_s
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def seen_at
|
|
28
|
+
envelope.occurred_at || Time.current
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def conversation
|
|
32
|
+
@conversation ||= InstagramConnect::Conversation.locate(account: account, igsid: igsid)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# A story expired, and Meta sent its final metrics with the notification.
|
|
5
|
+
#
|
|
6
|
+
# This is the only durable capture path there is. Story insights are
|
|
7
|
+
# retrievable from the API for 24 hours; after that the numbers do not
|
|
8
|
+
# exist anywhere. So the snapshot is written here, inside ingest, rather
|
|
9
|
+
# than handed to a job that might be delayed past the point of no return.
|
|
10
|
+
class StoryInsights < Base
|
|
11
|
+
def self.dedupe_key(event)
|
|
12
|
+
event.dig("value", "media_id").presence
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def self.requires_dedupe_key?
|
|
16
|
+
true
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call
|
|
20
|
+
# The story may well have been posted from a phone rather than through
|
|
21
|
+
# this app, so its media row might not exist yet.
|
|
22
|
+
InstagramConnect::MediaItem.record(
|
|
23
|
+
account: account, ig_media_id: media_id, media_product_type: "STORY"
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
InstagramConnect::InsightSnapshot.record(
|
|
27
|
+
account: account,
|
|
28
|
+
subject_type: "story",
|
|
29
|
+
subject_ref: media_id,
|
|
30
|
+
period: "lifetime",
|
|
31
|
+
period_start: captured_at.to_date,
|
|
32
|
+
source: "webhook",
|
|
33
|
+
metrics: metrics,
|
|
34
|
+
empty: metrics.empty?,
|
|
35
|
+
captured_at: captured_at
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def value
|
|
42
|
+
event["value"] || {}
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def media_id
|
|
46
|
+
value["media_id"].to_s
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# Everything except the identifier is a metric. Meta omits a metric it
|
|
50
|
+
# will not report rather than sending a zero, and that distinction is
|
|
51
|
+
# preserved by copying the payload as-is instead of filling defaults.
|
|
52
|
+
def metrics
|
|
53
|
+
@metrics ||= value.except("media_id")
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def captured_at
|
|
57
|
+
envelope.occurred_at || Time.current
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
module Handlers
|
|
4
|
+
# Anything the gem does not parse yet, or an entry belonging to no
|
|
5
|
+
# connected account. The event is banked verbatim rather than dropped:
|
|
6
|
+
# Meta never re-delivers, so replaying the row later is the only way to
|
|
7
|
+
# recover it once a handler exists.
|
|
8
|
+
#
|
|
9
|
+
# The dispatcher marks these `unhandled` without calling `#call` — this
|
|
10
|
+
# class exists so the registry always resolves to something, and so the
|
|
11
|
+
# ledger records which handler was chosen.
|
|
12
|
+
class Unknown < Base
|
|
13
|
+
def call
|
|
14
|
+
nil
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
# Maps a webhook field to the handler that parses it, and infers the field
|
|
4
|
+
# for messaging events (Meta delivers all of them under `entry.messaging`
|
|
5
|
+
# regardless of which subscription triggered them, so the field has to be
|
|
6
|
+
# recovered from the event's shape).
|
|
7
|
+
module Registry
|
|
8
|
+
HANDLERS = {
|
|
9
|
+
"messages" => Handlers::Messages,
|
|
10
|
+
"message_echoes" => Handlers::Messages,
|
|
11
|
+
"messaging_postbacks" => Handlers::MessagingPostbacks,
|
|
12
|
+
"message_reactions" => Handlers::MessageReactions,
|
|
13
|
+
"messaging_seen" => Handlers::MessagingSeen,
|
|
14
|
+
"messaging_referral" => Handlers::MessagingReferral,
|
|
15
|
+
"messaging_optins" => Handlers::MessagingOptins,
|
|
16
|
+
"messaging_handover" => Handlers::MessagingHandover,
|
|
17
|
+
"comments" => Handlers::Comments,
|
|
18
|
+
"live_comments" => Handlers::Comments,
|
|
19
|
+
"mentions" => Handlers::Mentions,
|
|
20
|
+
"story_insights" => Handlers::StoryInsights
|
|
21
|
+
}.freeze
|
|
22
|
+
|
|
23
|
+
# Every field the gem knows Meta can send on the `instagram` object.
|
|
24
|
+
# Fields without a handler are still worth subscribing to: they bank as
|
|
25
|
+
# `unhandled` rows and replay once a handler ships, whereas an
|
|
26
|
+
# unsubscribed field is gone for good — Meta does not store `mentions` or
|
|
27
|
+
# `story_insights` notifications, and re-delivers nothing.
|
|
28
|
+
KNOWN_FIELDS = %w[
|
|
29
|
+
messages
|
|
30
|
+
message_echoes
|
|
31
|
+
message_reactions
|
|
32
|
+
messaging_postbacks
|
|
33
|
+
messaging_seen
|
|
34
|
+
messaging_referral
|
|
35
|
+
messaging_optins
|
|
36
|
+
messaging_handover
|
|
37
|
+
messaging_policy_enforcement
|
|
38
|
+
response_feedback
|
|
39
|
+
standby
|
|
40
|
+
comments
|
|
41
|
+
live_comments
|
|
42
|
+
mentions
|
|
43
|
+
story_insights
|
|
44
|
+
].freeze
|
|
45
|
+
|
|
46
|
+
# Shape tests in priority order. `is_echo` must be checked before the
|
|
47
|
+
# plain `message` case, since an echo carries a message too.
|
|
48
|
+
MESSAGING_SHAPES = [
|
|
49
|
+
[ "message_echoes", ->(e) { e["message"] && e.dig("message", "is_echo") } ],
|
|
50
|
+
[ "messages", ->(e) { e["message"] } ],
|
|
51
|
+
[ "message_reactions", ->(e) { e["reaction"] } ],
|
|
52
|
+
[ "messaging_postbacks", ->(e) { e["postback"] } ],
|
|
53
|
+
[ "messaging_seen", ->(e) { e["read"] } ],
|
|
54
|
+
[ "messaging_referral", ->(e) { e["referral"] } ],
|
|
55
|
+
[ "messaging_optins", ->(e) { e["optin"] } ],
|
|
56
|
+
[ "messaging_handover", lambda { |e|
|
|
57
|
+
e["pass_thread_control"] || e["take_thread_control"] || e["request_thread_control"]
|
|
58
|
+
} ]
|
|
59
|
+
].freeze
|
|
60
|
+
|
|
61
|
+
UNKNOWN_FIELD = "unknown".freeze
|
|
62
|
+
|
|
63
|
+
module_function
|
|
64
|
+
|
|
65
|
+
def handler_for(field)
|
|
66
|
+
HANDLERS.fetch(field, Handlers::Unknown)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def handled?(field)
|
|
70
|
+
HANDLERS.key?(field)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# The fields a host should subscribe its Page to. Everything the gem
|
|
74
|
+
# knows about, handled or not — see KNOWN_FIELDS.
|
|
75
|
+
def subscribable_fields
|
|
76
|
+
KNOWN_FIELDS
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def handled_fields
|
|
80
|
+
HANDLERS.keys
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
# `message.referral` and `postback.referral` are nested inside their
|
|
84
|
+
# parent event and stay with it; only a standalone `referral` is a
|
|
85
|
+
# referral event in its own right, which is why the shape tests run in
|
|
86
|
+
# order rather than checking every key independently.
|
|
87
|
+
def messaging_field_for(event)
|
|
88
|
+
found = MESSAGING_SHAPES.find { |(_field, test)| test.call(event) }
|
|
89
|
+
found ? found.first : UNKNOWN_FIELD
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
module InstagramConnect
|
|
2
|
+
class Ingest
|
|
3
|
+
# What one webhook delivery produced. Behaves as a Hash so the return value
|
|
4
|
+
# of Ingest.call stays compatible with hosts written against 0.2.x, which
|
|
5
|
+
# read summary[:messages] and friends.
|
|
6
|
+
class Summary
|
|
7
|
+
COUNTERS = {
|
|
8
|
+
"messages" => :messages,
|
|
9
|
+
"message_echoes" => :messages,
|
|
10
|
+
"messaging_postbacks" => :postbacks,
|
|
11
|
+
"comments" => :comments,
|
|
12
|
+
"live_comments" => :comments
|
|
13
|
+
}.freeze
|
|
14
|
+
|
|
15
|
+
attr_accessor :messages, :comments, :postbacks, :duplicates, :unhandled,
|
|
16
|
+
:failed, :callback_errors
|
|
17
|
+
|
|
18
|
+
def initialize
|
|
19
|
+
@messages = 0
|
|
20
|
+
@comments = 0
|
|
21
|
+
@postbacks = 0
|
|
22
|
+
@duplicates = 0
|
|
23
|
+
@unhandled = 0
|
|
24
|
+
@failed = 0
|
|
25
|
+
@callback_errors = 0
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def count(field)
|
|
29
|
+
counter = COUNTERS[field]
|
|
30
|
+
return if counter.nil?
|
|
31
|
+
|
|
32
|
+
public_send(:"#{counter}=", public_send(counter) + 1)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# 0.2.x reported one `skipped` number covering both "we have seen this
|
|
36
|
+
# already" and "we do not parse this". They are different problems now —
|
|
37
|
+
# duplicates are healthy, unhandled events are a backlog to replay — but
|
|
38
|
+
# the combined figure is kept so existing hosts keep working.
|
|
39
|
+
def skipped
|
|
40
|
+
duplicates + unhandled
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def to_h
|
|
44
|
+
{
|
|
45
|
+
messages: messages,
|
|
46
|
+
comments: comments,
|
|
47
|
+
postbacks: postbacks,
|
|
48
|
+
skipped: skipped,
|
|
49
|
+
duplicates: duplicates,
|
|
50
|
+
unhandled: unhandled,
|
|
51
|
+
failed: failed,
|
|
52
|
+
callback_errors: callback_errors
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Implicit conversion, not just the explicit #to_h: it is what makes a
|
|
57
|
+
# Summary behave as a Hash for callers (and matchers) written against the
|
|
58
|
+
# plain hash 0.2.x returned.
|
|
59
|
+
alias_method :to_hash, :to_h
|
|
60
|
+
|
|
61
|
+
def [](key)
|
|
62
|
+
to_h[key.to_sym]
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def ==(other)
|
|
66
|
+
other.respond_to?(:to_h) ? to_h == other.to_h : super
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def each(&block)
|
|
70
|
+
to_h.each(&block)
|
|
71
|
+
end
|
|
72
|
+
include Enumerable
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|