# alexx666/slack-gpt architecture

> Architecture diagram of the GitHub repository alexx666/slack-gpt, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/alexx666/slack-gpt
- Repository: https://github.com/alexx666/slack-gpt

## Overview

##### Project’s Core Purpose and Type
- Project Type: A serverless integration service deployed on AWS.
- Purpose: To allow Slack users to interact with ChatGPT via Slack mentions. The integration uses dedicated AWS Lambda functions to manage API requests and bot responses, and it is deployed using CloudFormation templates (located in the cfn directory).

##### File Structure for Architectural Clues
- The presence of multiple files in the src folder (api-lambda.ts, bot-lambda.ts, and their non-lambda counterparts api.ts, bot.ts) indicates that the project separates Lambda handler entry points from the core business logic.
- Files like cache.ts, openai.ts, queue.ts, and slack.ts hint at distinct modules:
- openai.ts likely handles communication with the OpenAI API.
- slack.ts likely manages Slack-specific interactions.
- cache.ts and queue.ts suggest components for possibly caching responses and handling asynchronous processing.
- The cfn folder contains CloudFormation (or AWS SAM) templates and manifests, which are used for deploying AWS resources (e.g., API Gateway, Lambda functions, DynamoDB for caching/TTL, etc.).
- Package.json and tsconfig.json indicate a TypeScript project built with Node.js.

##### Key System Components and Their Relationships
a. External Services:
- Slack: Provides the integration’s inbound events (mentions, etc.) and will receive responses via Slackbot.
- OpenAI: Processes the ChatGPT prompts and produces responses.

b. AWS Cloud Components:
- API Gateway: Acts as the front door for handling Slack event subscriptions and directing requests to Lambda functions.
- AWS Lambda Functions: There are multiple Lambda functions (as evidenced by api-lambda.ts and bot-lambda.ts) which serve as the backend logic:
- API Lambda: Likely deals with receiving and processing HTTP requests from Slack.
- Bot Lambda: May handle bot-specific tasks like responding asynchronously or processing queued events.
- (Possibly) DynamoDB: While not explicitly visible in the code, the README’s mention of TTL for a DynamoDB table implies there is a data store for caching conversation history or temporary details.

c. Internal Modules:
- Slack Module: Implements Slack API interactions.
- OpenAI Module: Wraps API calls to ChatGPT.
- Cache/Queue Modules: Manage performance optimizations and asynchronous processing.

##### Architectural Patterns and Interactions
- Serverless Architecture: The design is based on AWS Lambda functions orchestrated by API Gateway and provisioned/deployed via CloudFormation.
- Separation of Concerns: The code is modularized (distinct files for Slack, OpenAI, caching, and queue management) to keep the integration points and business logic separate.

##### Special Points for this Project
- Serverless aspect and cloud provisioning (CloudFormation templates in cfn folder).
- Clearly separate the responsibilities of each Lambda (API vs. Bot) and show their interactions with external services.
- Reinforce that security configurations (tokens for Slack and OpenAI) and manual deployment actions are prerequisites.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Services
    SLACK["Slack"]:::external
    OPENAI["OpenAI"]:::external

    %% AWS Cloud Components
    subgraph "AWS Cloud"
        CF["CloudFormation Templates"]:::aws
        AG["API Gateway (30s Timeout)"]:::aws
        API["API Lambda"]:::aws
        subgraph "API Lambda Internal"
            API_BL["API Business Logic"]:::awsModule
            SM["Slack Module"]:::awsModule
            OM["OpenAI Module"]:::awsModule
            CM["Cache Module"]:::awsModule
            QM["Queue Module"]:::awsModule
        end
        BL["Bot Lambda"]:::aws
        subgraph "Bot Lambda Internal"
            BL_BL["Bot Business Logic"]:::awsModule
        end
    end

    %% Data Flow
    SLACK -->|"sendsEvents"| AG
    AG -->|"invokes"| API
    API -->|"processes"| API_BL
    API -->|"handlesSlack"| SM
    API -->|"callsChatGPT"| OM
    API -->|"readsWritesCache"| CM
    API -->|"queuesTask"| QM
    QM -->|"triggers"| BL
    BL -->|"executes"| BL_BL
    BL -->|"responds"| SLACK
    API -->|"calls"| OPENAI
    OPENAI -->|"returns"| API

    %% Deployment via CloudFormation
    CF -->|"deploys"| AG
    CF -->|"deploys"| API
    CF -->|"deploys"| BL

    %% Style Definitions
    classDef aws fill:#B3D4FC,stroke:#1B4F72,stroke-width:2px;
    classDef external fill:#D5A6BD,stroke:#7F1734,stroke-width:2px;
    classDef awsModule fill:#C9E0EC,stroke:#1B4F72,stroke-dasharray: 5 5,stroke-width:2px;

    %% Click Events for Component Mapping
    click CF "https://github.com/alexx666/slack-gpt/tree/main/cfn/"
    click API "https://github.com/alexx666/slack-gpt/blob/main/src/api-lambda.ts"
    click API_BL "https://github.com/alexx666/slack-gpt/blob/main/src/api.ts"
    click BL "https://github.com/alexx666/slack-gpt/blob/main/src/bot-lambda.ts"
    click BL_BL "https://github.com/alexx666/slack-gpt/blob/main/src/bot.ts"
    click OM "https://github.com/alexx666/slack-gpt/blob/main/src/openai.ts"
    click SM "https://github.com/alexx666/slack-gpt/blob/main/src/slack.ts"
    click CM "https://github.com/alexx666/slack-gpt/blob/main/src/cache.ts"
    click QM "https://github.com/alexx666/slack-gpt/blob/main/src/queue.ts"
```
