-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathchatbot.py
81 lines (67 loc) · 3.9 KB
/
chatbot.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
72
73
74
75
76
77
78
79
80
81
import openai
import twilio
import twilio.twiml
# Your Twilio Account SID and Auth Token
account_sid = "YOUR_ACCOUNT_SID"
auth_token = "YOUR_AUTH_TOKEN"
# Your Twilio phone number
twilio_phone_number = "+1234567890"
# OpenAI API key
openai_api_key = "YOUR_OPENAI_API_KEY"
# Initialize the OpenAI and Twilio clients
openai_client = openai.Client(api_key=openai_api_key)
twilio_client = twilio.Client(account_sid, auth_token)
# Define a function to handle incoming messages
def handle_incoming_message(from_number, body):
# Use GPT-3 to generate a response to the message
response = openai_client.engine.create_completion(
engine="text-davinci-002", prompt=body, temperature=0.7).text
# Check if the message is a question
if body.endswith("?"):
# Use GPT-3 to generate an answer to the question
response = openai_client.engine.create_completion(
engine="text-davinci-002", prompt=body, temperature=0.7).text
elif "inquiry" in body.lower() or "complaint" in body.lower():
# Handle customer inquiries and complaints
response = "We apologize for any inconvenience you may have experienced. Please send us more details about your inquiry or complaint, and we will do our best to resolve the issue as soon as possible."
elif "recommendation" in body.lower():
# Provide personalized recommendations
response = "Based on your past purchases and browsing history, we recommend the following products: [Product A, Product B, Product C]."
elif "promotional" in body.lower():
# Send promotional messages
response = "Thank you for your interest in our products! We are currently running a sale on [Product A, Product B, Product C]. Use promo code ABC123 at checkout to save 20%."
elif "appointment" in body.lower():
# Schedule appointments
response = "We would be happy to schedule an appointment for you. Please let us know the date and time that you prefer, and we will do our best to accommodate your request."
elif "order" in body.lower():
# Process orders
response = "Thank you for your order! Your purchase will be shipped to you within 3-5 business days. If you have any questions about your order, please don't hesitate to ask."
elif "reminder" in body.lower():
# Send reminders
response = "We'll be happy to set a reminder for you. Please let us know the date and time you would like to be reminded, and the message you would like us to send you."
elif "support" in body.lower():
# Provide support
response = "We are here to help! Please let us know how we can assist you, and we will do our best to resolve any issues you may be experiencing."
elif "feedback" in body.lower():
# Gathering feedback
response = "We value your feedback and appreciate you taking the time to share it with us. Your thoughts and opinions help us to improve our products and services."
elif "information" in body.lower():
# Providing information
response = "We would be happy to provide you with more information about our products and services. Please let us know what specific information you are looking for, and we will do our best to help."
else:
# Use GPT-3 to generate a general response
response = openai_client.engine.create_completion(
engine="text-davinci-002", prompt=body, temperature=0.7).text
# Send the response back to the user via SMS
message = twilio_client.messages.create(
to=from_number, from_=twilio_phone_number, body=response)
# Set up a Flask route to listen for incoming messages
@app.route("/sms", methods=["POST"])
def sms_reply():
# Get the incoming message details
from_number = request.form["From"]
body = request.form["Body"]
# Handle the incoming message
handle_incoming_message(from_number, body)
# Return an empty TwiML response
return str(twilio.twiml.Response())