forked from pedrorito/ChatGPTSlackBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
48 lines (40 loc) · 1.29 KB
/
app.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
import os
import re
import time
from threading import Thread
from revChatGPT.V3 import Chatbot
from slack_bolt import App
ChatGPTConfig = {
"api_key": os.getenv("OPENAI_API_KEY"),
}
if os.getenv("OPENAI_ENGINE"):
ChatGPTConfig["engine"] = os.getenv("OPENAI_ENGINE")
app = App()
chatbot = Chatbot(**ChatGPTConfig)
# Listen for an event from the Events API
@app.event("app_mention")
def event_test(event, say):
prompt = re.sub('(?:\s)<@[^, ]*|(?:^)<@[^, ]*', '', event['text'])
try:
response = chatbot.ask(prompt)
#user = event['user']
#user = f"<@{user}> you asked:"
#asked = ['>',prompt]
#asked = "".join(asked)
#send = [user,asked,response]
send = [response]
send = "\n".join(send)
except Exception as e:
print(e)
send = "We're experiencing exceptionally high demand. Please, try again."
# Get the `ts` value of the original message
original_message_ts = event["ts"]
# Use the `app.event` method to send a reply to the message thread
say(send, thread_ts=original_message_ts)
def chatgpt_refresh():
while True:
time.sleep(60)
if __name__ == "__main__":
thread = Thread(target=chatgpt_refresh)
thread.start()
app.start(4000) # POST http://localhost:4000/slack/events