This is a SlackBot(SlackApp) that uses the ChatGPT Completions API to respond to messages in Slack.
- AWS Account with Administrator Access
- OpenAI API key
-
Set up this Slack App on AWS by following the instructions in the Setup section below.
-
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
)
-
The bot will respond with a chat completion
-
Create a your own Slack App at https://api.slack.com/apps
- Select "From scratch"
- Set App Name and select a workspace to install the app
- 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)
- Add the following scopes
- In Slack App Settings Page, Go to "Basic Information"
- "Install your app" > "Install App to Workspace"
- Install the app to your workspace and permit the requested scopes.
- "Display Information"
- Customize display information of the app, for friendly user experience.
- "Save Changes"
- "Install your app" > "Install App to Workspace"
- In Slack App Settings Page, Go to "App Home"
- "Your App's Presence in Slack"
- Turn on "Always Show My Bot as Online" to be aware of the bot's presence.
- "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.
- "Your App's Presence in Slack"
-
Clone this repo.
-
In AWS Systems Manager Parameter Store, with stage name(
prod
orstg
), 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")
-
Deploy the Lambda function with Serverless Framework
- Install Node.js LTS
- install Node.js LTS: https://nodejs.org/en/download/ or use Node Version Manager (nvm, n, etc.)
- Install Serverless Framework
npm install -g serverless
ref. https://www.serverless.com/framework/docs/getting-started
- cd to the repo directory
- run
yarn install
to install dependencies - 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 ofyarn deploy:prod
. of course, you need to create parameters in SSM Parameter Store with stage namestg
instead ofprod
.
- If you use specific AWS profile and not default profile, run
- 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
)
- Install Node.js LTS
-
In Slack App Settings Page, Go to "Event Subscriptions"
- Turn on "Enable Events"
- Set Request URL to the API Gateway URL of the Lambda function which you got in the previous step.
- "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)
- "Save Changes"
-
If you want to remove the Slack App from AWS, run
yarn remove:prod