ChatGPT Slack Bot

ChatGPT Slack Bot

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.

The GPT Bot :tada:

Note: the API has a higher uptime compared to the ChatGPT UI šŸ˜„

1. Register an app with Slack and gather all the tokens

First step is to register a new app on Slack and obtain the Slack Bot Token and Slack API Token.

  • Log in to your Slack workspace and Go to Slack API website.
  • Click on ā€œCreate an appā€ and select ā€œFrom scratchā€
  • Give your app a name, select your Slack workspace.
  • 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.
  • In settings, click on ā€œSocket Modeā€, enable it and give the token a name. Copy the Slack Bot App Token.
  • 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.
  • Go to the ā€œOAuth & Permissionsā€ section and install your app to your workspace.
  • Copy the Slack Bot Token.

2. Get the OpenAI API key . [ Valid for a month for free users ]

Need to have a OpenAI API key to integrate ChatGPT.

  • Go to OpenAI website.
  • Go to API ket section and create a new API key after you login.
  • Copy the API key.

3. Install necessary dependencies

pip install openai
pip install slack-bolt 
pip install slack

Install these dependencies . Slack - bolt is a bunch of tools and libraries that allow developers to easily create Slack applications.

  1. Run the application

Fill in the first 3 tokens in this script with your tokens and run the application.

SLACK_BOT_TOKEN = "xoxb-2196501177986-5475158173799-DTxoGAJMjSrqZ1UbKJQDRkYq"
SLACK_APP_TOKEN = "xapp-1-A05DZ02E7JT-5502278073649-6d0d2eabadfa2388189e2bd414393d764d87de68f4df7234f2e87a421eba9440"
OPENAI_API_KEY  = "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"
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 the 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 LazyPay junkies !! :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()

Add here we go !! Add your bot to a channel in the integration tab. :rocket: