Skip to content

A Python based Slack Chatbot that aggregates ChatGPT responses to Slack messages.

License

Notifications You must be signed in to change notification settings

liaocyintl/PySlackChatGPT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Introduction

A Python based Slack Chatbot that aggregates ChatGPT responses to Slack messages.

Usage

Mention the bot in a channel and it will respond with a ChatGPT response.

Spcial Commands

Replying following commands to the bot will trigger special actions.

  • reset: Reset current conversation

Quick Deployment

Register Slack App

  1. Go to https://api.slack.com/apps and create a new app

  1. 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.

  1. 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.

  1. Go to "OAuth & Permissions" and copy the "Bot User OAuth Access Token" value. You will need it later.

  1. Go to "Basic Information" and copy the "Signing Secret" value. You will need it later.

  1. 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:

Clone the repository

git clone https://github.com/liaocyintl/PySlackChatGPT.git

Setup config.py

  1. Copy and rename src/config.py.example to src/config.py
cp src/config.py.example src/config.py
  1. Update the following values in src/config.py:
  • SLACK_BOT_TOKEN: the value of "Bot User OAuth Access Token" you copied in the previous step
  • SLACK_SIGNING_SECRET: the value of "Signing Secret" you copied in the previous step
  • OPENAI_API_KEY: obtain an API key from OpenAI API keys
  • CHATGPT_MODEL: the module name of ChatGPT (default is gpt-3.5-turbo)

Run the Bot

Run on Docker (Recommended)

  1. Build Docker Image
docker build -t pyslackchatgpt:1.1.0 .
  1. Confirm Docker Image (Optional)

Make sure the image pyslackchatgpt:1.1.0 is listed.

docker images
  1. 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
  1. Confirm Docker Container Log (Optional)
docker logs -f pyslackchatgpt

Run on Local

  1. Setup Python Environment
python -m venv venv
  1. Activate Python Environment
source venv/bin/activate
  1. Install Python Dependencies
pip install -r requirements.txt
  1. Run the Bot
python main.py

Release Notes

v1.0.0 (2023-03-04)

The first available version of the PySlackChatGPT.

v1.1.0 (2023-03-05)

  • 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.