Skip to content

Instantly share code, notes, and snippets.

Created December 25, 2022 03:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Enable chatGPT as chatbot on slack.
import os
import openai
from slack_sdk import WebClient
from slackeventsapi import SlackEventAdapter
from flask import Flask
# To create a chatbot on Slack, you will need to do the following steps:
# - Go to the Slack API website and sign in to your Slack account. Click on the "Start Building" button in the top
# right corner of the page.
# - Give your app a name and select a workspace to develop it in. Click on the "Create App" button to continue. Give
# your bot a display name and default username, and then click on the "Add Bot User" button.
# - Your bot user will now be created, and you will see an "Install App" button on the page. Click on this button to
# install your app to your workspace. Follow the prompts to authorize the app and install it to your workspace. Once
# the app is installed, you will see an "Install App" success message, and you will be redirected to the app's
# management page.
# - In the "Add an OAuth Scope" dialog, select chat:write scope from the list of available scopes.
# - Enable event subscription and add message.channels event to the list of events. You can also add message.groups
# and events if you want your bot to respond to messages in private channels and direct messages.
# - Add the url of your server to the "Request URL" field. You can use ngrok to expose your local server to the
# internet. You can also use the url of your server if it is accessible from the internet. - Click on the "Save
# Changes" button to save your changes.
app = Flask(__name__)
slack_web_client: WebClient = WebClient(token=os.environ.get("SLACK_BOT_OAUTH_TOKEN"))
slack_events_adapter = SlackEventAdapter(os.environ.get("SLACK_SINGING_SECRET"), "/slack/events", app)
openai.api_key = os.environ.get("OPENAI_API_KEY")
def message(payload):
event = payload.get("event", {})
channel_id = event.get("channel")
user_id = event.get("user")
text = event.get("text")
if user_id != "U04G5G98F6F":
response = openai.Completion.create(model="text-davinci-003",
prompt=text + ", answer me with a rhyme from the Grinch movie",
if __name__ == "__main__":"", port=3000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment