Skip to content

scottykwok/openai-slackbot

Repository files navigation

OpenAI API integration with SlackBot

Usage

  1. Create your own .env based on .env.sample . The following keys must be configured:
# See https://api.slack.com/start/building/bolt-python
SLACK_BOT_TOKEN=xoxb-your-Bot-User-OAuth-Token
SLACK_SIGNING_SECRET=your-signing-secret

# See https://beta.openai.com/account/api-keys
OPENAI_API_KEY=your-openai-api-key

# NGROK
NGROK_AUTHTOKEN=your-ngrok-token
  1. Then docker compose up:
docker compose up -d
  1. Get the Ngrok URL:
python3 get_ngrok_url.py
#Output: https://xxxx-xxx-xxx-xxx-xx.xx.ngrok.io
  1. Goto your Slack App "Event Subscriptions" page and put the Ngrok URL into the "Request URL". And add "Subscribe to bot events". If verified successfully, you will be able to "Save Changes".

  1. Done! You can now direct message the bot or add the bot to your channel(s). It will reply to the conversation thread.

Slack App Setup

To create Slack App, see this example. Refer to the sections from "Create a Slack app" to "Save your apps credentials".

For this particular app, it need the following Bot Token Scopes:

If you want to direct message the bot, enable the followings:

Remember to "Reinstall to Workspace".

Ngrok Authtoken

OpenAPI API keys

More

You can also play around with other settings:

# Some of the OpenAI params
OPENAI_API_ENGINE=text-davinci-003
OPENAI_API_MAX_TOKENS=1024
OPENAI_API_TEMPERATURE=0
OPENAI_API_TOP_P=1
OPENAI_API_N=1

# Bot
BOT_MAX_WORKERS=5
BOT_PORT=3000

# The max number of chars to send in the request history. Note the larger the value, the more tokens may be consumed. According to OpenAI, 1 token is ~4 chars. So 2000 chars is ~500 tokens
BOT_MAX_HISTORY_CHARS=2000

# For debug, if set to true, the bot will dump the whole JSON response in slack message
BOT_REPLY_IN_JSON=False

## For debug, if set to true, the bot will append meta data in the in slack message
BOT_REPLY_SHOW_META=False

Local development using venv

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
ngrok http 3000
python3 bot.py

Screenshots

Here is a sample output of asking the bot to generate a login page.

After deleting the wrong tailwind integrity checksum, the generated HTML actually not bad:

Disclaimer

This is a personal project for fun and is not affiliated with OpenAI or Slack in any way.

About

OpenAI API integration with Slack in Python

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published