Oct 23, 2025

Interest Lookup Workflow with n8n & Facebook API

Automating audience research makes scaling ad targeting faster and less error-prone. This step-by-step guide walks you through a complete n8n workflow that listens to Telegram for a hashtag, queries Facebook’s Graph API for ad interests, converts results into a CSV, and distributes the report back to Telegram and Slack. Why build an Interest Lookup Workflow? […]

Interest Lookup Workflow with n8n & Facebook API

Automating audience research makes scaling ad targeting faster and less error-prone. This step-by-step guide walks you through a complete n8n workflow that listens to Telegram for a hashtag, queries Facebook’s Graph API for ad interests, converts results into a CSV, and distributes the report back to Telegram and Slack.

Why build an Interest Lookup Workflow?

Marketers and growth teams often need to identify Facebook ad interests related to keywords quickly. Manually searching the Facebook Ads Manager or Graph API is slow. This n8n workflow automates the process so you can trigger a lookup from Telegram and receive a CSV report automatically — saving time and improving consistency.

Key features

  • Trigger from Telegram using a hashtag (#interest)
  • Filter messages to run only for a specific Telegram channel
  • Extract and parse the hashtag + search phrase
  • Query Facebook Graph API for ad interests
  • Flatten and normalize results into a CSV
  • Send the CSV to Telegram and notify Slack

Workflow overview

The workflow consists of a linear pipeline made with n8n nodes. Here’s the high-level flow:

  1. TelegramTrigger: listens for new messages
  2. MessageFilter (IF): ensures only messages from a specific channel and that start with #interest are processed
  3. MessageExtractor (Code): gets the raw text from the Telegram payload
  4. MessageSplitter (Code): regex to extract hashtag keyword and remaining search phrase
  5. FacebookGraphSearch: performs the Graph API call to search ad interests
  6. InterestsToTable (Code): turns nested JSON into row records
  7. ExtractVariables (Code): selects the important fields (name, audience sizes, description, path, topic)
  8. SpreadsheetCreator: writes the CSV file
  9. TelegramSender: uploads the CSV back to the channel
  10. SlackNotifier: posts a notification confirming the report was sent

Node-by-node breakdown

1. TelegramTrigger

Use the Telegram Trigger node to subscribe to message updates. Configure it with your bot’s API token and a webhook ID. The node receives the full Telegram message object, including chat id, text, and metadata.

2. MessageFilter (IF)

The IF node enforces two conditions: 1) the chat id equals your target channel (for example -1001805495093), and 2) the message text starts with #interest. If both are true the pipeline continues; otherwise the flow ends in a NoOperation node.

3. MessageExtractor (Code)

This small code node extracts the message text safely from the incoming JSON:

let inputData = items[0].json;
let messageContent = '';
if (inputData.message && inputData.message.text) {
  messageContent = inputData.message.text;
}
return [{ json: { messageContent } }];

4. MessageSplitter (Code)

Use a regex to capture the hashtag keyword and the remaining query text. The regex in the workflow is /#(\w+)\b(.*)/. This extracts the tag word (e.g., “interest”) and everything after it as the search phrase. The node outputs two fields: extractedContent and remainingContent.

5. FacebookGraphSearch

Configure the Facebook Graph API node to call the ad interest search endpoint:

search?type=adinterest&q={{ $json.remainingContent }}&limit=1000000&locale=en_US

Notes:

  • You must use a valid Facebook Graph API credential with the required permissions for ad interest search.
  • Keep locale and limit adjustable depending on your needs.

6. InterestsToTable (Code)

Facebook returns nested JSON. This code node iterates over the response and flattens keys and subkeys into an array of row-like objects containing: Item, SubItem, Value. That makes it easier to extract fields in the next step.

7. ExtractVariables (Code)

From the flattened rows, this code node normalizes and outputs the fields you care about for each interest:

  • name
  • audience_size_lower_bound
  • audience_size_upper_bound
  • path
  • description
  • topic

8. SpreadsheetCreator

Use the Spreadsheet node to convert the array of interest objects into a CSV file. Configure the node to output as CSV and set a filename like report.csv. This node will generate a binary file asset that can be sent to messaging platforms.

9. TelegramSender

The Telegram node uploads the CSV file back to the original channel as a document. Set chatId to your channel id and choose the sendDocument operation. Add a friendly caption if you want.

10. SlackNotifier

Optionally post a short message in Slack to confirm that the report was delivered. This keeps the team informed without checking Telegram constantly.

Setup & Permissions

Before running the workflow, ensure:

  • Your Telegram bot is added to the channel and has permission to read messages and send documents.
  • Your Facebook Graph API app has access to the Marketing API or the adinterest search endpoint. Follow Facebook’s docs to create an app, request access, and generate tokens. (See developers.facebook.com)
  • Your Slack token has permission to post to the target channel.

Testing and debugging tips

  • Test the MessageFilter conditions by sending messages and verifying which branch runs.
  • Use temporary debug nodes in n8n (or log outputs in code nodes) to inspect the Facebook response structure before flattening.
  • Start with a smaller limit on Graph API calls to avoid long responses during development.
  • Check API quotas and handle rate limits — add retries or pauses if you plan automated bulk lookups.

Security & Best Practices

  • Store credentials securely inside n8n credentials (do not hardcode tokens in code nodes).
  • Sanitize input from Telegram before using it in API queries to avoid injection-like issues.
  • Respect privacy and ads policy rules: don’t store sensitive PII and follow Facebook’s terms.
  • Use environment-specific tokens (dev/staging/prod) and rotate tokens periodically.

Optimize and extend

Consider these enhancements:

  • Cache frequent searches in a database to avoid repeated API calls.
  • Allow different hashtags (e.g., #audience) and map them to different locales or limits.
  • Send summary messages with top results (e.g., top 5 interests) before attaching a full CSV.
  • Store results automatically in Google Sheets or a data warehouse for long-term analysis.

Troubleshooting common errors

If Facebook returns an empty result set, verify the q parameter and the app’s access rights. If the CSV is missing fields, inspect the flattening code — Facebook may change response keys depending on API versions. If Telegram upload fails, confirm bot permissions and chat id correctness.

Conclusion & next steps

This n8n interest lookup workflow turns a chat-based request into an automated market-research report. It’s a flexible pattern you can adapt to other APIs, channels, and formats.

If you want a ready-to-import n8n workflow JSON or help integrating this into your workspace, contact us or download the sample workflow. Try sending #interest coffee in your configured Telegram channel to see the pipeline in action.

Call to action: Download the workflow JSON, import it into n8n, and test with your Facebook credentials. If you need help, reach out for a step-by-step setup session or a custom integration.

Leave a Reply

Your email address will not be published. Required fields are marked *