Free

How to build an Instagram bot: keyword, Direct message and comment reply

Build an Instagram keyword bot for a professional account. Prepare the code, connect it through Meta and verify that the promised resource reaches the right person.

Instructions

Someone writes a keyword under your post. The bot sends them a link in Direct and replies below the comment. Build this workflow for one professional Instagram account: prepare the code, connect it through Meta and verify actual delivery.

I use this mechanism for Codex Bible. Below is a separate practice kit you can adapt to your own material. Fixed messages need neither a language model nor a paid intermediary. Costs depend on your chosen host.

You need a public professional Instagram account of type Business or Creator, access to Meta for Developers, material at a public link and another account for testing. Convert a personal Instagram account to a professional one first. The Instagram Login method chosen here does not require linking it to a Facebook Page.

Paste the prompts in order into one Work/Codex project. The assistant performs available actions itself. You complete sign-in, access confirmation and mandatory identity verification when the service requires your personal participation. Do not send tokens or passwords to the chat.

Download the practice kit (ZIP). It contains a server, settings dashboard, queue, tests, example variables and MIT license. The archive has no working keys or user data. You may copy and modify it.

Step 1. Choose the keyword and replies

Start with one post and one resource. Set the word, message and link. For example, someone writes “codex” and receives: “Here is the promised resource. The link is below.” The bot posts its public reply only after Direct sending has been confirmed.

Prepare six short public replies. They rotate in order. Here is an example for the Bible; replace its name for your own resource:

  1. I sent the Codex Bible link in Direct.
  2. Codex Bible is now in your private messages.
  3. The Codex Bible link is in Direct. Check message requests if you cannot see it.
  4. Done, the Codex Bible link has been sent to your messages.
  5. Look in Direct for the Codex Bible link.
  6. I sent Codex Bible. Check your private messages and requests.

Rotation changes the wording but does not replace Meta’s access rules and restrictions. In this workflow, the bot delivers prepared material rather than holding an open-ended conversation.

Prepare an Instagram bot workflow for my professional account: a keyword under a post, a Direct message with a link, then a public reply. First find the existing project and agreed decisions. Clarify only what is missing: account, one post or all posts, keyword, resource, link and wording. Prepare six public replies of no more than 300 characters each, a short brief and test criteria. Do the available work yourself, but do not enable real sending yet.
Ask questions to make it better and suggest improvements.

Step 2. Prepare and test the practice project

Codex downloads the archive, unpacks it into a separate folder and checks Node.js. Use Node.js 24 LTS for this kit. No extra npm packages are needed: the server and SQLite are included in Node.js. SQLite stores settings and the queue in one file.

Tests must pass before connecting Instagram. The assistant then creates .env from .env.example. Do not start the server while real values are missing. For a general walkthrough of a project folder, open Run an existing project on your computer.

Find the Instagram bot project for the current task. If the practice kit is missing, download https://codexbible.org/assets/instagram-keyword-bot-en.zip and unpack it into a separate folder. If you cannot download the archive, ask me to open the link in a browser and attach the ZIP. The library and its practice materials are free. Read README.md and the code. Check Node.js 24 LTS and run node --test test.mjs http-test.mjs. Create .env from .env.example using a method appropriate for my OS; do not expose secrets in chat. Save the test result and missing settings list. Do not start real sending.

Step 3. Create a Meta app and select Instagram

Open the Meta app list. Reuse an app for this task if one exists. For a new one, click “Create app”: enter a name and contact address, then choose the “Manage messaging and content on Instagram” use case.

The screenshot shows the wizard’s first screen. The contact address was cleared before capture. This is a screenshot of the form; no new app was created while preparing this guide.

Start of Meta app creation: name, contact address and wizard stages

The wizard may also show a business selection and app requirements. Specify only your own business, if you have one, and actual information. Do not choose someone else’s business portfolio just to continue. If the dashboard requires business verification, complete it under Meta’s rules.

After creation, open “Use cases” and click “Customize” for Instagram. This is how the section looks in an existing app:

The Instagram messaging and content use case and its Customize button

An older wizard may offer “Other,” then “Business,” then the Instagram product. Follow the screen actually shown. The result is the same: Instagram API configuration is available in the app.

Find the available Meta for Developers session and app for my Instagram bot. Reuse a suitable existing app. If none exists, prepare creation with the “Manage messaging and content on Instagram” use case using my actual information. Compare current screens with https://developers.facebook.com/documentation/instagram-platform/create-an-instagram-app . Do not substitute someone else’s business. Perform available actions; if personal sign-in, accepting terms or Meta verification is needed, show the specific step. Result: the chosen app and open Instagram API configuration.

Step 4. Choose Instagram Login and permissions

In the use-case configuration, open “API setup with Instagram login.” A separate Facebook login section is nearby: this kit uses Instagram Login specifically.

The top shows the Instagram app’s name, ID and secret. Its parent Meta app ID may differ. Below, the dashboard offers instagram_business_basic, instagram_business_manage_comments and instagram_business_manage_messages permissions.

Instagram Login configuration: app ID and required permissions

The first permission gives profile access, the second comments and the third messaging functions. The Private Replies reference lists the first two for a single private reply to a comment; the messaging configuration screen offers all three. Check the permissions for the specific sending method. Content publication and insights are unnecessary for delivering a link.

Do not reveal the secret for a screenshot. The server needs the secret of the Instagram app subscribed to events. Codex later checks it through the webhook signature.

In the chosen Meta app, open Instagram API with Instagram Login. Check instagram_business_basic, instagram_business_manage_comments and the messaging permissions required by the current configuration. Compare https://developers.facebook.com/documentation/instagram-platform/instagram-api-with-instagram-login and https://developers.facebook.com/documentation/instagram-platform/private-replies . Distinguish the parent Meta app ID, Instagram app ID and account ID. Prepare server settings without exposing secrets. Do not request content publication or insights just to send a link.

Step 5. Add the account and obtain a token

In “Generate access tokens,” click “Add account.” Meta says the account must be professional and public. Testing uses an Instagram tester role and acceptance of the invitation by the account itself.

Meta explains professional account and tester invitation requirements

Continue signing in with the correct Instagram account. If a tester invitation appears, open Instagram in a browser: “Settings,” “Website permissions,” “Apps and websites,” then “Tester invites.” You can go directly to Apps and websites and choose that tab. Accept your app’s invitation. Return to Meta and check that the account appears in the list. For a developer-role error, check “App roles,” then “Roles,” the assigned account and invitation acceptance.

The Tester invites tab in Instagram settings

The screenshot shows where the tab is. Your app’s actual invitation appears after it is sent from Meta.

Next to the connected account, select “Generate token,” complete sign-in and save the token only in server settings. The Meta guide gives dashboard-generated tokens a 60-day lifetime; a normal initial OAuth token lasts one hour. Codex should check your token’s actual expiry and prepare a renewal reminder. The practice code does not renew it automatically.

Ask Codex to obtain user_id through GET /me?fields=user_id,username. This is the professional Instagram account ID for INSTAGRAM_ACCOUNT_ID, not the app ID. Choose an API version supported in your dashboard and save it in GRAPH_VERSION.

Help connect my professional account in the Instagram Login token-generation section. Check assignment and acceptance of the tester role if Meta requires it. Carry out available steps to obtain an Instagram User Access Token. Ask only for necessary personal sign-in or access confirmation. Save the token as INSTAGRAM_ACCESS_TOKEN on the server, not in chat. Using current official documentation, perform a safe GET /me with fields=user_id,username, verify the intended account and save user_id as INSTAGRAM_ACCOUNT_ID. Record the API version, token expiry and renewal method without revealing keys. Leave the workflow disabled.

Step 6. Start the server before configuring Webhooks

Meta needs a public HTTPS address for a running server. Deploy the code first, then return to the callback. A webhook, or callback, means Meta itself sends a new-comment event to this address.

The main route can use Railway with a persistent volume. An alternative is Render with a persistent disk. Codex checks prices and storage terms before choosing. Free hosting with temporary disk storage is unsuitable for the SQLite queue.

On Railway, configure one application instance, a /data volume, DB_PATH=/data/bot.sqlite and node server.mjs as the start command. Render may use a different disk path; it must match DB_PATH. During updates, do not run two instances against the same database. Prepare backups and a restoration check.

Variable What to save
INSTAGRAM_ACCESS_TOKEN The connected Instagram account’s token
INSTAGRAM_ACCOUNT_ID Its user_id, checked through the API
META_APP_SECRET The Instagram app secret for checking event signatures
META_VERIFY_TOKEN A random string Codex creates to verify the callback
ADMIN_TOKEN A different random key of at least 32 characters for the bot dashboard
GRAPH_VERSION An API version supported in the Meta dashboard
DB_PATH SQLite path on persistent storage

These are different keys for different purposes. Set values in the hosting dashboard. Locally, Codex starts the server with node --env-file=.env server.mjs. On the deployed server, /health should respond and settings requests without ADMIN_TOKEN should be rejected.

Prepare deployment of the practice Instagram bot to a continuously running server. Compare current Railway volume and Render persistent disk terms, and show the cost before any paid action. Use Node.js 24 LTS, one instance, persistent DB_PATH and HTTPS. Transfer required variables into the server environment without exposing secrets. Check /health, rejection of /api/rule without a key, settings persistence after restart and backup restoration. Prepare a factual description of stored data and its deletion method for the app policy. Leave the workflow disabled. Save the exact server URL and its /webhook for the next step.

Step 7. Connect Webhooks in Meta

Return to “API setup with Instagram login” and expand “Configure webhooks.” In “Callback URL,” enter your server’s HTTPS address followed by /webhook. In “Verify token,” enter META_VERIFY_TOKEN. Click “Verify and save.”

Meta sends a verification request. The server must return the received hub.challenge value. Then check that the comments field has a subscription.

Callback URL, verification token and comments subscription in Meta

The screenshot shows my live service’s address with a different path. Do not copy it: the practice kit receives events at /webhook. The screenshot also includes extra subscriptions used by the live app; for this lesson, check comments and preserve the others.

There is another subscription: the Instagram account itself subscribes to the app. Codex runs node --env-file=.env setup.mjs. The script reads subscribed_apps, preserves existing fields and adds comments. Successful callback saving and account subscription need separate checks.

Meta signs event requests. The server checks X-Hub-Signature-256 against the raw request body using META_APP_SECRET. Do not enable the bot if it accepts an invalid signature. The “Test” button proves callback availability; a real comment is checked later.

Connect the practice Instagram bot webhook without real sending. Find the server’s public URL and append /webhook. Set it as the Callback URL in Meta and use META_VERIFY_TOKEN from the environment without exposing it in chat. Check the challenge, HMAC SHA-256 of the raw body and rejection of an invalid signature. Confirm the comments subscription. Run setup.mjs to subscribe the account itself through subscribed_apps, preserve existing fields and verify by reading again. Distinguish callback availability, app subscription, account subscription and receipt of a real event. Leave the workflow disabled.

Step 8. Check access before real comments

“Published” status and approval of required permissions are separate checks. The current Meta dashboard explicitly says an app needs “Published” status for Webhooks notifications. The publication switch alone does not grant advanced access.

Check “Permissions and features,” “Testing,” “Publish” and app review requirements. Access conditions differ for your own professional account and for connecting professional accounts owned by others. Check whether standard access allows your workflow with assigned roles. Connecting other owners’ accounts requires advanced access and App Review. The presence of an ordinary commenter does not itself determine the required access level: check the chosen API and dashboard requirements.

Meta’s Publication screen reminds you to check use cases and app approvals

This step is ready when Codex records the app status, available permissions and the account allowed for the next test. If your workflow requires App Review, stay within Meta’s permitted testing until approval.

The “Complete app review” section lists Meta’s required actions. These may include a privacy policy, data deletion, business verification, use-case description and screen recording. The recording must show actual use of the requested permissions. A screenshot from this guide cannot replace it.

If users connect through your own login button, you also need “Set up Instagram business login”: an allowed OAuth redirect URI, deauthorization URL and data deletion URL. The practice kit sets its token on the server and has no OAuth dashboard of its own.

Check whether the Instagram bot’s Meta app is ready for the chosen audience. Separately check publication status, tester roles, required permission levels and App Review requirements. Use the dashboard and official documentation to establish which accounts may participate in the test and what ordinary users require. Prepare any missing policy, data deletion, use-case description and screen-recording plan based on the actual code. Do not invent legal information or review results. If personal Meta confirmation is required, show the exact step. Do not enable broad processing before requirements are met.

Step 9. Configure the six replies and queue

Open the bot dashboard at the server URL, enter ADMIN_TOKEN and click “Open settings.” Enter the keyword, message, HTTPS link, post ID and six replies, one per line. Codex obtains the post ID through your account’s API and checks it against the chosen post; a post URL is not its numeric ID.

The reply order survives text edits and restarts. A disabled workflow stays disabled. Leave the ID field empty for all posts; for a first run, use one test post.

“CODEX!” matches “codex,” while “codexes” does not. For a new matching comment, the bot saves the job and chosen reply in one database operation. A repeat of the same event does not advance the order. Each author also receives the resource only once per post, even if they delete and write the comment again.

The Direct message goes first through /{IG_ACCOUNT_ID}/messages with recipient.comment_id, followed by a reply through /{COMMENT_ID}/replies. The queue keeps the job’s original text even if you later change settings. Reply rotation applies to new jobs: sending errors may change the order of replies visible on Instagram.

Configure the practice Instagram bot dashboard according to the agreed brief. Obtain and check the intended post ID through the API; do not use its URL as an ID. Save six public_replies and a nonempty public_reply. Make sure the existing reply_cursor is preserved and text editing does not enable a disabled workflow. Run checks for the 1/2/3/4/5/6/1 cycle, repeated events and authors, persistence after restart, Direct before public reply and two concurrent handlers. Use practice events without calls to Meta. Do not enable real sending yet.

Step 10. Verify real delivery and disabling

Once settings and access have been checked, enable the workflow for the chosen post. Leave a fresh keyword comment from another account allowed by the previous step’s access checks. Compare that exact comment, queue record, Direct message and public reply. Check message requests if the message is absent from the inbox.

A full cycle needs seven new eligible jobs: for example, seven different authors on one post or comments on seven posts covered by the rule. Record the current position first: an existing workflow may not start at the first reply. If you have tested only one comment, state that.

Under Private Replies rules, a reply to a comment on an ordinary post or Reels must be sent within seven days. This is one private message; further conversation becomes possible after the recipient replies and within Meta’s established window. Live broadcasts are outside this practice workflow.

Log status What happened
queued The job is waiting
sending The request is being sent
sent Meta confirmed the Direct message; separately check the public reply error field
failed Meta rejected the request
unknown No confirmation was received; check Direct manually first
skipped The job was skipped, for example because of comment age or a disabled workflow

For unknown, the code does not automatically resend the Direct message. A public reply error also does not trigger another Direct send. sent does not mean the person read the message.

After testing, disable the workflow and confirm that new comments trigger no sends. Reenable it only for the agreed post. The kit’s internal limiter allows 100 attempts per hour; this is an example setting, not a universal Meta quota.

Run acceptance checks for the configured Instagram bot. Repeat local tests without Meta first. Then agree with me on the post and test time, enable only the intended workflow and wait for my fresh comment from another eligible account. Match the exact comment, incoming event, queue, confirmed Direct message and separate public reply. Do not create jobs from old comments to imitate automatic operation. Check repeats, the reply sequence actually available and disabling. Do not resend automatically when delivery is unknown. Save a report without tokens or private conversations; state what was checked, what remains and whether the workflow is enabled at the end.

Find the cause when the bot stays silent

What you see What to check
Meta’s test arrives, but a real comment does not App status, access, roles, comments and account subscription through subscribed_apps
The comment can be read through the API, but there is no job Webhook receipt, signature, account ID, keyword, post ID and workflow enablement
Direct arrived, but there is no public reply Comment access and the separate replies sending error
Meta reports an insufficient developer role The signed-in account, assigned role and invitation acceptance
The bot stops after reconnection The new token’s type, expiry and permissions, account ID and comments subscription
The queue disappears after an update DB_PATH and persistent disk attachment

Start where the event disappeared. Do not recreate the entire app or change the business at random.

What you will have and how to continue

The finished result: a fresh comment triggers one message with a link and one public reply, repeats cause no new delivery, settings survive a restart and the bot can be disabled from its dashboard. Your account’s launch is considered checked only after this test.

The practice kit was checked locally with automated tests without Instagram. Screenshots were captured in the actual Meta dashboard on October 18, 2026; they show an existing app’s configuration. Connecting this kit to a separate account, testing seven real comments and running it on Windows were not performed while preparing the article.

For questions about prices and terms, follow Build an assistant for customer replies. Check its drafts first, then connect a working channel.

To assign the entire route as one task, paste this prompt into Work/Codex:

Help launch an Instagram bot: a keyword under a post, one Direct message with a link, then one of six rotating public replies. Find the existing project and settings; do not duplicate a working bot. If no project exists, use the practice kit https://codexbible.org/assets/instagram-keyword-bot-en.zip ; if access is restricted, ask me to attach the downloaded archive. Read README, check Node.js 24 LTS and run the tests. Prepare a server with persistent storage, a professional account connection through Meta Instagram Login, permissions, token, signed webhook and the account’s comments subscription. Check publication and App Review requirements for my audience. Perform available actions yourself and request only missing information and mandatory personal confirmations. Do not expose secrets in chat. Preserve the cursor and disabled state during editing. Finish by checking a fresh comment, Direct message, public reply, repeats and disabling; report the actual result, cost and remaining limitations.
Ask questions to make it better and suggest improvements.

What to try next

In this guide
Saved