A Slack bot that uses the OpenAI API to generate responses and images based on user prompts.
Inspired by @kvzhuang. Original source code can be found here.
This document is polished by ChatGPT :P .
- Mention bot and ask the bot a question at channel. Tt will generate a response:
- Directly have a converstion with the bot:
- Generate an image based on a prompt:
/gen_image [prompt]
- Summary the thread:
- Ask the question based on the conversations in the thread:
- Obtain an OpenAI API key.
You can easily create a pre-configured Slack app by click this link.
This is done by Slack App Manifest. You can also manually copy the manifest below, and paste while creating Slack app.
display_information:
name: OpenAI Chat
description: An OpenAI Slack bot
features:
app_home:
home_tab_enabled: false
messages_tab_enabled: true
messages_tab_read_only_enabled: false
bot_user:
display_name: OpenAI Chat
always_online: true
shortcuts:
- name: Summarize...
type: message
callback_id: summarize
description: Summarize this thread...
- name: Ask something...
type: message
callback_id: ask
description: Ask the question in the thread
slash_commands:
- command: /gen_image
description: Generate image from OpenAI
usage_hint: "[a white siamese cat]"
should_escape: false
oauth_config:
scopes:
bot:
- app_mentions:read
- channels:history
- chat:write
- chat:write.customize
- chat:write.public
- commands
- files:write
- groups:history
- im:history
- users:read
settings:
event_subscriptions:
bot_events:
- app_mention
- message.im
interactivity:
is_enabled: true
org_deploy_enabled: false
socket_mode_enabled: true
token_rotation_enabled: false
- Create a new app and install it to your workspace through the Slack API.
- Enable Socket Mode on the
Socket Mode
page. - On the
OAuth & Permissions
page, add the following scopes toScopes > Bot Token Scopes
:
- Enable Events and subscribe to the
app_mention
andmessage.im
bot events on theEvent Subscriptions
page:
- Add the
/gen_image
command on theSlash Commands
page:
- Add
summarize
andask
shortcuts on theInteractivity & Shortcuts
page:
- Obtain the following keys and add them to a
.env
file at the root of the project directory:- Slack
SLACK_SIGNING_SECRET
: found underBasic Information > App Credentials > Signing Secret
SLACK_BOT_TOKEN
: found underOAuth & Permissions > Bot User OAuth Token
SLACK_APP_TOKEN
: found underBasic Information > App-Level Tokens
, click on the token name to obtain itSLACK_APP_MENTION_QUOTE_USER_MESSAGE
: If enabled, user's message will be added as quote before response message when you mention bot. Default value: false.
- OpenAI auth
You can use different OpenAI providers by setting
OPENAI_PROVIDER
toopenai
orazure
. Each provider has different environment variables.- OpenAI
OPENAI_PROVIDER=openai
OPENAI_OPENAI_AUTH_API_KEY
: OpenAI API keyOPENAI_OPENAI_AUTH_BASE_PATH
: OpenAI API base path.
- Azure
OPENAI_PROVIDER=azure
OPENAI_AZURE_AUTH_API_BASE
: Azure API base pathOPENAI_AZURE_AUTH_DEPLOYMENT_NAME
: Azure API deployment nameOPENAI_AZURE_AUTH_API_VERSION
: Azure API versionOPENAI_AZURE_AUTH_API_KEY
: Azure API key
- OpenAI
- Common OpenAI settings
OPENAI_CHAT_MODEL
: Which chat model you want to use. Be careful your money when you use gpt-4 :P. See available models. Default value: gpt-3.5-turbo.OPENAI_CHAT_ENABLE_SUMMARIZE
: If enabled, it will call OpenAPI chat completion to summarize previous conversioations when number of messages cached >=OPENAI_CHAT_NUM_OF_MESSAGES
. Default value: false.OPENAI_CHAT_NUM_OF_MESSAGES
: Number of messages bot will cache. It's used for appending previous conversioations when calling chat completions API. MUST BE EVEN. Default value: 2.OPENAI_CHAT_TTL
: The duration of time (second) that messages will be kept. Default value: null (keep forever).OPENAI_CHAT_SYSTEM_MESSAGE
: First messsage to help set the behavior of the assistant. Default value: null.
- Slack
IMPORTANT: If you are using Azure as OpenAI provider, you cannot use /gen_image
command.
Your .env
file should look like this:
OPENAI_PROVIDER=openai
OPENAI_OPENAI_AUTH_API_KEY=xxxxxxx
SLACK_BOT_TOKEN=xoxb-xxxxxxx
SLACK_SIGNING_SECRET=xxxxxx
SLACK_APP_TOKEN=xapp-xxxxxx
OPENAI_CHAT_MODEL=gpt-3.5-turbo
OPENAI_CHAT_ENABLE_SUMMARIZE=false
OPENAI_CHAT_NUM_OF_MESSAGES=4
OPENAI_CHAT_TTL=1200
OPENAI_CHAT_SYSTEM_MESSAGE="You are a helpful assistant."
To get started with the bot, follow these steps:
-
Install the necessary dependencies by running
yarn install
-
Start the bot by running
yarn start
Alternatively, you can use docker by running docker-compose up
.