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 .
Generate an image based on a prompt:
/gen_image [prompt]
Summary the thread:
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
Socket Mode
page.OAuth & Permissions
page, add the following scopes to Scopes > Bot Token Scopes
:app_mention
and message.im
bot events on the Event Subscriptions
page:/gen_image
command on the Slash Commands
page:summarize
and ask
shortcuts on the Interactivity & Shortcuts
page:.env
file at the root of the project directory:
SLACK_SIGNING_SECRET
: found under Basic Information > App Credentials > Signing Secret
SLACK_BOT_TOKEN
: found under OAuth & Permissions > Bot User OAuth Token
SLACK_APP_TOKEN
: found under Basic 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_PROVIDER
to openai
or azure
. Each provider has different environment variables.
OPENAI_PROVIDER=openai
OPENAI_OPENAI_AUTH_API_KEY
: OpenAI API keyOPENAI_OPENAI_AUTH_BASE_PATH
: OpenAI API base path.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 keyOPENAI_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.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
.