Opening Problem Statement
Imagine Sarah, a data analyst at a tech news startup, who spends countless hours manually fetching trending stories from Hacker News and collating them into reports. She struggles to keep up with fast-changing popular posts and often wastes valuable time sifting through irrelevant data. Each update requires new queries, and the lack of automation introduces delays and occasional errors, costing the team critical insights and timely content.
This delay means missed opportunities to engage readers with the hottest tech trends. For Sarah, manually gathering this data is repetitive and inefficient, taking up hours per week she could spend on deeper analysis or creative tasks.
What This Automation Does
This unique n8n workflow streamlines the entire process by integrating an AI chat agent with a custom tool that fetches the top 50 posts ever from Hacker News in real-time. Here’s what happens:
- Real-Time Retrieval: Automatically pulls the latest top 50 Hacker News posts through a sub-workflow.
- Cleaned Data: Extracts and reformats key details like title, points, URL, author, and creation date for easy reading.
- AI-Powered Responses: Uses OpenAI’s chat model to understand and respond to specific user queries about Hacker News trends.
- Custom Tool Integration: The AI agent calls a bespoke tool workflow that focuses solely on Hacker News data, enabling precise and rapid answers.
- Interactive Chat Trigger: Receives manual chat messages to trigger data processing and replies, allowing dynamic, user-driven insights on demand.
- JSON Output Formatting: Serializes the final data neatly for downstream applications or further automation.
By automating these steps, Sarah saves hours weekly, avoids manual errors, and enables her team to have instant access to the most popular tech news, driving timely decisions and content creation.
Prerequisites ⚙️
- n8n account (Cloud or self-hosted) 🔑
- OpenAI API key for the OpenAI Chat model interaction 🔐
- Access to Hacker News API via the built-in Hacker News node in n8n 🔌
- Understanding of manual chat triggers using the Manual Chat Message node for input 💬
- Optional: Basic familiarity with JavaScript (for the Code node) 📜
For self-hosting, platforms like Hostinger can provide reliable infrastructure.
Step-by-Step Guide
1. Set Up the Manual Chat Message Trigger
Navigate to the Manual Chat Message node (“On new manual Chat Message”), which initiates the workflow when you input a chat prompt. This sets the starting point for interactive queries.
Tip: Try typing questions like What is the 5th most popular post ever on Hacker News?
This node listens for manual chat input to prompt the AI agent.
2. Configure the AI Agent Node
The AI Agent node is central here. Connect the manual chat trigger output to this node. In this configuration, set the option for a maximum of 10 iterations to allow the AI multiple passes for refining answers.
This agent manages the conversational AI logic and tool invocation.
3. Integrate the OpenAI Chat Model
Connect the OpenAI Chat Model node to the AI Agent. Use your OpenAI API credentials here. This node provides the language understanding and response generation capabilities.
OpenAI Chat Model turns raw input queries into meaningful dialogue and guides the AI agent.
4. Set Up the Custom Tool to Fetch Hacker News Data
Add the Custom Tool to call the workflow below node, linked to the AI Agent as a tool. This sub-workflow is configured to pull the top 50 all-time posts from Hacker News.
The tool references the same workflow ID for dynamic data access, forming a neat integration point.
5. Define the Execute Workflow Trigger inside the Sub-Workflow
Within the sub-workflow called by the custom tool, the Execute Workflow Trigger node activates the internal process of data fetching once called.
6. Fetch Data Using the Hacker News Node
After the trigger, the Hacker News node is configured to fetch 50 posts from the “all” resource. This ensures you get a broad range of top entries.
7. Clean Up Data with Set Node
Use the Clean up data node (Set) to extract essential fields like title, points, url, created_at, and author. These fields are picked directly from the JSON response, making the result user-friendly.
8. Format the Response with the Code Node
The Stringify Code node converts the array of post objects into a JSON string. The included JavaScript code is:
return {
'response': JSON.stringify($input.all().map(x => x.json))
}This prepares the data for output consumption.
9. Connect Sub-Workflow Output Back to AI Agent
Ensure the sub-workflow result is accessible as a tool output by linking the nodes correctly. This allows the AI agent to leverage data fetched live.
10. Add Sticky Notes for Documentation
Add Sticky Note nodes around the workspace to document purposes and instructions. These help maintain clarity for collaborators or future edits.
Customizations ✏️
- Change Number of Posts Fetched: In the Hacker News node inside the sub-workflow, adjust the
limitparameter from 50 to any number you prefer, allowing more or fewer posts. - Enhance AI Iterations: In the AI Agent node, increase or decrease
maxIterationsto control how deeply the agent processes queries. - Expand Data Fields: In the Clean up data node, add more fields like
num_commentsorstory_textto enrich the data provided. - Integrate Another Data Source: Replace or supplement the Hacker News node with other news APIs by adjusting the sub-workflow accordingly.
- Personalize Chat Prompts: Customize the manual chat trigger prompts or AI instructions to suit your team’s language and context.
Troubleshooting 🔧
Problem: AI Agent doesn’t respond or stalls
Cause: The OpenAI Chat Model node is not properly connected or lacks valid credentials.
Solution: Check your OpenAI API key is correctly configured under the node’s credentials. Ensure the node is connected to the AI Agent’s language model input.
Problem: Hacker News node returns empty or no data
Cause: API limit reached or network issues.
Solution: Verify your network connectivity. Restart the workflow. Check n8n’s API usage dashboard or logs for rate limits.
Problem: JSON formatting error in Code node
Cause: The input to the Code node is not correctly structured as expected.
Solution: Confirm upstream nodes output data arrays. The JavaScript code should map over all inputs and stringify the JSON. Adjust logic if data shape changes.
Pre-Production Checklist ✅
- Verify OpenAI API key is active and authorized.
- Test manual chat input triggers correct AI Agent response.
- Confirm sub-workflow returns valid Hacker News data.
- Inspect Clean up data node outputs expected fields.
- Run end-to-end test with example query “What is the 5th most popular post ever on Hacker News?”
- Backup workflow before production deployment.
Deployment Guide
Activate the workflow by toggling it ‘ON’ in n8n once all nodes are properly connected and tested. Monitor executions in the workflow run history to ensure smooth operation.
Use the built-in n8n interface for logs and error messages. You can set alerts or logs for failures to quickly react.
Conclusion
By building this AI-powered interactive workflow, Sarah can instantly retrieve and analyze the top Hacker News posts without manual intervention. This saves hours weekly and ensures her team stays on top of important tech conversations.
Next, consider expanding to include real-time alerts for new Hacker News posts or integrating other news platforms similarly. You’ve laid a solid foundation for AI-driven news analysis with n8n!