This repository has been archived by the owner on Dec 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
71 lines (53 loc) · 1.88 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
from flask import Flask, request
from revChatGPT.revChatGPT import Chatbot
import os
from twilio.rest import Client
from twilio.twiml.messaging_response import MessagingResponse
import threading
from os.path import join, dirname
from dotenv import load_dotenv
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
app = Flask(__name__)
client = Client(os.environ['TWILIO_SID'], os.environ['TWILIO_TOKEN'])
config = {
"email": os.environ["OPENAI_EMAIL"],
"password": os.environ["OPENAI_PASSWORD"],
}
def getChatResponse(prompt, number, twilio_number):
try:
chatbot = Chatbot(config, conversation_id=None)
chatbot.reset_chat()
chatbot.refresh_session()
message = chatbot.get_chat_response(prompt)
client.messages.create(to=number,
from_=twilio_number,
body=message['message'][:1600])
except:
client.messages.create(to=number,
from_=twilio_number,
body="We're at capacity. Sorry the request could not be processed.")
return(None)
@app.route('/')
def index():
return ("This is a backend service. You'll need to add your own OpenAI session token and Twilio credentials")
@app.route('/sms', methods=['POST'])
def sms():
message = request.form['Body']
phone_number = request.form['From']
twilio = request.form['To']
thread = threading.Thread(target=getChatResponse,
args=(message, phone_number, twilio))
thread.start()
return ("200")
@app.route('/timeout', methods=['POST'])
def timeout():
phone_number = request.form['From']
twilio = request.form['To']
client.messages.create(to=phone_number,
from_=twilio,
body="Sorry an error occured")
return ("timeout")
if __name__ == "__main__":
from waitress import serve
serve(app, host="0.0.0.0", port=8080)