This document was written by ChatGPT and directed by Aaron Ng (@localghost).
This script creates a Slack bot that uses ChatGPT to respond to direct messages and mentions in a Slack workspace. It functions as a general question-answering bot for your company.
OPENAI_API_KEY
: Your OpenAI API key, which starts with "sk-".SLACK_APP_TOKEN
: Your Slack App Token, which starts with "xapp-".SLACK_BOT_TOKEN
: Your Slack Bot Token, which starts with "xoxb-".
MODEL
: The OpenAI model to use. Can be "gpt-3.5-turbo" or "gpt-4". Default is "gpt-3.5-turbo".PROMPT
: A custom prompt for the bot. Default is a predefined prompt for a friendly company assistant.
- Go to https://api.slack.com/apps?new_app=1.
- Click "Create New App".
- Click "Basic", then name your Slack bot and select a workspace.
- In "Settings" → "Socket Mode", enable both Socket Mode and Event Subscriptions.
- In "Settings" → "Basic Information", install your app to the workspace by following the instructions.
- In "Settings" → "Basic Information", scroll to "App-Level Tokens" and create one with the permission
connections:write
. Set the resulting token that starts withxapp-
as yourSLACK_APP_TOKEN
. - In "Features" → "OAuth and Permissions", copy the "Bot User OAuth Token" and set it as the
SLACK_BOT_TOKEN
in your environment. - In "Features" → "OAuth and Permissions" → "Scopes", add the following permissions:
app_mentions:read
,channels:history
,channels:read
,chat:write
,chat:write.public
,groups:history
,groups:read
,im:history
,im:read
,mpim:history
,mpim:read
,users:read
. - In "Features" → "Event Subscriptions" → "Subscribe to Bot Events", add the following bot user events:
app_mentions:read
,message.im
. - In "Features" → "App Home", turn on the "Messages Tab" switch, and enable the
Allow users to send Slash commands and messages from the messages tab
feature.
Now your Slack bot should be ready to use!
- If deploying to a cloud service, check out and reconfigure
setup.sh
andstart.sh
.
- If running locally, install dependencies with
poetry
. - Comment out these two lines in the script:
# from dotenv import load_dotenv
# load_dotenv()
Start the bot and enjoy using it in your Slack workspace.