SlackGPT is a Slack Bolt App which acts as a proxy between you and ChatGPT. This repository is all the code you need to run your own instance of SlackGPT.
- We are not affiliated or promoted by Slack or OpenAI. We are a third party and we use both the Slack and OpenAI APIs.
- We want these instructions to be as clear as possible, so if you have any questions, or feel that things should be clarified, feel free to open an issue on this repository.
- We would be happy to see improvements to the repo. If you have any ideas, feel free to open a pull request.
- If you have questions or concerns, don't be shy! Feel free to open an issue on this repository about anything.
Setting up the Slack app is unfortanately a fairly complex process, as running a Slack application requires a lot of steps. However, we'll provide all of the steps you should need to get your own instance of SlackGPT up and running.
The first thing you'll have to do is initialize a new Slack App. You'll do this by going to the Slack App Directory and clicking the "Create New App" button. You'll then be prompted to enter a name for your app, and the workspace you want to add it to. Once you've done that, you'll be taken to the app's dashboard.
Once you've created the app, we'll want to add some features to it, but before we can do that, we'll need to set some things up.
First, you'll want to clone this repository onto your computer. This will use the following command:
git clone https://github.com/AidanTilgner/SlackGPT /path/to/your/directory
Next, you'll want to install dependencies for the app. This will be done with the pnpm package manager. You can install it with the following command:
npm install -g pnpm
Once it is installed on your computer, you can install the dependencies with the following command:
pnpm install
Two more things before we can run our App:
- Environment Variables
- Deployment
For the Slack app to run, we'll need to initialize some variables that are specific to your environment. We'll do this by creating a new file called .env
in the root directory of the project. This file will contain all of the environment variables that are needed to run the app. First, create the fie with the following command:
touch .env
Then, open the file and add the following lines:
SLACK_SIGNING_SECRET=""
SLACK_APP_TOKEN=""
SLACK_BOT_TOKEN=""
PORT=3000 # The port that the app will run on
NODE_ENV="development" # or "production", depending on deployment status
INITIAL_CHATGPT_PROMPT="You are a helpful Slack bot." # The initial prompt for your ChatGPT instance
Now that we have the file, we'll need to add values for some of these, mainly the ones that start with SLACK_
. You can do this by finding the following values in the Slack App Dashboard. Start by going to the app dashboard, and finding "Basic Information" on the left side of the screen.
- SLACK_SIGNING_SECRET - Scroll down to "App Credentials" and copy the "Signing Secret" value. Paste it into the corresponding line in the
.env
file. - SLACK_APP_TOKEN - Scroll further down to "App-Level Tokens", and click on "Generate Token and Scopes". Here, give your token a name, such as
xapp
, then a scope. A scope is going to give permission to your Slack app to perform some functionality. SlackGPT requires theconnections:write
scope, so add that before generating your token. Copy the token that is generated, and paste it into the corresponding line in the.env
file.
Now, we'll need to navigate to "OAuth & Permissions" on the left side of the screen. Here you will see a section called "OAuth Tokens for Your Workspace".
- SLACK_BOT_TOKEN - Find the field called "Bot User OAuth Access Token", and copy it's value. Paste it into the corresponding line in the
.env
file.
Now that we have all of the environment variables working. Try running the app with the following command:
pnpm start
If you don't recieve any errors, then you're good to go. Otherwise, go back through the steps and make sure you didn't miss anything. If you're still having issues, feel free to open an issue on this repository.
*For Development's sake, we're going to use Ngrok. That way we can test our app in real time without having to deploy it every time we make a change. First, you'll want to get started with Ngrok:
- Sign up for an account here
- Follow "Getting Started" instructions here
- In a new terminal window, run the following command:
ngrok http 3000
Keep in mind, the port specified, in this case 3000
, is the port that you specified in the .env
file. If you changed it, make sure to change it here as well.
Once you run that command, you should be given an ngrok url. Copy the value of this "Forwarding" url, and we'll use it in our next steps. Also, keep in mind that this is assuming you didn't stop your previous pnpm run start
command. If you did, you'll need to restart it. Basically think of ngrok as forwarding an actual URL to your local machine. So whatever is running on the port that you specific to it, the new url will point to.
We're almost done I promise! Now that your app is deployed to a URL, we can start adding some event subscriptions. To do this, go to your app's dashboard, and click on "Event Subscriptions" from the sidebar.
- Next, you'll want to enable events. To do this, toggle the switch to the right of "Enable Events".
- Second, it'll prompt you for your Request URL. This is the URL that Slack will send events to. Paste the ngrok url that you copied earlier into the field, and add
/slack/events
to the end of it. So if your ngrok url ishttps://123456.ngrok.io
, then your Request URL will behttps://123456.ngrok.io/slack/events
. It will then attempt to verify this URL, and if the previous steps worked correctly, it should be successful. - Third, you'll want to scroll down to the "Subscribe to bot events" section. Here, you'll want to add some events, using the "Add Bot User Event" button.
message.channels
- A message was posted to a public channelmessage.groups
- A message was posted to a private channelmessage.im
- A message was posted in a direct message channelmessage.mpim
- A message was posted in a multiparty direct message channelapp_mention
- Subscribe to only the message events that mention your app or bot
- Finally, make sure to hit the "Save Changes" button on the bottom right. I missed it plenty of times the first time.
This is going to be very similar to the previous step. Go to your app's dashboard, and click on "Interactivity & Shortcuts" from the sidebar. Then, we're going to do the following:
- Toggle the switch to the right of "Interactivity"
- Add a request URL, which should be the exact same URL as the Event Subscription url
- Hit the "Save Changes" button on the bottom right.
Now, this will allow interactive components to work, which will be used to let users enter their API keys.
Similar to the first two steps, we're going to configure slash commands. Go to your app's dashboard, and click on "Slash Commands" from the sidebar. Then, we're going to do the following:
- Click on the "Create New Command" button
- Fill in the properties for a new command called
/removekeys
. Keep in mind the the "Request URL" should be the exact same URL as the Event Subscription url. The rest of the properties can be whatever you want. The things that matter here are the "Request URL" and the name of the command, which must be/removekeys
. - Hit the "Save" button on the bottom right.
Lastly, we're going to configure OAuth & Permissions. Go to your app's dashboard, and click on "OAuth & Permissions" from the sidebar. Then, we're going to do the following:
- Scroll down to "Scopes", and click on the "Add an OAuth Scope" button
- Add
app_mentions:read
- Add
channels:history
- Add
chat:write
- Add
commands
- Add
groups:history
- Add
im:history
- Add
mpim:history
There are too many deployment strategies for us to pick one. Here are some resources to get you started deploying for production:
- Deploy Slack Bolt app to Heroku
- Deploy Slack Bolt app to AWS Lambda
- Manual Deployment with Nginx and DigitalOcean
If you haven't already, make sure to install your Slack app to your work space from the "Basic Information" or "Install App" sections of your app's dashboard. You also may want to reinstall after changes have been made. Also note that you may want to reload your Slack window after changes have been made.
You're all done with setup. Now you can test your app to see if it works for you. There is some basic usage information below.
When you initially call @, if it doesn't have an openai api key for you, it will prompt you to enter one with a text box. Once it has one on file for you, it will use that for subsequent calls. If you want to change your key, you can use the /removekeys
command, which will remove all of your keys from the database. Then, you can call @ again, and it will prompt you to enter a new key.
To prompt ChatGPT, you'll use the following syntax in a Slack channel where your app is invited.
@ prompt:
Then, the app will respond with a ChatGPT generated response, and keep track of your conversation history.
There is only one command for now, removekeys
. To use it, simple type in a Slack window where your app is installed: /removekeys
. By confirming and entering this command, all of your own API keys will be removed from the database.