This commit is contained in:
nusquama
2025-11-13 13:29:16 +01:00
parent 2cd47970d4
commit b4d42853d2
@@ -0,0 +1,401 @@
Subscription Tracker & Reminders with GPT-4 nano, Telegram & Google Sheets
https://n8nworkflows.xyz/workflows/subscription-tracker---reminders-with-gpt-4-nano--telegram---google-sheets-7885
# Subscription Tracker & Reminders with GPT-4 nano, Telegram & Google Sheets
---
## 1. Workflow Overview
This workflow, titled **Subscription Tracker & Reminders with GPT-4 nano, Telegram & Google Sheets**, is designed to manage subscription billing data interactively and automatically. It serves two main use cases:
- **Interactive Subscription Management via Telegram:** Users interact with an AI agent powered by GPT-4 nano through Telegram to add, update, delete, or query subscription information using natural language. The AI extracts detailed subscription data from user messages and maintains conversational context per user.
- **Automated Daily Subscription Reminders:** Every day at 8:00 AM, the workflow reads subscription data from Google Sheets, identifies subscriptions due within the next 3 days, and sends formatted reminders via Telegram or optionally email (note: SMTP/email sending node not explicitly present but implied by notes).
The workflow is logically grouped into two primary blocks:
### 1.1 AI-Powered Subscription Management Block
- Handles Telegram message reception.
- Maintains per-user conversation memory.
- Uses GPT-4 nano and LangChain AI Agents to parse and manage subscription data with built-in currency conversion and validation.
- Updates or appends subscription data in a connected Google Sheets document.
- Sends responses back to users on Telegram.
### 1.2 Daily Subscription Reminder Block
- Scheduled trigger at 8:00 AM daily.
- Reads all subscription data from Google Sheets.
- Filters subscriptions due within 3 days.
- Aggregates and formats subscription reminders.
- Sends formatted notifications via Telegram.
Supporting tools used include Google Sheets nodes for data persistence, LangChain tools (calculator, SerpAPI) for currency conversion and pricing checks, and Telegram nodes for messaging.
---
## 2. Block-by-Block Analysis
### 2.1 AI-Powered Subscription Management Block
**Overview:**
This block processes incoming Telegram messages, maintains conversation context, invokes an AI agent with GPT-4 nano to parse natural language subscription requests, manages subscription CRUD operations in Google Sheets, and sends formatted responses back to Telegram users.
**Nodes Involved:**
- Telegram Message Received
- Window Buffer Memory
- OpenAI Chat Model
- SerpAPI (LangChain Tool)
- Calculator (LangChain Tool)
- Get row(s) in sheet in Google Sheets
- Append or update row in sheet in Google Sheets
- Smart Bill Manager (LangChain Agent)
- Format Response
- Send Telegram Response
- Send a chat action
**Node Details:**
- **Telegram Message Received**
- *Type:* Telegram Trigger
- *Role:* Entry point for user messages via Telegram bot.
- *Config:* Listens for "message" updates. Uses credentials "Bill Manager AI".
- *Input:* Telegram user messages.
- *Output:* Message JSON passed to memory and AI Agent nodes.
- *Failures:* Telegram API errors, invalid webhook setup.
- **Window Buffer Memory**
- *Type:* LangChain Memory Buffer Window
- *Role:* Maintains conversational context keyed by Telegram user ID.
- *Config:* Session key derived dynamically from incoming Telegram user ID.
- *Input:* Telegram message data.
- *Output:* Contextual memory provided to AI Agent.
- *Failures:* Expression evaluation errors, memory storage limits.
- **OpenAI Chat Model**
- *Type:* LangChain OpenAI Chat Model
- *Role:* Provides GPT-4.1-nano language model for AI agent processing.
- *Config:* Uses OpenAI API credentials "Rdavid". Model: gpt-4.1-nano.
- *Input:* Prompt and context from LangChain agent.
- *Output:* AI-generated text responses.
- *Failures:* API quota limits, network errors, invalid credentials.
- **SerpAPI**
- *Type:* LangChain Search Tool
- *Role:* Used by AI agent for currency conversion and pricing verification searches.
- *Config:* Uses SerpAPI credentials.
- *Input:* Search queries generated by AI agent.
- *Output:* Search results for AI processing.
- *Failures:* API key limits, search failures.
- **Calculator**
- *Type:* LangChain Calculator Tool
- *Role:* Assists AI agent in complex calculations, e.g., currency conversion math.
- *Config:* No special parameters.
- *Input:* Calculation requests from AI agent.
- *Output:* Calculation results.
- *Failures:* Invalid expressions, calculation errors.
- **Get row(s) in sheet in Google Sheets**
- *Type:* Google Sheets Tool
- *Role:* Enables AI agent to query subscription data for validation or updates.
- *Config:* Filters by multiple columns (Bill Name, Billing Cycle, Due In, Status, Card Used). Reads from "Dashboard" sheet of a specified spreadsheet.
- *Credentials:* Google Sheets OAuth2 API "Sheets: RDavid".
- *Input:* Search filters from AI Agent.
- *Output:* Matching sheet rows.
- *Failures:* Credential expiration, sheet access issues, filter expression errors.
- **Append or update row in sheet in Google Sheets**
- *Type:* Google Sheets Tool
- *Role:* Updates or adds subscription records based on AI agent output.
- *Config:* Maps extracted fields: Bill Name, Amount, Plan, Start Date, Billing Cycle, Status, Card Used (Last 4 Digits), Last Updated timestamp. Matches rows by Bill Name. Writes to "Raw Data" sheet.
- *Credentials:* Google Sheets OAuth2 API "Sheets: RDavid".
- *Input:* Subscription data from AI agent.
- *Output:* Confirmation of sheet update.
- *Failures:* Write permission issues, data format mismatches.
- **Smart Bill Manager**
- *Type:* LangChain Agent Node
- *Role:* Core AI agent that interprets user subscription requests, extracts all required data fields, manages currency conversions, and controls conversation flow.
- *Config:* Uses GPT-4.1-nano model, integrates SerpAPI and Calculator tools, connected to OpenAI Chat Model and Window Buffer Memory. System message defines detailed roles, data extraction rules, error prevention, and conversation examples.
- *Input:* User message text from Telegram Message Received, memory context, and AI language model.
- *Output:* Structured subscription data, natural language response for Telegram, and sheet operation commands.
- *Failures:* Parsing errors, incomplete data extraction, API rate limits.
- **Format Response**
- *Type:* Set Node
- *Role:* Formats AI agents JSON output into a Telegram-friendly message string.
- *Config:* Assigns field `response` with AI output content.
- *Input:* AI agent output JSON.
- *Output:* Formatted string for Telegram.
- *Failures:* Expression evaluation errors if AI output is malformed.
- **Send Telegram Response**
- *Type:* Telegram Node
- *Role:* Sends formatted AI responses back to the originating Telegram chat.
- *Config:* Sends message text from formatted response; uses chat ID from incoming Telegram message; attribution disabled.
- *Credentials:* Telegram API "Bill Manager AI".
- *Input:* Formatted response string.
- *Output:* Telegram message sent confirmation.
- *Failures:* Telegram API errors, invalid chat ID.
- **Send a chat action**
- *Type:* Telegram Node
- *Role:* Sends "typing" or other chat actions to indicate bot responsiveness.
- *Config:* Sends chat action to same chat as incoming message.
- *Credentials:* Telegram API "TG: Rockstar AI" (different credential set).
- *Input:* Triggered on Telegram message reception.
- *Output:* Chat action sent.
- *Failures:* Telegram API errors, credential mismatches.
---
### 2.2 Daily Subscription Reminder Block
**Overview:**
This block runs every day at 8:00 AM to identify subscriptions due within 3 days. It reads subscription data from Google Sheets, filters relevant entries, aggregates the data, formats a concise reminder message using GPT-4 nano, and sends the notification to a specified Telegram chat.
**Nodes Involved:**
- Daily 8AM Check
- Read Subscriptions
- Any Subscriptions Due? (If node)
- Aggregate
- Message a model (OpenAI Chat)
- Send Telegram Response1
**Node Details:**
- **Daily 8AM Check**
- *Type:* Schedule Trigger
- *Role:* Triggers workflow execution every day at 8:00 AM server time.
- *Config:* Cron expression "0 8 * * *".
- *Input:* None; time-triggered.
- *Output:* Triggers downstream nodes.
- *Failures:* Server clock issues, trigger misconfiguration.
- **Read Subscriptions**
- *Type:* Google Sheets Node
- *Role:* Reads all subscription data from the "Dashboard" sheet in Google Sheets.
- *Config:* Connected to the same spreadsheet as other Google Sheets nodes; retrieves all rows.
- *Credentials:* Google Sheets OAuth2 API "Sheets: RDavid".
- *Input:* Trigger from schedule.
- *Output:* List of subscriptions with fields including "Due In".
- *Failures:* Credential expiration, API quota, sheet access issues.
- **Any Subscriptions Due?**
- *Type:* If Node
- *Role:* Filters subscriptions where "Due In" is less than or equal to 3 days, indicating upcoming due dates.
- *Config:* Condition compares each rows "Due In" field <= 3. Loose type validation enabled.
- *Input:* Rows from "Read Subscriptions".
- *Output:* True branch proceeds if any matches; false branch terminates or skips further processing.
- *Failures:* Missing or malformed "Due In" data, expression errors.
- **Aggregate**
- *Type:* Aggregate Node
- *Role:* Aggregates all filtered subscription items to pass as a single data object for AI formatting.
- *Config:* Aggregates all item data into one JSON array.
- *Input:* Filtered subscriptions from If node.
- *Output:* Aggregated subscription list.
- *Failures:* Large datasets causing memory issues.
- **Message a model**
- *Type:* OpenAI Chat Node
- *Role:* Uses GPT-4.1-nano to generate a concise, emoji-rich Telegram message summarizing bills due soon.
- *Config:*
- System prompt instructs formatting: include total bills due, each bills name, amount in Peso, plan type, exact due date, usage of emojis, and message length under 400 characters.
- User message is JSON stringified aggregated subscription data.
- *Credentials:* OpenAI API "Rdavid".
- *Input:* Aggregated subscription data.
- *Output:* Formatted Telegram message JSON.
- *Failures:* API rate limits, malformed input.
- **Send Telegram Response1**
- *Type:* Telegram Node
- *Role:* Sends the formatted reminder message to a fixed Telegram chat ID (hardcoded as "7707872097").
- *Config:* Message text from AI output; attribution disabled.
- *Credentials:* Telegram API "Bill Manager AI".
- *Input:* AI-generated message.
- *Output:* Confirmation of message sent.
- *Failures:* Invalid chat ID, Telegram API errors.
---
## 3. Summary Table
| Node Name | Node Type | Functional Role | Input Node(s) | Output Node(s) | Sticky Note |
|---------------------------------|-------------------------------------|--------------------------------------------|----------------------------------|----------------------------------|------------------------------------------------------------------------------------------------------------------------------------|
| Telegram Message Received | Telegram Trigger | Entry point for Telegram user messages | - | Window Buffer Memory, Smart Bill Manager, Send a chat action | Triggers when any message is received via Telegram bot |
| Window Buffer Memory | LangChain Memory Buffer Window | Maintains conversation context per user | Telegram Message Received | Smart Bill Manager | Maintains conversation context for each Telegram user session |
| OpenAI Chat Model | LangChain OpenAI Chat Model | Provides GPT-4 nano model for AI agent | Smart Bill Manager (ai_languageModel) | Smart Bill Manager | Provides the Language Model (GPT-4) for the AI Agent to process natural language requests |
| SerpAPI | LangChain Search Tool | Currency conversion and pricing searches | Smart Bill Manager (ai_tool) | Smart Bill Manager | |
| Calculator | LangChain Calculator Tool | Performs calculations for AI agent | Smart Bill Manager (ai_tool) | Smart Bill Manager | |
| Get row(s) in sheet in Google Sheets | Google Sheets Tool | Query subscription data for AI agent | Smart Bill Manager (ai_tool) | Smart Bill Manager | |
| Append or update row in sheet in Google Sheets | Google Sheets Tool | Updates or adds subscription records | Smart Bill Manager (ai_tool) | Smart Bill Manager | |
| Smart Bill Manager | LangChain Agent | Core AI agent managing subscriptions | Telegram Message Received, Window Buffer Memory, OpenAI Chat Model, SerpAPI, Calculator, Google Sheets Tools | Format Response | The core AI Agent that processes user requests and manages subscription data through natural language |
| Format Response | Set Node | Formats AI response for Telegram | Smart Bill Manager | Send Telegram Response | Formats the AI Agent's response for sending back to Telegram |
| Send Telegram Response | Telegram Node | Sends AI responses back to Telegram user | Format Response | - | Sends the AI Agent's response back to the user via Telegram |
| Send a chat action | Telegram Node | Sends typing indicator on Telegram | Telegram Message Received | - | |
| Daily 8AM Check | Schedule Trigger | Triggers daily subscription checks | - | Read Subscriptions | Triggers daily at 8:00 AM to check for subscriptions due within 3 days |
| Read Subscriptions | Google Sheets Node | Reads subscription data from Google Sheets | Daily 8AM Check, When clicking Execute workflow | Any Subscriptions Due?, - | Reads all subscription data from Google Sheets to check for upcoming due dates |
| Any Subscriptions Due? | If Node | Filters subscriptions due within 3 days | Read Subscriptions | Aggregate (true), - (false) | Checks if there are any subscriptions due within 3 days that need email notification |
| Aggregate | Aggregate Node | Aggregates filtered subscriptions | Any Subscriptions Due? (true) | Message a model | |
| Message a model | OpenAI Chat Node | Generates formatted reminder message | Aggregate | Send Telegram Response1 | |
| Send Telegram Response1 | Telegram Node | Sends daily subscription reminders via Telegram | Message a model | - | Sends the AI Agent's response back to the user via Telegram |
| When clicking Execute workflow | Manual Trigger | Manual trigger to test reading subscriptions | - | Read Subscriptions | |
| AI Agent Core Section | Sticky Note | Describes the AI Agent block | - | - | # 🤖 Bill Agent\n\nThe heart of the subscription management system:\n- Processes natural language requests\n- Connects to Google Sheets via sub-workflow tool\n- Maintains conversation memory per user\n- Handles CRUD operations on subscription data\n\n**Key Features:**\n- Add/update/delete subscriptions\n- List active/inactive subscriptions\n- Calculate due dates and currency conversions\n- Provide spending summaries |
| Daily Reminder Section | Sticky Note | Describes the daily reminder block | - | - | # 📅 Daily Reminder System\n\nAutomated daily checks at 8:00 AM:\n- Reads all subscription data\n- Filters subscriptions due within 3 days\n- Sends email reminders with formatted content\n- Includes totals and detailed breakdown\n\n**Setup Required:**\n- Configure Google Sheets credentials\n- Set up SMTP for email sending\n- Update user email address\n- Ensure Google Sheets has proper formulas for 'Due In' column |
---
## 4. Reproducing the Workflow from Scratch
1. **Create Telegram Bot and Setup Credentials:**
- Register a Telegram bot and obtain API token.
- Configure Telegram credentials in n8n as "Bill Manager AI" and "TG: Rockstar AI" (both with the same or separate tokens as needed).
2. **Create Google Sheets OAuth2 Credential:**
- Create Google API credentials with Sheets API enabled.
- Configure in n8n as "Sheets: RDavid" with OAuth2.
3. **Configure OpenAI API Credential:**
- Obtain API key from OpenAI for GPT-4 access.
- Add credential in n8n as "Rdavid".
4. **Configure SerpAPI Credential:**
- Obtain SerpAPI key.
- Add credential accordingly.
5. **Add Nodes for AI-Powered Subscription Management Block:**
5.1. **Telegram Message Received Node:**
- Type: Telegram Trigger
- Listen for "message" updates
- Credential: "Bill Manager AI"
5.2. **Window Buffer Memory Node:**
- Type: LangChain Memory Buffer Window
- Session key: `={{ $('Telegram Message Received').item.json.message.from.id }}`
- Session ID type: customKey
5.3. **OpenAI Chat Model Node:**
- Type: LangChain OpenAI Chat Model
- Model: "gpt-4.1-nano"
- Credential: "Rdavid"
5.4. **SerpAPI Node:**
- Type: LangChain Tool SerpAPI
- Credential: configured SerpAPI key
5.5. **Calculator Node:**
- Type: LangChain Tool Calculator
- No special parameters
5.6. **Get row(s) in sheet in Google Sheets Node:**
- Document ID: Google Sheets ID for subscription data
- Sheet name: "Dashboard"
- Filters for columns: Bill Name, Billing Cycle, Due In, Status, Card Used
- Credential: "Sheets: RDavid"
5.7. **Append or update row in sheet in Google Sheets Node:**
- Document ID: as above
- Sheet name: "Raw Data"
- Matching column: Bill Name
- Columns mapped: Bill Name, Amount, Plan, Start Date, Billing Cycle, Status, Card Used (Last 4 Digits), Last Updated (set to current timestamp)
- Credential: "Sheets: RDavid"
5.8. **Smart Bill Manager Node:**
- Type: LangChain Agent
- Text input: `={{ $json.message.text }}` (from Telegram Message Received)
- System message: Detailed instructions specifying data extraction rules, currency conversion logic, conversation flow, error prevention, and examples (copy content from original node)
- Tools: Connect to OpenAI Chat Model, SerpAPI, Calculator, Google Sheets nodes as AI tools
- Memory: Connect to Window Buffer Memory
- Version: 2
5.9. **Format Response Node:**
- Type: Set Node
- Assign field "response" to `={{ $json.output }}` from Smart Bill Manager output
5.10. **Send Telegram Response Node:**
- Type: Telegram Node
- Text: `={{ $json.response }}`
- Chat ID: `={{ $('Telegram Message Received').item.json.message.chat.id }}`
- Credential: "Bill Manager AI"
- Disable attribution
5.11. **Send a chat action Node:**
- Type: Telegram Node
- Operation: sendChatAction (e.g., typing)
- Chat ID: same as Telegram Message Received
- Credential: "TG: Rockstar AI"
6. **Connect Nodes for AI Block:**
- Telegram Message Received → Window Buffer Memory → Smart Bill Manager
- Telegram Message Received → Send a chat action
- Smart Bill Manager → Format Response → Send Telegram Response
- Smart Bill Manager → Google Sheets "Get row(s)" and Append or Update nodes as AI tools
- Smart Bill Manager → OpenAI Chat Model, SerpAPI, Calculator as AI language model and tools
7. **Add Nodes for Daily Subscription Reminder Block:**
7.1. **Daily 8AM Check Node:**
- Type: Schedule Trigger
- Cron: "0 8 * * *"
7.2. **Read Subscriptions Node:**
- Type: Google Sheets Node
- Document ID and sheet "Dashboard"
- Credential: "Sheets: RDavid"
7.3. **Any Subscriptions Due? Node (If):**
- Condition: `Due In` <= 3
- Loose type validation enabled
7.4. **Aggregate Node:**
- Aggregate all filtered items into one JSON array
7.5. **Message a model Node:**
- Type: OpenAI Chat Node
- Model: "gpt-4.1-nano"
- System prompt: instruct formatting a concise Telegram message listing bills due soon, with emojis and length constraints (copy exact prompt)
- Input message: stringified aggregated subscription data
- Credential: "Rdavid"
7.6. **Send Telegram Response1 Node:**
- Type: Telegram Node
- Text: AI-generated formatted message
- Chat ID: hardcoded "7707872097" (replace with intended recipient ID)
- Credential: "Bill Manager AI"
- Disable attribution
8. **Connect Reminder Block Nodes:**
- Daily 8AM Check → Read Subscriptions → Any Subscriptions Due?
- If true → Aggregate → Message a model → Send Telegram Response1
9. **Add Manual Trigger Node (Optional):**
- For testing, manual trigger node connected to Read Subscriptions
10. **Add Sticky Notes:**
- Add descriptive sticky notes for "AI Agent Core Section" and "Daily Reminder Section" with content as provided for documentation clarity.
---
## 5. General Notes & Resources
| Note Content | Context or Link |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| Users must ensure Google Sheets contains proper formulas for the "Due In" column to calculate days until due date, enabling filtering of subscriptions due soon. | See setup instructions in Daily Reminder Section sticky note |
| Telegram chat ID "7707872097" used for sending daily reminders is hardcoded; replace with your own Telegram user or group chat ID for proper delivery. | Telegram API documentation: https://core.telegram.org/bots/api#chat |
| OpenAI GPT-4.1-nano model provides efficient and cost-effective AI processing for natural language understanding and generation in this workflow. | OpenAI models: https://platform.openai.com/docs/models/gpt-4 |
| Currency conversion is intelligently handled by the AI agent using SerpAPI search and Calculator tools, only converting amounts not already in USD, saving API calls and ensuring accuracy. | SerpAPI: https://serpapi.com/ |
| The workflows conversational logic minimizes back-and-forth by extracting maximum data from first user message, asking only for missing required fields, improving user experience and reducing token usage. | AI Agent Core Section sticky note |
| For email reminders, SMTP node setup is implied but not included; users wishing to send email notifications must add and configure SMTP nodes with valid credentials and connect accordingly. | n8n SMTP node documentation: https://docs.n8n.io/integrations/builtin/smtp/ |
---
**Disclaimer:**
The text provided is exclusively derived from an automated workflow created with n8n, a tool for integration and automation. This processing strictly complies with current content policies and contains no illegal, offensive, or protected elements. All handled data is legal and public.
---