Lesson 615 min

Build an Instagram Auto-Reply AI Agent with n8n and ManyChat

Learn how to create an AI-powered agent that automatically replies to Instagram DMs using n8n for the AI logic and ManyChat for the Instagram integration.

Instagram Auto-Reply AI Agent Tutorial

In this tutorial, you'll build an AI agent that automatically responds to direct messages on Instagram. This is perfect for businesses or creators who want to instantly engage with their audience, answer common questions, or deliver resources based on specific keywords. We'll use two powerful tools: n8n for building the AI agent and its logic, and ManyChat to connect to Instagram and trigger the automation.


System Overview

The agent works in a seamless loop:

  1. A user sends a direct message with a specific keyword (e.g., "gym") to your Instagram account.
  2. ManyChat detects the keyword and triggers an automation.
  3. ManyChat sends the user's message and ID to an n8n webhook.
  4. An n8n AI Agent processes the message, generates a relevant response, and maintains a conversation history.
  5. n8n sends the AI-generated reply back to ManyChat.
  6. ManyChat sends the reply to the user on Instagram.

Part 1: Setting Up ManyChat

ManyChat will act as the bridge between Instagram and our n8n workflow.

Step 1: Connect Your Instagram Account

  1. Create a ManyChat account if you don't have one.
  2. Navigate to Settings > Channels > Instagram and connect your Instagram business account. Follow the on-screen instructions.

Step 2: Create a New Automation

  1. Go to Automation and click "New Automation".
  2. Select "Start from Scratch".

Step 3: Set Up the Keyword Trigger

  1. The first node in your automation is the trigger. Click on it and select "User sends a message with a keyword".
  2. Choose "Detect specific words in a message".
  3. Add the keywords you want to trigger the automation. For this example, add gym, link, and info. These are not case-sensitive.
  4. Click "Create".

Step 4: Store the User's Message

We need to save the user's message to send it to our n8n agent.

  1. Add a new Action node.
  2. Select Contact Data > Set User Field.
  3. Create a new user field by clicking the input box and selecting "New user field".
    • Name it user_message_from_dm.
    • Set the type to Text.
    • Click "Create".
  4. For the value, click the {} icon and select "Last Text Input". This stores the most recent message from the user in your newly created field.

Part 2: Building the n8n AI Agent

This is where the AI magic happens.

Step 1: Create the Webhook Trigger in n8n

  1. In a new n8n workflow, add a Webhook node.
  2. Change the HTTP Method to POST.
  3. Customize the path for clarity, e.g., /manychat.
  4. Copy the Test URL.

Step 2: Connect ManyChat to n8n

  1. Go back to your ManyChat automation.
  2. Add another Action node and select "Make an external request".
  3. Paste the n8n webhook URL into the "Request URL" field.
  4. Go to the "Body" tab and add a JSON object to send data to n8n. We need to send the user's message and their ID.
{
  "user_message": "{{user_message_from_dm}}",
  "user_id": "{{contact.id}}"
}
  • {{user_message_from_dm}} is the custom field we created.
  • {{contact.id}} is a built-in ManyChat variable for the user's unique ID.
  1. In n8n, click "Listen for test event" on the Webhook node.
  2. In ManyChat, click "Test the request". You should see the data arrive successfully in n8n.

Step 3: Create the AI Agent in n8n

  1. Add an AI Agent node after the Webhook.

  2. Set Source for the prompt to "Define Below".

  3. Configure the prompts:

    System Prompt:

    You are FitBot, an AI gym assistant that helps people improve their fitness journey. Your goal is to understand what the user needs (workout plans, nutrition advice, etc.) and provide helpful, encouraging, and clear information. Keep replies concise and friendly.
    

    User Prompt: Use an expression to insert the message from the webhook: {{ $json.body.user_message }}

  4. Set up the Chat Model:

    • Choose your preferred model (e.g., OpenAI's GPT-4o Mini).
    • Connect your credentials.
  5. Configure Memory:

    • Choose "Simple Memory".
    • For the Key, use the user's ID from the webhook: {{ $json.body.user_id }}. This creates a unique conversation history for each user.
    • Set a Context Window Length (e.g., 10) to remember the last 10 messages.

Step 4: Respond to the Webhook

  1. Add a Respond to Webhook node. This node will send the AI's output back to ManyChat.
  2. Ensure the Respond With option is set to "All Incoming Items".

Step 5: Finalize and Activate the n8n Workflow

  1. In the Webhook node, under "Respond", select "Using Respond to Webhook node".
  2. Save and activate your n8n workflow.
  3. Copy the Production URL from the Webhook node.

Part 3: Completing the ManyChat Automation

Let's finish the loop in ManyChat.

Step 1: Update the Webhook URL

  1. In your ManyChat external request action, replace the test URL with the Production URL from your n8n webhook.

Step 2: Map the AI Response

  1. In the same external request node, go to the "Response Mapping" tab.
  2. We need to store the AI's reply. Create another custom field named ai_assistant_reply (Type: Text).
  3. Map the response from n8n to this new field. The path to the data will likely be $.0.output.

Step 3: Send the Reply on Instagram

  1. Add a new step in your automation and choose "Instagram".
  2. Select "Send Message".
  3. In the text box, click the {} icon and insert the ai_assistant_reply custom field. This will send the AI's generated message to the user.

Step 4: Loop the Conversation

To make the conversation continuous, we need to create a loop.

  1. After the Instagram message is sent, add a "Data Collection" element. This will wait for the user's next reply.
  2. Save the user's new reply back to the user_message_from_dm field, overwriting the previous one.
  3. Add a "Go to step" action and connect it back to the "Make an external request" node. This sends the new message back to n8n, and the conversation continues.

Step 5: Go Live!

  1. Click "Update" or "Set Live" in the top-right corner of your ManyChat automation.

Now, when a user sends your keyword to your Instagram account, the AI agent will take over and handle the conversation!