diff --git a/docs/architecture/decisions/0028-instagram-channel.md b/docs/architecture/decisions/0028-instagram-channel.md new file mode 100644 index 0000000..78449f1 --- /dev/null +++ b/docs/architecture/decisions/0028-instagram-channel.md @@ -0,0 +1,156 @@ +# ADR-0028: Instagram direct messages as a fourth channel + +**Status:** Accepted, 2026-12-01, by the owner. Before it was written, the owner chose: Instagram connects through the Facebook Page the +Instagram account is linked to, reusing the Messenger connection, with Instagram Login possible later; the first +version covers direct messages only, not comments; and it follows Messenger's other choices (replies typed in +Instagram's own inbox are shown, replies go only within the 24-hour window, and it is included in Cloud's free plan). + +## Context + +[ADR-0027](0027-messenger-channel.md) made Messenger a channel of a website and said Instagram, if it came, would use +the same `conversations.channel_kind` column. The owner asked for Instagram next. + +Meta offers two ways for an app to read and answer Instagram direct messages: + +- **Through a Facebook Page** (Meta calls it "Messenger API support for Instagram"). The Instagram professional + account is linked to a Page, and the app uses the **Page access token** on `graph.facebook.com`, as Messenger does. + A system user's token can be set to never expire. +- **With Instagram Login.** This needs no Page. It uses an Instagram user token on `graph.instagram.com` that lasts 60 + days and must be renewed before it runs out. + +The owner chose the first way. What it requires, as read from Meta's documentation in December 2026: + +- **The account.** It must be an Instagram professional account (business or creator) linked to the Page. In the + Instagram app, *Allow access to messages* must be on (in December 2026 under *Settings and activity → Account + type and tools → Messaging tools → Message controls*). A creator account must make one Conversations API request before webhooks arrive. +- **The token.** The Page access token also needs `instagram_basic` and `instagram_manage_messages`, on top of what + Messenger asks for. `business_management` is a dependency of both. In a business portfolio, the system user must be + given the Instagram account as well as the Page. +- **Webhooks.** The object is `instagram`, not `page`, and is set separately in the Meta app, with the same callback + checks: `hub.verify_token` and `X-Hub-Signature-256` with the app secret. The fields are `messages` (echoes arrive + here with `is_echo`), `messaging_seen`, `message_reactions`, `messaging_postbacks` and `messaging_referral`. In each + entry, `id` is the **Instagram account ID**, not the Page ID. Customers are **Instagram-scoped IDs (IGSIDs)**. +- **No delivery status.** Instagram has no `message_deliveries`. There is only `messaging_seen`, so a reply goes from + Sent straight to Read. +- **Sending.** `POST /me/messages` with the Page token and the IGSID. Text is limited to 1,000 bytes, where Messenger + allows 2,000. Images, video, audio and PDF files can be sent. +- **What customers send that is not plain text or media.** This covers story mentions, replies to a story (with a + link to it), shared posts (a link only), view-once media (no link at all) and reactions. A message the customer + unsends comes as `is_deleted`. Some messages come only as `is_unsupported`, and GIFs and stickers from customers + come with no webhook at all. +- **Profiles.** `GET /{IGSID}?fields=name,username,profile_pic` works once the customer has written to the business. + It needs `pages_read_engagement` and `pages_show_list` too, which Messenger's token already has. +- **The 24-hour window and `HUMAN_AGENT`.** They work as on Messenger. `HUMAN_AGENT` lets a person reply for 7 days + and needs App Review. +- **Who can write.** Meta's pages again say that with Standard Access the app hears only from people with a role on + the app, and that the app must be published. ADR-0027's step 1 found that a published app with no App Review heard + Messenger customers with no role. Whether that holds for Instagram decides how hard setup is, so step 1 tests it + first, as ADR-0027 did. + +**Step 1 finding, 2 December 2026.** Tested on real Cloudflare with OpenChat's own Page, its linked Instagram business +account (@openchat.run) and the same published Meta app as Messenger's test, with no App Review and an unverified +business portfolio. Turning Instagram on worked: Meta named the account, and the Conversations API call succeeded, so +"Allow access to messages" was on. A direct message from an Instagram account linked to the app's admin reached the +webhook. Three from an account with no role on the app did not, neither while in the account's message requests nor +after the request was accepted. So, unlike Messenger, Instagram customers reach a business's own app only after Meta +grants Advanced Access to `instagram_manage_messages` through App Review, which needs business verification. Until +then only people with a role on the app get through, which is enough to build and test the next steps. + +**Step 2 note, 2 December 2026.** Two details differ from the Decision. `messenger_identities` gains a `channel` +column (migration 0043, expand only), so that export and the profile job tell an Instagram customer from a Messenger +one without depending on the connection, which can be removed. A message the customer unsends keeps its own Meta ID, so +the line saying so is keyed `unsent:`, or it would be taken for a repeat. Tested the same day on real Cloudflare +with an account that has a role on the app: a text, a photo, a reaction and an unsent message arrived, and the contact +was named from Meta's profile. The test found that Instagram's CDN redirects a request with no User-Agent, which is +what a Worker sends, to an "unsupported browser" page, so file downloads now send one. + +**Step 3 note, 2 December 2026.** Replies go with the Page's token to `POST /{page-id}/messages`, as on Messenger. +Instagram's read names one message and has no watermark, so everything sent before the read's time counts as read, and +a window refused by Meta comes as code 10, subcode 2534022. Past the window, a reply is refused with its own code, +`instagram_window_closed`. Tested the same day on real Cloudflare: a short reply and a 1,053-byte reply in Chinese, +which went as two parts, reached the customer and were marked Read when they opened the chat. A reply typed in the +Instagram app showed as sent from Instagram, and the echoes of OpenChat's own replies were not shown again. + +## Decision + +Instagram is an addition to a website's **Messenger connection**, not a separate connection. A business connects +Messenger as today, with the same Page, token and app secret, and then turns on Instagram for the account linked to +that Page. Its conversations get the website's assistant, translation, saved replies and opening hours. Everything +below that is not said otherwise works as ADR-0027 describes, with the same code. + +### Connecting + +The website's Messenger tab becomes **Messenger and Instagram**. When Instagram is turned on, OpenChat reads the +Instagram account linked to the Page (`GET /{page-id}?fields=instagram_business_account{id,username}`) with the +saved token. If there is none, or the token lacks the Instagram permissions, it says which, and nothing is saved. It +stores the account's ID and username on the connection and shows what to paste into the Meta app's Instagram webhook +settings: **the same webhook address and verify token** as Messenger. An Instagram account ID is unique across the +installation, as a Page ID is. + +Turning Instagram off stops new Instagram messages being accepted and keeps the conversations. Messenger cannot be +turned off while Instagram stays on, because Instagram depends on its token. + +### Receiving + +- The existing webhook, `/api/v1/messenger/webhook/{connectionId}`, accepts `object: "instagram"` as well as + `object: "page"`, with the same checks. An Instagram entry whose ID is not the connection's Instagram account, or + that arrives while Instagram is off, is acknowledged and dropped. +- A customer is found through `messenger_identities`, with the Instagram account ID in the column that holds the Page + ID for Messenger. The two kinds of ID never collide, so the unique key stays as it is. The contact is named from the + profile: the name, else `@username`. +- Conversations have `channel_kind = 'instagram'`. Messages go to the newest open or pending Instagram conversation of + that contact, as for Messenger. +- Text, images, video, audio and files are kept, with attachments fetched into R2. A story mention, a story reply, a + shared post or a reaction becomes a short line saying what was sent, with Meta's link where there is one. View-once + media and unsupported messages become a line saying they can be seen only in Instagram. When a customer unsends a + message, a line says so. The original is kept in OpenChat, as it is when a visitor leaves a chat; deleting it would + need message deletion, which OpenChat does not have yet. +- **Echoes** are shown as "Sent from Instagram", and OpenChat's own are recognised as on Messenger. + +### Replying + +Replies use the `messenger.send` job and `messenger_sent`, sent to the IGSID. A reply longer than 1,000 bytes is +split into parts, as Messenger splits at 2,000. `messaging_seen` marks parts read. The window, Try again and internal +notes work as on Messenger. + +### Data + +All migrations are expand only. + +- `messenger_connections` gains `instagram_account_id` (unique when set), `instagram_username` and + `instagram_enabled`. Each is nullable or defaulted, so the previous Worker is unaffected. +- `messenger_identities` and `messenger_sent` are reused unchanged. +- `channel_kind` gains the value `instagram`, checked in code. Every check that reads the combined channel already + treats anything but `web` as not the widget. Step 2 adds tests that say so for `instagram`. +- A new API minor version, for `instagram` as a channel and the Instagram fields on the Messenger connection routes. + +### Cost and responsibility + +Instagram is included in Cloud's free plan within its limits, and a new Instagram conversation counts towards the +monthly allowance as a Messenger one does. The business is in its own contract with Meta. The privacy documents and +DPA name Messenger but not Instagram, so the owner checks new wording before Cloud turns Instagram on. + +## Steps + +1. **Connecting, and the access question.** The migration, the tab with its check of the linked account, the webhook + accepting `instagram` (which only logs counts at this step), and the guide's setup page. Then a real test with OpenChat's own Page and an Instagram + professional account linked to it: a person with no role on the Meta app sends a direct message, and we record + whether the webhook hears it without App Review. The guide and this ADR are updated with the answer. +2. **Receiving.** Identities, contacts named from the profile, `instagram` conversations, attachments into R2, the + lines for story, share, view-once, reaction, unsend and unsupported messages, the Instagram mark in the dashboard + inbox, and export, erasure and retention, which already reach these tables. +3. **Replying.** Sending, the 1,000-byte split, Read, failures, echoes, the window, the assistant and translation. +4. **The apps**, in the private repository: the Instagram mark. They need a new store build. + +Each step is tested on real Cloudflare with a real Instagram account before it is called done. + +## Consequences + +- A business must have a Facebook Page linked to its Instagram account. One without a Page cannot connect until + Instagram Login is added, which is its own decision and brings a token-renewing job. +- Comments on posts are not in the inbox. +- GIFs and stickers a customer sends never reach OpenChat, because Meta sends no webhook for them. Known issues says so. +- Replies show Sent and Read but never Delivered. +- Without `HUMAN_AGENT`, a business cannot answer on Instagram more than 24 hours after the customer last wrote. +- If Instagram does need App Review for a business's own app, setup by keys takes days, and a single Orango Labs app + on Cloud ("Continue with Facebook") becomes the practical way, as ADR-0027 noted for Messenger.