This project is # Slack ChatGPT Bot
A Slack bot that integrates with OpenAI's ChatGPT to provide AI-powered conversations directly in your Slack workspace.
- π€ Direct message conversations with ChatGPT
- π¬ Responds when mentioned in channels (
@botname) - β‘ Slash command support (
/chat) - π§ Maintains conversation context per user
- π Secure environment variable configuration
- Node.js 16+ installed
- A Slack workspace where you have permission to add apps
- OpenAI API account and API key
git clone <your-repo>
cd slack-bot
npm install- Go to Slack API
- Click "Create New App" β "From scratch"
- Name your app (e.g., "ChatGPT Bot") and select your workspace
In your Slack app settings, go to "OAuth & Permissions" and add these scopes:
app_mentions:readchat:writecommandsim:historyim:readim:write
- Enable Events and set Request URL to your server (e.g.,
https://your-domain.com/slack/events) - Subscribe to these bot events:
app_mentionmessage.im
Create a slash command:
- Command:
/chat - Request URL:
https://your-domain.com/slack/commands - Description: "Chat with AI"
- Go to "Socket Mode" and enable it
- Generate an App Token with
connections:writescope
Copy .env.example to .env and fill in your values:
cp .env.example .envFill in:
SLACK_BOT_TOKEN: From "OAuth & Permissions" (starts withxoxb-)SLACK_SIGNING_SECRET: From "Basic Information"SLACK_APP_TOKEN: From "Basic Information" β App Token (starts withxapp-)OPENAI_API_KEY: Your OpenAI API key (starts withsk-)
- Go to "OAuth & Permissions" in your Slack app
- Click "Install to Workspace"
- Authorize the app
# Development mode (with auto-restart)
npm run dev
# Production mode
npm startSimply send a direct message to the bot and it will respond using ChatGPT.
Mention the bot in any channel: @botname How can you help me?
Use the slash command: /chat What is the weather like?
src/
βββ app.ts # Main application entry point
βββ services/
β βββ openai.ts # OpenAI service integration
βββ handlers/
β βββ events.ts # Slack event handlers
βββ commands/
β βββ index.ts # Slash command handlers
βββ types/
βββ index.ts # TypeScript type definitions
The bot maintains conversation context per user for more natural interactions. This is stored in memory (for production, consider using a database).
You can modify the OpenAI parameters in src/services/openai.ts:
- Model: Currently using
gpt-3.5-turbo - Max tokens: 1000
- Temperature: 0.7
The bot uses different system prompts for different contexts (DMs, mentions, slash commands) to provide appropriate responses.
Ensure all environment variables are set in your production environment.
- Heroku
- Railway
- DigitalOcean App Platform
- AWS Lambda (with serverless framework)
Slack requires HTTPS for event subscriptions and slash commands in production.
- Bot doesn't respond: Check that the bot token has the correct scopes
- Events not received: Verify the request URL is accessible and uses HTTPS
- OpenAI errors: Check your API key and ensure you have credits
The bot logs all interactions to the console. Check these for debugging.
- Fork the repository
- Create a feature branch
- Make your changes
- Submit a pull request
MIT License It is built using TypeScript and utilizes the Slack API to interact with Slack workspaces.
slack-bot
βββ src
β βββ app.ts # Entry point of the Slack bot application
β βββ handlers
β β βββ events.ts # Handles various Slack events
β βββ commands
β β βββ index.ts # Defines bot commands
β βββ types
β βββ index.ts # Type definitions for the application
βββ package.json # npm configuration file
βββ tsconfig.json # TypeScript configuration file
βββ README.md # Project documentation
-
Clone the repository:
git clone <repository-url> cd slack-bot -
Install dependencies:
npm install -
Configure your Slack app:
- Create a new Slack app at Slack API.
- Add the necessary permissions and event subscriptions.
- Obtain your bot token.
-
Set environment variables: Create a
.envfile in the root directory and add your Slack bot token:SLACK_BOT_TOKEN=your-bot-token -
Run the bot:
npm start
- The bot listens for messages and can respond to specific commands.
- You can define custom commands in the
src/commands/index.tsfile.
Feel free to submit issues or pull requests for improvements or bug fixes.