Spaces:
Sleeping
Sleeping
Neurolingua
commited on
Commit
•
052e52f
1
Parent(s):
d173c0d
Update app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,59 @@
|
|
1 |
-
from flask import Flask, request
|
2 |
-
from twilio.twiml.messaging_response import MessagingResponse
|
3 |
-
from twilio.rest import Client
|
4 |
-
import os
|
5 |
-
|
6 |
-
# Initialize the Flask app
|
7 |
-
app = Flask(__name__)
|
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 |
-
# Start the Flask server
|
60 |
app.run(host='0.0.0.0', port=7860)
|
|
|
1 |
+
from flask import Flask, request
|
2 |
+
from twilio.twiml.messaging_response import MessagingResponse
|
3 |
+
from twilio.rest import Client
|
4 |
+
import os
|
5 |
+
|
6 |
+
# Initialize the Flask app
|
7 |
+
app = Flask(__name__)
|
8 |
+
|
9 |
+
account_sid = os.environ.get('TWILIO_ACCOUNT_SID')
|
10 |
+
auth_token = os.environ.get('TWILIO_AUTH_TOKEN')
|
11 |
+
client = Client(account_sid, auth_token)
|
12 |
+
|
13 |
+
# OpenAI API Key
|
14 |
+
|
15 |
+
# WhatsApp number to send messages from (your Twilio number)
|
16 |
+
from_whatsapp_number = 'whatsapp:+14155238886'
|
17 |
+
|
18 |
+
# Route to handle incoming WhatsApp messages
|
19 |
+
@app.route('/whatsapp', methods=['POST'])
|
20 |
+
def whatsapp_reply():
|
21 |
+
incoming_msg = request.values.get('Body', '').lower()
|
22 |
+
sender = request.values.get('From')
|
23 |
+
|
24 |
+
if 'bookkeeping' in incoming_msg:
|
25 |
+
response_text = "Please provide the details you'd like to record."
|
26 |
+
else:
|
27 |
+
response_text = get_agricultural_insights(incoming_msg)
|
28 |
+
|
29 |
+
send_message(sender, response_text)
|
30 |
+
return '', 204 # Return an empty response to Twilio
|
31 |
+
|
32 |
+
def get_agricultural_insights(query):
|
33 |
+
|
34 |
+
return "I'm sorry, I couldn't process that request. Please try again later."
|
35 |
+
|
36 |
+
def send_message(to, body):
|
37 |
+
try:
|
38 |
+
message = client.messages.create(
|
39 |
+
from_=from_whatsapp_number,
|
40 |
+
body=body,
|
41 |
+
to=to
|
42 |
+
)
|
43 |
+
print(f"Message sent with SID: {message.sid}")
|
44 |
+
except Exception as e:
|
45 |
+
print(f"Error sending message: {e}")
|
46 |
+
|
47 |
+
# Function to send an initial message
|
48 |
+
def send_initial_message(to_number):
|
49 |
+
send_message(
|
50 |
+
f'whatsapp:{to_number}',
|
51 |
+
'Welcome to the Agri AI Chatbot! How can I assist you today?'
|
52 |
+
)
|
53 |
+
|
54 |
+
if __name__ == '__main__':
|
55 |
+
# Send an initial message when the script starts
|
56 |
+
send_initial_message('916382792828') # Replace with the recipient's number
|
57 |
+
|
58 |
+
# Start the Flask server
|
|
|
59 |
app.run(host='0.0.0.0', port=7860)
|