chatwoot/app/models
Muhsin Keloth d827e66453
feat: Instagram Inbox using Instagram Business Login (#11054)
This PR introduces basic minimum version of **Instagram Business
Login**, making Instagram inbox setup more straightforward by removing
the Facebook Page dependency. This update enhances user experience and
aligns with Meta’s recommended best practices.

Fixes
https://linear.app/chatwoot/issue/CW-3728/instagram-login-how-to-implement-the-changes


## Why Introduce Instagram as a Separate Inbox?


Currently, our Instagram integration requires linking an Instagram
account to a Facebook Page, making setup complex. To simplify this
process, Instagram now offers **Instagram Business Login**, which allows
users to authenticate directly with their Instagram credentials.

The **Instagram API with Instagram Login** enables businesses and
creators to send and receive messages without needing a Facebook Page
connection. While an Instagram Business or Creator account is still
required, this approach provides a more straightforward integration
process.

| **Existing Approach (Facebook Login for Business)** | **New Approach
(Instagram Business Login)** |
| --- | --- |
| Requires linking Instagram to a Facebook Page | No Facebook Page
required |
| Users log in via Facebook credentials | Users log in via Instagram
credentials |
| Configuration is more complex | Simpler setup |

Meta recommends using **Instagram Business Login** as the preferred
authentication method due to its easier configuration and improved
developer experience.

---

## Implementation Plan

The core messaging functionality is already in place, but the transition
to **Instagram Business Login** requires adjustments.

### Changes & Considerations

- **API Adjustments**: The Instagram API uses `graph.instagram`, whereas
Koala (our existing library) interacts with `graph.facebook`. We may
need to modify API calls accordingly.
- **Three Main Modules**:
  1. **Instagram Business Login** – Handle authentication flow.
2. **Permissions & Features** – Ensure necessary API scopes are granted.
  3. **Webhooks** – Enable real-time message retrieval.

![CleanShot 2025-03-10 at 21 32
28@2x](https://github.com/user-attachments/assets/1b019001-8d16-4e59-aca2-ced81e98f538)


---

## Instagram Login Flow

1. User clicks **"Create Inbox"** for Instagram.
2. App redirects to the [Instagram Authorization
URL](https://developers.facebook.com/docs/instagram-platform/instagram-api-with-instagram-login/business-login#embed-the-business-login-url).
3. After authentication, Instagram returns an authorization code.
5. The app exchanges the code for a **long-lived token** (valid for 60
days).
6. Tokens are refreshed periodically to maintain access.
7. Once completed, the app creates an inbox and redirects to the
Chatwoot dashboard.

---

## How to Test the Instagram Inbox

1. Create a new app on [Meta's Developer
Portal](https://developers.facebook.com/apps/).
2. Select **Business** as the app type and configure it.
3. Add the Instagram product and connect a business account.
4. Copy Instagram app ID and Instagram app secret
5. Add the Instagram app ID and Instagram app secret to your app config
via `{Chatwoot installation
url}/super_admin/app_config?config=instagram`
6. Configure Webhooks:
   - Callback URL: `{your_chatwoot_url}/webhooks/instagram`
   - Verify Token: `INSTAGRAM_VERIFY_TOKEN`
- Subscribe to `messages`, `messaging_seen`, and `message_reactions`
events.
7. Set up **Instagram Business Login**:
   - Redirect URL: `{your_chatwoot_url}/instagram/callback`
8. Test inbox creation via the Chatwoot dashboard.


## Troubleshooting & Common Errors

### Insufficient Developer Role Error

- Ensure the Instagram user is added as a developer:
- **Meta Dashboard → App Roles → Roles → Add People → Enter Instagram
ID**

### API Access Deactivated

- Ensure the **Privacy Policy URL** is valid and correctly set.

### Invalid request: Request parameters are invalid: Invalid
redirect_uri

- Please configure the Frontend URL. The Frontend URL does not match the
authorization URL.
---


## To-Do List

- [x] Basic integration setup completed.  
- [x] Enable sending messages via [Messaging
API](https://developers.facebook.com/docs/instagram-platform/instagram-api-with-instagram-login/messaging-api).
- [x] Implement automatic webhook subscriptions on inbox creation.  
- [x] Handle **canceled authorization errors**.  
- [x] Handle all the errors
https://developers.facebook.com/docs/instagram-platform/instagram-graph-api/reference/error-codes
- [x] Dynamically fetch **account IDs** instead of hardcoding them.  
- [x] Prevent duplicate Instagram channel creation for the same account.
- [x] Use **Global Config** instead of environment variables.  
- [x] Explore **Human Agent feature** for message handling.  
- [x] Write and refine **test cases** for all scenarios.  
- [x] Implement **token refresh mechanism** (tokens expire after 60
days).
Fixes https://github.com/chatwoot/chatwoot/issues/10440

---------

Co-authored-by: Sivin Varghese <64252451+iamsivin@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Shivam Mishra <scm.mymail@gmail.com>
2025-04-08 10:47:41 +05:30
..
channel feat: Instagram Inbox using Instagram Business Login (#11054) 2025-04-08 10:47:41 +05:30
concerns feat: Instagram reauthorization (#11221) 2025-04-03 14:30:48 +05:30
integrations feat(apps): Shopify Integration (#11101) 2025-03-19 15:37:55 -07:00
access_token.rb Feature: Access tokens for API access (#604) 2020-03-11 00:02:15 +05:30
account_user.rb feat: Add APIs to manage custom roles in Chatwoot (#9995) 2024-08-23 17:18:28 +05:30
account.rb chore: Clean up report & knowledge base policies (#11234) 2025-04-03 16:00:32 -07:00
agent_bot_inbox.rb Feature: Access tokens for API access (#604) 2020-03-11 00:02:15 +05:30
agent_bot.rb fix: Delete agent bots without deleting the messages (#7754) 2023-08-17 13:48:49 -07:00
application_record.rb chore: fix sla email notifications (#9192) 2024-04-04 21:16:49 +05:30
article.rb chore: Limit the number of articles retrieved by widget (#11095) 2025-03-15 14:10:12 -07:00
attachment.rb feat: Show shared contact's name in Telegram channel (#10856) 2025-02-11 19:39:54 +05:30
automation_rule.rb feat: validate query conditions (#10595) 2024-12-17 17:16:37 +05:30
campaign.rb chore: Add validations to campaign model 2025-03-19 17:29:08 -07:00
canned_response.rb chore: Apply fixes for items in rubocop_todo [CW-1806] (#8864) 2024-02-07 13:36:04 +04:00
category.rb feat: Adds the ability to set an emoji for help center category (#8111) 2023-10-20 13:52:30 +05:30
contact_inbox.rb feat: Add job to remove stale contact inboxes (#8096) 2023-11-06 20:24:30 -08:00
contact.rb feat: add job to remove stale contacts and contact_inboxes (#11186) 2025-03-28 12:18:39 +05:30
conversation_participant.rb chore: Add controllers for conversation participants (#6462) 2023-02-15 16:33:31 -08:00
conversation.rb feat: Add support for multi-language support for Captain (#11068) 2025-03-19 18:25:33 -07:00
csat_survey_response.rb chore: Apply fixes for items in rubocop_todo [CW-1806] (#8864) 2024-02-07 13:36:04 +04:00
custom_attribute_definition.rb chore: Ability to filter conversations with priority (#10967) 2025-03-03 16:38:22 +05:30
custom_filter.rb fix: Revert the changes for SyncCustomFilterCountJob (#8238) 2023-10-27 10:45:48 -07:00
dashboard_app.rb fix: validate url for Dashboard Apps [CW-2979] (#8736) 2024-01-18 17:48:30 +05:30
data_import.rb chore: Add delay before running dataimport job (#8039) 2023-10-03 22:18:57 -07:00
email_template.rb feat: Customisable Email Templates (#1095) 2020-08-06 15:21:06 +05:30
folder.rb feat: Portal endpoint (#4633) 2022-05-16 13:59:59 +05:30
inbox_member.rb feat: auditlog for team and inbox member updates (#7516) 2023-08-15 19:55:19 -07:00
inbox.rb feat: Instagram reauthorization (#11221) 2025-04-03 14:30:48 +05:30
installation_config.rb chore: Enable the new Rubocop rules (#7122) 2023-05-19 14:37:10 +05:30
integrations.rb Feature: Slack integration (#783) 2020-06-12 23:12:47 +05:30
jsonb_attributes_length_validator.rb Fix: added validation for custom and additional attribute (#4260) 2022-03-24 15:38:28 +05:30
kbase.rb Feature: Knowledge Base APIs (#1002) 2020-09-26 02:32:34 +05:30
label.rb feat: multiple UX improvements to labels (#7358) 2023-06-25 18:49:49 +05:30
macro.rb feat: Add webhook event support for macros (#11235) 2025-04-02 20:26:55 -07:00
mention.rb fix: Notification page breakages (#5236) 2022-08-10 13:46:46 +02:00
message.rb chore: Remove delete Instagram story implementation (#11097) 2025-03-25 09:44:26 +05:30
note.rb feat(ee): Add Captain features (#10665) 2025-01-14 16:15:47 -08:00
notification_setting.rb fix: Specify external db with non-standard port (#2711) 2021-07-28 19:36:51 +05:30
notification_subscription.rb fix: Change the column identifier from string to text to avoid overflow (#9073) 2024-03-07 11:13:01 +05:30
notification.rb feat(v4): Update Inbox view card design (#10599) 2025-01-07 21:11:54 +05:30
platform_app_permissible.rb fix: SuperAdmin Improvements (#3733) 2022-01-11 19:00:00 -08:00
platform_app.rb Chore: Replaced dependent destroy with dependent destroy_async in all models (#3249) 2021-11-18 10:32:29 +05:30
portal.rb chore: Clean up report & knowledge base policies (#11234) 2025-04-03 16:00:32 -07:00
related_category.rb feat: CRUD operation for associated articles to current article (#4912) 2022-07-04 20:29:44 +05:30
reporting_event.rb feat: add index to reporting events [CW-1960] (#7294) 2023-06-12 18:23:42 +05:30
super_admin.rb chore: Remove message_signature_enabled from users (#3982) 2022-02-15 12:10:53 +05:30
team_member.rb feat: auditlog for team and inbox member updates (#7516) 2023-08-15 19:55:19 -07:00
team.rb feat: invalidate cache after inbox members or team members update (#10869) 2025-02-20 21:28:38 -08:00
telegram_bot.rb chore: Apply fixes for items in rubocop_todo [CW-1806] (#8864) 2024-02-07 13:36:04 +04:00
user.rb chore: Clean up report & knowledge base policies (#11234) 2025-04-03 16:00:32 -07:00
webhook.rb feat: Add inbox webhook events (#8006) 2023-09-28 15:28:10 -07:00
working_hour.rb chore: Replace deprecated functions (#5611) 2022-10-12 14:55:59 -07:00