Skip to content

jack482653/openai-slack-bot

Repository files navigation

openai-slack-bot

GitHub Workflow Status GitHub tag (latest by date)

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 .

Functions

  • Mention bot and ask the bot a question at channel. Tt will generate a response:

image

  • Directly have a converstion with the bot:

image

  • Generate an image based on a prompt:
/gen_image [prompt]

image

  • Summary the thread:

image

  • Ask the question based on the conversations in the thread:

image

Prerequisites

OpenAI

  • Obtain an OpenAI API key.

Slack

Use App Manifest

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

Manual

  1. Create a new app and install it to your workspace through the Slack API.
  2. Enable Socket Mode on the Socket Mode page.
  3. On the OAuth & Permissions page, add the following scopes to Scopes > Bot Token Scopes:

image

  1. Enable Events and subscribe to the app_mention and message.im bot events on the Event Subscriptions page:

image

  1. Add the /gen_image command on the Slash Commands page:

image

  1. Add summarize and ask shortcuts on the Interactivity & Shortcuts page:

image

  1. Obtain the following keys and add them to a .env file at the root of the project directory:
    1. Slack
      • 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 it
      • SLACK_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.
    2. OpenAI auth You can use different OpenAI providers by setting OPENAI_PROVIDER to openai or azure. Each provider has different environment variables.
      1. OpenAI
        • OPENAI_PROVIDER=openai
        • OPENAI_OPENAI_AUTH_API_KEY: OpenAI API key
        • OPENAI_OPENAI_AUTH_BASE_PATH: OpenAI API base path.
      2. Azure
        • OPENAI_PROVIDER=azure
        • OPENAI_AZURE_AUTH_API_BASE: Azure API base path
        • OPENAI_AZURE_AUTH_DEPLOYMENT_NAME: Azure API deployment name
        • OPENAI_AZURE_AUTH_API_VERSION: Azure API version
        • OPENAI_AZURE_AUTH_API_KEY: Azure API key
    3. 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.

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

Getting Started

To get started with the bot, follow these steps:

  1. Install the necessary dependencies by running yarn install

  2. Start the bot by running yarn start

Alternatively, you can use docker by running docker-compose up.