A Python based Slack Chatbot that aggregates ChatGPT responses to Slack messages.
Mention the bot in a channel and it will respond with a ChatGPT response.
Replying following commands to the bot will trigger special actions.
reset
: Reset current conversation
- Go to https://api.slack.com/apps and create a new app
- Go to "OAuth & Permissions" and add the following scopes to the "Bot Token Scopes" section by clicking "Add an OAuth Scope" button:
chat:write
: a scope to grant your app the permission to post messages in channels it's a member of.
- Go to "Install App" and click "Install App to Workspace" button. You will be redirected to the installation page. Click "Allow" button to grant the permission to your app.
- Go to "OAuth & Permissions" and copy the "Bot User OAuth Access Token" value. You will need it later.
- Go to "Basic Information" and copy the "Signing Secret" value. You will need it later.
- Go to "Event Subscriptions" and enable events. You will need to specify a Request URL for receiving events from Slack. You should subscribe to the following events:
app_mention
: a type of event that occurs when a user mentions your app in a channel.message.channels
: a type of event that occurs when a message is sent to a channel.
Note: You may need to Run the Bot first to get a public IP address or Domain. Then, you can use the public IP address as the Request URL. (e.g. http://<public-ip-address>:9999/slack/events
Hint: if you have no public IP address, you can use ngrok to expose a local server to the internet.
REFERENCES:
git clone https://github.com/liaocyintl/PySlackChatGPT.git
- Copy and rename
src/config.py.example
tosrc/config.py
cp src/config.py.example src/config.py
- Update the following values in
src/config.py
:
SLACK_BOT_TOKEN
: the value of "Bot User OAuth Access Token" you copied in the previous stepSLACK_SIGNING_SECRET
: the value of "Signing Secret" you copied in the previous stepOPENAI_API_KEY
: obtain an API key from OpenAI API keysCHATGPT_MODEL
: the module name of ChatGPT (default isgpt-3.5-turbo
)
- Build Docker Image
docker build -t pyslackchatgpt:1.1.0 .
- Confirm Docker Image (Optional)
Make sure the image pyslackchatgpt:1.1.0
is listed.
docker images
- Run Docker Container
Note: You can change the port mapping number 9999 to any other port number you like.
docker run -d -p 9999:9999 --name pyslackchatgpt pyslackchatgpt:1.1.0
- Confirm Docker Container Log (Optional)
docker logs -f pyslackchatgpt
- Setup Python Environment
python -m venv venv
- Activate Python Environment
source venv/bin/activate
- Install Python Dependencies
pip install -r requirements.txt
- Run the Bot
python main.py
The first available version of the PySlackChatGPT.
- Add exception handling for ChatGPT API call.
- Add conversition reset by replying
reset
to the bot. - Instantiate ChatGPT API client for each Slack channel.
- Show ChatGPT messages role count in Slack messages.
- Show message role count in Slack messages.
- Show token count in Slack messages.