Created
December 25, 2022 03:00
-
-
Save javier-games/8bf672b097f17af20b86bf10bcd91341 to your computer and use it in GitHub Desktop.
Enable chatGPT as chatbot on slack.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 message.im 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") | |
@slack_events_adapter.on("message") | |
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", | |
temperature=0.8, | |
max_tokens=250) | |
slack_web_client.chat_postMessage(channel=channel_id, | |
text=response["choices"][0]["text"]) | |
if __name__ == "__main__": | |
app.run(host="0.0.0.0", port=3000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment