SlackBot with ChatGPT API

This is a SlackBot(SlackApp) that uses the ChatGPT Completions API to respond to messages in Slack.

System Requirements

  • AWS Account with Administrator Access
  • OpenAI API key

Usage

  1. Set up this Slack App on AWS by following the instructions in the Setup section below.

  2. Send a message to the bot by mentioning it in a message (in a channel or DM)

    • @<bot-name> <prompt> (e.g. @bot-name Please help me with my homework)
  3. The bot will respond with a chat completion

Setup

  1. Create a your own Slack App at https://api.slack.com/apps

    1. Select "From scratch"
    2. Set App Name and select a workspace to install the app
    3. In Slack App Settings Page, Go to "Features" > "OAuth & Permissions" > "Scopes" > "Bot Token Scopes"
      • Add the following scopes
        • app_mention (to reply to @mention messages to the bot)
        • chat:write (to send messages)
        • channels:history (to read messages in channels)
        • groups:history (to read messages in private channels)
        • im:history (to read messages in DMs)
        • mpim:history (to read messages in group DMs)
    4. In Slack App Settings Page, Go to "Basic Information"
      1. "Install your app" > "Install App to Workspace"
        • Install the app to your workspace and permit the requested scopes.
      2. "Display Information"
        • Customize display information of the app, for friendly user experience.
      3. "Save Changes"
    5. In Slack App Settings Page, Go to "App Home"
      1. "Your App's Presence in Slack"
        • Turn on "Always Show My Bot as Online" to be aware of the bot's presence.
      2. "Show Tabs"
        • Add "Messages Tab" and "Allow users to send Slash commands and messages from the messages tab" to directly send messages to the bot.
  2. Clone this repo.

  3. In AWS Systems Manager Parameter Store, with stage name(prod or stg), create the following parameters (with SecureString type or String type if you don't want to use SecureString type. When you use SecureString type, you need to use KMS key(default key is OK) to encrypt the value.)

    • /slackbot-with-chatgpt-api/prod/OPENAI_API_KEY: OpenAI API Key (to use OpenAI Completion API)
    • /slackbot-with-chatgpt-api/prod/OPENAI_MODEL_NAME: OpenAI model name (e.g. gpt-4)
    • /slackbot-with-chatgpt-api/prod/OPENAI_SYSTEM_PROMPT: ChatGPT system prompt (e.g. You are helpful assistant)
    • /slackbot-with-chatgpt-api/prod/SLACK_BOT_ID: Slack Bot ID (e.g. U01AAABBBCC) (to judge whether the message is for the bot, you can get it from installed app's prpfile page with "Copy member ID")
    • /slackbot-with-chatgpt-api/prod/SLACK_BOT_TOKEN: Slack Bot token to send messages and to read messages. (you can get it "OAuth & Permissions" > "OAuth Tokens for Your Workspace")
    • /slackbot-with-chatgpt-api/prod/SLACK_SIGNING_SECRET: Slack signing secret to verify requests from Slack. (you can get it "Basic Information" > "App Credentials", "Signing Secret")
  4. Deploy the Lambda function with Serverless Framework

    1. Install Node.js LTS
    2. Install Serverless Framework
    3. cd to the repo directory
    4. run yarn install to install dependencies
    5. run yarn deploy:prod to deploy the Lambda function to AWS
      • If you use specific AWS profile and not default profile, run AWS_SDK_LOAD_CONFIG=1 yarn deploy:prod --aws-profile <profile name>
      • If you want to deploy to staging environment, run yarn deploy:stg instead of yarn deploy:prod. of course, you need to create parameters in SSM Parameter Store with stage name stg instead of prod.
    6. After the deployment, you can get the API Gateway URL of the Lambda function. (e.g. https://xxxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/prod/slack/events)
  5. In Slack App Settings Page, Go to "Event Subscriptions"

    1. Turn on "Enable Events"
    2. Set Request URL to the API Gateway URL of the Lambda function which you got in the previous step.
    3. "Subscribe to bot events"
      • app_mention (to respond to mentions in not participating channels)
      • message.channels (to respond to messages in channels)
      • message.groups (to respond to messages in private channels)
      • message.im (to respond to messages in DMs)
      • message.mpim (to respond to messages in group DMs)
    4. "Save Changes"
  6. If you want to remove the Slack App from AWS, run yarn remove:prod