Skip to content

Latest commit

 

History

History
79 lines (51 loc) · 2.56 KB

File metadata and controls

79 lines (51 loc) · 2.56 KB

Tutorial: Webhooks for mentions and comments

This walks through subscribing to Instagram mentions and comments and receiving them in this app.

Prerequisites

  • App running with a public HTTPS URL (e.g. Cloudflare Tunnel) to http://localhost:8000
  • .env filled with valid Meta credentials
  • Instagram professional account linked to your app / Page

Step 1 — Verify the callback

  1. In Meta for Developers open your app → Webhooks.

  2. Select the Instagram webhook configuration (Instagram Graph / Instagram object).

  3. Set Callback URL to:

    https://YOUR-TUNNEL-HOST/webhook
    
  4. Set Verify token to the exact value of META_WEBHOOK_VERIFY_TOKEN in .env.

  5. Save. Meta sends GET /webhook?hub.mode=subscribe&hub.verify_token=...&hub.challenge=....

Your server must return HTTP 200 and a plain-text body equal to hub.challenge.

Manual check:

curl -sS "https://YOUR-TUNNEL/webhook?hub.mode=subscribe&hub.verify_token=YOUR_TOKEN&hub.challenge=test123"
# expect: test123

Step 2 — Subscribe to fields

In the same Webhooks → Instagram screen, subscribe to:

  • mentions
  • comments

Save. (Production may require Live mode and Advanced Access for comments — see Instagram webhooks.)

Step 3 — Receive events

When someone comments on your media or you get a mention (per Meta rules), Meta POSTs JSON to /webhook.

This app:

  1. Optionally verifies X-Hub-Signature-256 if INSTAGRAM_APP_SECRET is set.
  2. Stores each change in webhook_events.
  3. For comments, also inserts into tracked_comments if not already present.

Inspect deliveries:

curl -sS "http://localhost:8000/webhook/events?limit=20"

Step 4 — Load mention context from Graph

Webhook payloads include identifiers (e.g. media_id). Use:

GET /instagram/user/{INSTAGRAM_USER_ID}/mentioned_media/{media_id}

(Requires the right permissions; see the Instagram Graph docs.)

Troubleshooting

Symptom Check
Verify fails Tunnel up, token matches, URL is https, path /webhook
POST 403 Signature: set INSTAGRAM_APP_SECRET correctly or leave empty to skip
No events App Live mode, subscribed fields, test user in Development mode

References