Jesus Vasquez
Hire me

Open source · MIT · Build an AI front desk, part 4

instagram-dm

Instagram DMs: webhooks, echoes and the 24-hour window. The plumbing for an Instagram DM bot that is easy to get subtly wrong: verifying webhooks, ignoring your own echoes, and not messaging outside the window.

  • TypeScript
  • Instagram API

Early (v0.x), written with Claude Code and directed and reviewed by me. Not on npm yet: clone the repository to use it.

A DM bot sounds like "receive message, send reply". In practice most of the work is the surrounding plumbing, and a mistake there either lets forged requests in or makes your bot talk to itself.

  1. Meta webhook
  2. Signature check
  3. Parse and dedupe events
  4. Your handler
  5. Send API (window rules)

Verify the webhook

Meta signs each webhook with an X-Hub-Signature-256 header. The check must be done over the raw request body, not the parsed JSON, and in constant time. The library does it so you cannot accidentally compare the wrong thing.

Dedupe redeliveries and ignore your own echoes

Webhooks can arrive more than once, and Instagram also sends you an event for messages your own account sent. Without filtering both, a bot replies twice, or replies to itself forever.

Respect the messaging window

As documented by Meta when the library was written, a business can message someone for 24 hours after that person's most recent message. A human following up can use a HUMAN_AGENT tag for up to 7 days, but never for automated messages. The client refuses to send outside the window before Meta does. Re-check Meta's current documentation before relying on these numbers.

import { evaluateWindow } from '@27colab/instagram-dm';

const w = evaluateWindow(lastInboundAt, { humanAgent: false });
if (!w.open) {
  // refuse to send; w.expiresAt says when it closed
}

Try the window

Instagram DMs: the 24-hour window

Instagram only lets a business message someone for a limited time after that person's last message. Drag the slider to say how long ago they wrote, then try to reply. Real instagram-dm (opens in a new tab) window logic. Rules as documented by Meta when the library was written; re-check Meta's current docs.

20 h ago

Window: open, 4.0 h left

Want to see a real one? Slide into the DMs at @27colab.

What this does not do

  • It is not an official Meta SDK and is not affiliated with Meta.
  • It has no language model, prompts, storage or framework: you plug in a conversation handler.
  • Pre-1.0, with a fake Graph API for offline tests.

Get it

git clone https://github.com/27colab/instagram-dm.git
cd instagram-dm && npm ci && npm test

Want this built into your product? Get in touch.