-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatgpt_slackbot.py
53 lines (37 loc) · 1.25 KB
/
chatgpt_slackbot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import threading
from revChatGPT.revChatGPT import Chatbot
from flask import Flask, json, request
import requests
# Slack OAuth Access Token
# slack_token = "<your-bot-user-oauth-token>"
# response_url = "<your-incoming-webhook-url>"
config = {
"email": "<your email>",
"password": "<your password>",
"Authorization": "<API-KEY>",
"session_token": "<your session token>",
}
chatbot = Chatbot(config, conversation_id=None)
# Initialize the Flask object which will be used to handle HTTP requests
# from Slack
app = Flask(__name__)
@app.route("/chatgpt", methods=["POST"])
def chatgpt_handler():
data = request.form.get("text")
channel = request.form.get("channel_id"),
# starting a new thread for doing the actual processing
x = threading.Thread(
target=sub_chatgpt,
args=[data, channel])
x.start()
return "Contacting OpenAI.... please wait (can take ~30 sec)"
def sub_chatgpt(data, channel):
message = chatbot.get_chat_response(data)['message']
payload = {"token": slack_token,
"channel": channel,
"text": message}
requests.post(response_url,
json.dumps(payload))
# Run on local port 3000
if __name__ == "__main__":
app.run(port=3000, debug=True)