n8n Automation ProjectπŸš€

n8n Automation ProjectπŸš€

Table of Contents

Belgian Train Departure Agent: An n8n Automation Project

Overview

This project showcases a powerful n8n workflow that creates an intelligent Belgian train departure agent. The system integrates with the iRail API to provide real-time train departure information through Telegram bot interactions and scheduled checks, demonstrating the capabilities of low-code automation for real-world transportation data.

Features

  • Real-time Train Data: Fetches live departure information from Belgian railway stations via the iRail API
  • Telegram Bot Integration: Users can query train departures through natural language messages
  • Smart Station Matching: Intelligent fuzzy matching algorithm to find stations from user input
  • Multiple Input Methods: Supports both webhook triggers and scheduled automated checks
  • Rich Formatting: Displays departure information with platform details, delays, and status indicators
  • Error Handling: Robust error handling for invalid station names or API failures

Architecture

The workflow consists of several key components working in harmony:

Core Components

  1. Webhook Trigger (Webhook): Listens for POST requests at /train-departures endpoint
  2. Message Parser (Code in JavaScript1): Extracts city and time information from natural language messages
  3. Station Finder (Find Station): Matches user input to official Belgian station names using fuzzy logic
  4. API Integration (Get Stations & Get Liveboard): Retrieves station data and live departure information
  5. Response Formatter (Code in JavaScript3): Formats departure data into user-friendly messages
  6. Telegram Bot (Send to Telegram): Delivers formatted results to users

Additional Features

  • Schedule Trigger: Enables automated departure checks at specified intervals
  • Chat Interface: Provides an interactive chat-based interface for users
  • HTTP Request Nodes: Allow testing and integration with external systems

Technical Implementation

Natural Language Processing

The system uses sophisticated regex patterns to parse user messages:

const patterns = [
  /departures?\s+from\s+([^\s]+)\s+at\s+(\d{1,2}:?\d{0,2})/i,
  /([^\s]+)\s+(\d{1,2}:?\d{0,2})/,
  /([^\s]+)\s+at\s+(\d{1,2}:?\d{0,2})/i
];

Station Matching Algorithm

The fuzzy matching system scores stations based on:

  • Perfect match: Exact name match (100 points)
  • Close match: Partial name inclusion (80 points)
  • Weak match: First three characters (40 points)

Response Formatting

The system displays up to 5 departures with rich formatting:

  • Train numbers and destinations
  • Departure times with delay information
  • Platform numbers
  • Real-time status (on time, delayed, or canceled)

API Integration

iRail API Endpoints

  • Stations: https://api.irail.be/stations/?format=json&lang=en
  • Liveboard: https://api.irail.be/liveboard/?station={station}&format=json&lang=en

Data Processing

The workflow handles:

  • Timestamp conversion from Unix timestamps to human-readable format
  • Delay calculations (seconds to minutes)
  • Vehicle ID cleanup (removes “BE.NMBS.” prefix)
  • Cancellation status detection

Use Cases

For Commuters

  • Quick departure checks before leaving home/office
  • Real-time delay information
  • Platform changes and service updates

For Travelers

  • Tourist-friendly station name matching
  • Multi-language support (English)
  • Intuitive natural language queries

For Developers

  • Example of n8n workflow design
  • API integration patterns
  • Error handling best practices

Configuration Requirements

Prerequisites

  • n8n instance with webhook capabilities
  • Telegram Bot API token
  • Internet access to iRail API endpoints

Setup Steps

  1. Import the workflow JSON into n8n
  2. Configure Telegram bot credentials
  3. Set up webhook endpoint URL
  4. Test with sample requests
  5. Configure schedule triggers if needed

Example Interactions

User Message: “Charleroi” Bot Response:

πŸš„ Departures: Charleroi-Central

1. πŸš‚ S614160
   πŸ• 09:54
   πŸ“ Platform 10
   🧭 To: Jambes
   βœ… On time

2. πŸš‚ S624260
   πŸ• 09:55
   πŸ“ Platform 6
   🧭 To: Luttre
   βœ… On time

3. πŸš‚ IC932
   πŸ• 10:13
   πŸ“ Platform 9
   🧭 To: Lille Flandres
   βœ… On time

4. πŸš‚ S614560
   πŸ• 10:16
   πŸ“ Platform 1
   🧭 To: Jambes
   βœ… On time

5. πŸš‚ IC3810
   πŸ• 10:19
   πŸ“ Platform 7
   🧭 To: Herstal
   βœ… On time

πŸ“… Requested for: now
πŸ”„ Updated: 9:53:26 AM

Benefits

  • Automation: Reduces manual checking of train schedules
  • Accessibility: Provides instant information through familiar chat interfaces
  • Reliability: Handles errors gracefully with helpful messages
  • Scalability: Can be extended to support multiple languages or regions
  • Integration: Easy to integrate with other systems and workflows

Future Enhancements

Potential improvements could include:

  • Multi-language support (English, French, Dutch…)
  • Journey planning with route information
  • Fare information and ticket booking integration
  • Push notifications for favorite routes
  • Historical delay analytics

Conclusion

This Belgian Train Departure Agent demonstrates how n8n can be used to create sophisticated, real-world automation solutions that integrate multiple APIs and provide genuine value to users. The combination of natural language processing, API integration, and messaging platform connectivity creates a seamless user experience while showcasing the power of low-code automation platforms.

The project serves as an excellent reference for developers looking to build similar transportation or data-driven automation workflows using n8n.

Share :
comments powered by Disqus