Skip to content

Patoski-patoski/MemeStream-Agent

Repository files navigation

MemeStream Agent 🎭

A Telegram bot that discovers, analyzes, and delivers meme templates with AI-powered context.

Build Status License


πŸ“– Table of Contents


🎯 About The Project

MemeStream Agent is a full-stack TypeScript application that combines web scraping, AI integration, and bot development to create an intelligent meme discovery platform. It allows users to request memes by name, and the bot will deliver a blank template, a brief history of the meme, and a gallery of examples.

This project demonstrates a robust, scalable, and resilient architecture, with a focus on performance optimization and a great user experience.

Architecture Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Telegram Bot  │───▢│  Meme Agent     │───▢│  Web Scraping   β”‚
β”‚   (Webhook)     β”‚    β”‚  (AI Logic)     β”‚    β”‚  (Playwright)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
         β”‚                       β”‚                       β”‚
         β–Ό                       β–Ό                       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”    β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   User Interfaceβ”‚    β”‚   Google Gemini β”‚    β”‚   Meme Database β”‚
β”‚   (Commands)    β”‚    β”‚   (AI Context)  β”‚    β”‚   (Templates)   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜    β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Key Features

  • πŸ€– Intelligent Meme Discovery: Utilizes natural language processing to understand user requests and handle variations in meme names.
  • 🎨 Instant Template Generation: Delivers blank meme templates for customization, along with a gallery of examples.
  • 🧠 AI-Powered Context: Provides historical and cultural background for each meme using Google Gemini.
  • ⚑ High Performance: Optimized for speed and low memory usage, with concurrent processing and resource pooling.
  • πŸ›‘οΈ Error Resilience: Graceful degradation and intelligent retry logic ensure high availability.
  • πŸ” Advanced Search Capabilities: Implements fuzzy string matching, partial matching, and Levenshtein distance for robust meme name recognition, ensuring users find their memes even with typos or incomplete queries.

πŸ’‘ Technical Deep Dive

This project showcases several advanced software engineering practices and technical implementations:

1. Intelligent Meme Search & Matching

Our bot goes beyond simple keyword matching. It employs a multi-layered approach to meme discovery:

  • Exact & Partial Matching: Prioritizes direct matches while also identifying memes where the search term is a substring.
  • Levenshtein Distance: For close but not exact matches (e.g., "Two Path" for "Two Paths"), we calculate the Levenshtein distance to find memes with minor typos or variations, ensuring a high success rate for user queries.
  • Fuzzy Word Matching: Breaks down complex search terms into individual words to find relevant memes even if the full phrase isn't an exact match.

2. Optimized Performance & Resource Management

  • Direct API Integration for /blank: The /blank command now directly interacts with the Imgflip API, bypassing the job queue for instant responses and reduced latency.

  • Playwright Browser Optimization: The web scraping component utilizes Playwright with aggressive optimizations for serverless environments, including:

    • --no-sandbox, --disable-dev-shm-usage, --disable-gpu flags for reduced memory footprint.
    • Resource blocking (image, font, media) to minimize page load times and memory consumption during scraping.
    • Page reuse and intelligent cleanup to prevent memory leaks.
  • Redis Caching: Extensive use of Redis for caching meme data, user contexts, and popular meme suggestions, significantly reducing API calls and improving response times.

3. Robust Error Handling & Resilience

  • Comprehensive Error Notifications: Users receive clear, actionable feedback when memes are not found or when API calls fail.
  • Graceful Shutdowns: Implemented across the bot, worker, and Redis connections to ensure clean exits, prevent data corruption, and maintain system stability during restarts or deployments.
  • Retry Mechanisms: AI calls and other external interactions are wrapped with intelligent retry logic using exponential backoff, enhancing the bot's resilience to transient network issues or API rate limits.

4. Observability & Maintainability

  • Structured Logging: Detailed console logs provide insights into job processing, cache hits/misses, and error occurrences.
  • Modular Architecture: The codebase is organized into distinct modules (bot core, meme generator, API, utilities) promoting maintainability, testability, and scalability.
  • TypeScript: Strong typing throughout the project enhances code quality, reduces bugs, and improves developer experience.

πŸ› οΈ Technical Stack

  • Backend: TypeScript, Node.js, Express.js
  • Bot Framework: node-telegram-bot-api
  • Web Scraping: Playwright
  • AI: Google Gemini (Function Calling, Streaming)
  • Infrastructure: Docker, Webhooks, Redis (BullMQ for job queuing)

🏁 Getting Started

Follow these instructions to get a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

  • Node.js (v16 or higher)
  • Docker (optional, for containerized deployment)
  • An ngrok account or other tunneling service to expose your local server to the internet.
  • A Telegram Bot Token (get from @BotFather)
  • A Google Generative AI API Key

Installation

  1. Clone the repository:

    git clone https://github.com/Patoski-patoski/MemeStream-Agent.git
    cd MemeStream-Agent
  2. Install dependencies:

    npm install

Configuration

  1. Create a .env file in the root directory by copying the example file:

    cp .env.example .env
  2. Open the .env file and add your credentials:

    # Your Telegram Bot token from @BotFather
    TELEGRAM_BOT_TOKEN=your_telegram_bot_token
    
    # Your public URL from ngrok or a similar service
    WEBHOOK_URL=https://your-ngrok-url.ngrok.io
    
    # The port your local server will run on
    PORT=3300
    
    # Your Gemini API Key
    GEMINI_API_KEY=your_gemini_api_key
    MODEL_NAME=gemini-pro

πŸ’‘ Usage

  1. Start your tunneling service to expose your local port. For example, using ngrok:

    ngrok http 3300

    Copy the HTTPS forwarding URL provided by ngrok into the WEBHOOK_URL variable in your .env file.

  2. Start the bot in development mode:

    npm run dev

    nodemon will watch for any changes in the src directory and automatically restart the bot.

  3. Interact with the bot in Telegram:

    • /start - Get a welcome message and instructions.
    • /meme [name] - Search for a specific meme (e.g., /meme Distracted Boyfriend).
    • /blank [name] - Get a blank meme template (e.g., /blank Drake hotline bling).

πŸ“‚ Project Structure

MemeStream-Agent/
β”œβ”€β”€ .github/                # GitHub Actions workflows
β”œβ”€β”€ dist/                   # Compiled TypeScript files
β”œβ”€β”€ src/                    # Source code
β”‚   β”œβ”€β”€ bot/                # Telegram bot logic
β”‚   β”‚   β”œβ”€β”€ core/           # Core bot functionalities
β”‚   β”‚   β”œβ”€β”€ types/          # TypeScript types for the bot
β”‚   β”‚   └── utils/          # Utility functions for the bot
β”‚   └── meme-generator/     # Meme agent and tools
β”‚       β”œβ”€β”€ agents/         # AI agent for meme generation
β”‚       β”œβ”€β”€ tools/          # Web scraping and other tools
β”‚       β”œβ”€β”€ types/          # TypeScript types for the meme generator
β”‚       └── utils/          # Utility functions for the meme generator
β”œβ”€β”€ tests/                  # Test files
β”œβ”€β”€ .dockerignore           # Docker ignore file
β”œβ”€β”€ .env.example            # Example environment variables
β”œβ”€β”€ .gitignore              # Git ignore file
β”œβ”€β”€ Dockerfile              # Docker configuration
β”œβ”€β”€ eslint.config.ts        # ESLint configuration
β”œβ”€β”€ jest.config.mjs         # Jest configuration
β”œβ”€β”€ nodemon.json            # Nodemon configuration
β”œβ”€β”€ package.json            # Project dependencies and scripts
β”œβ”€β”€ README.md               # This file
└── tsconfig.json           # TypeScript configuration

🀝 Contributing

Contributions are what make the open-source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“œ License

Distributed under the MIT License. See LICENSE for more information.


πŸ“ž Contact

Patrick Patoski - @codesbypatrick

Project Link: https://github.com/Patoski-patoski/MemeStream-Agent

Live Demo: @MemeStreamAgentBot

About

AI-powered agent built with the Google's AI SDK that allows users to search for internet memes, retrieve meme templates, and get engaging descriptions and usage information about popular memes. It leverages web scraping and LLM capabilities to provide a rich, interactive meme discovery experience.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages