Integrate OpenAI’s ChatGPT within Slack: a step-by-step approach!

Alexandre t'Kint
4 min readFeb 3, 2023

--

Slack is one of the most widely used communication tools for teams, and with the integration of OpenAI’s ChatGPT, it becomes an even more powerful tool. ChatGPT is a highly advanced language model that can generate human-like responses to a given prompt. In this blog, we will show you how to integrate ChatGPT with Slack and use it to answer questions and have conversations with your team members.

Note: the API has a higher uptime compared to the ChatGPT UI 😄

This guide uses the GPT-3 model which is the closest to what ChatGPT offers. The ChatGPT is its older brother, and will require a one line code change once it is released

This is our end goal:

Exciting! Let’s get started!

Table of contents:

  • Step 1: Register an app with Slack and obtain tokens
  • Step 2: Obtain the OpenAI API key
  • Step 3: Install necessary dependencies
  • Step 4: Run the application
  • Step 5: Test out

Step 1: Register an app with Slack and obtain tokens

The first step in integrating ChatGPT with Slack is to register an app with Slack and obtain the Slack Bot Token and Slack App Token. To do this, follow these steps:

  1. Log in to your Slack workspace
  2. Go to the Slack API website
  3. Click on “Create an app” and select “From scratch”
  4. Give your app a name, select your Slack workspace
  5. In Basic information > Add features and functionality. Click on “Permissions” and in Scopes add in Bot Token Scopes: app_mentions:read ; channels:history ; channels:read ; chat:write
  6. In settings, click on “Socket Mode”, enable it and give the token a name. Copy the Slack Bot App Token (starts with xapp)
  7. In Basic information > Add features and functionality. Click on “Event Subscriptions” and enable it. Furthermore in “Subscribe to bot events” select “app_mention”. Save changes.
  8. Go to the “OAuth & Permissions” section and install your app to your workspace
  9. Copy the Slack Bot Token (starts with xoxb)

Step 2: Obtain the OpenAI API key

The next step is to obtain the OpenAI API key. Why? Will need to connect to OpenAI’s API in order to use their GPT-3 API. If you are new to this, no problem, you’ll get 18$ for free without having to provide a credit card. To generate an API key, follow these steps:

  1. Go to the OpenAI API website
  2. Log in or sign up for an OpenAI account
  3. Go to the API Key section and create a new API key
  4. Copy the API key

Step 3: Install necessary dependencies

The next step is to install the necessary dependencies such as slack-bolt, slack and openai. The Slack-Bolt package is a set of tools and libraries that allow developers to quickly and easily create Slack applications. It provides an easy-to-use API for building bots, custom integrations, and Slack app features. You can install these dependencies by running the following command in your terminal:

pip install openai
pip install slack-bolt
pip install slack

Step 4: Run the application

Fill in the 3 tokens that you created above in the script below and run the application. The app will listen to events in which he is tagged, once that happens a message is shown to the user that the bot is working on an answer. Next, the question is sent to a GPT-3 model and finally returned to the user.

There will be a message “⚡️ Bolt app is running!” in your Python environment to indicate that the app is active

SLACK_BOT_TOKEN = "YOUR_TOKEN"
SLACK_APP_TOKEN = "YOUR_TOKEN"
OPENAI_API_KEY = "YOUR_TOKEN"

import os
import openai
from slack_bolt.adapter.socket_mode import SocketModeHandler
from slack import WebClient
from slack_bolt import App

# Event API & Web API
app = App(token=SLACK_BOT_TOKEN)
client = WebClient(SLACK_BOT_TOKEN)

# This gets activated when the bot is tagged in a channel
@app.event("app_mention")
def handle_message_events(body, logger):
# Log message
print(str(body["event"]["text"]).split(">")[1])

# Create prompt for ChatGPT
prompt = str(body["event"]["text"]).split(">")[1]

# Let thre user know that we are busy with the request
response = client.chat_postMessage(channel=body["event"]["channel"],
thread_ts=body["event"]["event_ts"],
text=f"Hello from your bot! :robot_face: \nThanks for your request, I'm on it!")

# Check ChatGPT
openai.api_key = OPENAI_API_KEY
response = openai.Completion.create(
engine="text-davinci-003",
prompt=prompt,
max_tokens=1024,
n=1,
stop=None,
temperature=0.5).choices[0].text


# Reply to thread
response = client.chat_postMessage(channel=body["event"]["channel"],
thread_ts=body["event"]["event_ts"],
text=f"Here you go: \n{response}")

if __name__ == "__main__":
SocketModeHandler(app, SLACK_APP_TOKEN).start()

Step 5: Test out

Voila! Add your bot to a channel in the integration tab (the first step in video).

The basics are now in your hands to tweak the above and solve your use case. Easily search for information, summarise long messages, quick Q&A,… Once ready, we’ll deploy this on a server in order to have this application nonstop available. This will be the topic of my next blog!

❤️ If you found this article helpful, I’d be grateful if you could follow me on Medium and give it a clap or two. Your support means a lot to me. Thank you!

Check out how you can build out a LinkedIn Post Generator Web App in my next blog:

Enjoy!
Alexandre t’Kint

--

--