Neurolingua commited on
Commit
052e52f
1 Parent(s): d173c0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -59
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
- # Twilio credentials
10
- account_sid = 'AC97ad50edcf94fdd7d4576be9651bf4bf'
11
- auth_token = '6d8b2559ffbbdbc5d06542e545220aaa'
12
- client = Client(account_sid, auth_token)
13
-
14
- # OpenAI API Key
15
-
16
- # WhatsApp number to send messages from (your Twilio number)
17
- from_whatsapp_number = 'whatsapp:+14155238886'
18
-
19
- # Route to handle incoming WhatsApp messages
20
- @app.route('/whatsapp', methods=['POST'])
21
- def whatsapp_reply():
22
- incoming_msg = request.values.get('Body', '').lower()
23
- sender = request.values.get('From')
24
-
25
- if 'bookkeeping' in incoming_msg:
26
- response_text = "Please provide the details you'd like to record."
27
- else:
28
- response_text = get_agricultural_insights(incoming_msg)
29
-
30
- send_message(sender, response_text)
31
- return '', 204 # Return an empty response to Twilio
32
-
33
- def get_agricultural_insights(query):
34
-
35
- return "I'm sorry, I couldn't process that request. Please try again later."
36
-
37
- def send_message(to, body):
38
- try:
39
- message = client.messages.create(
40
- from_=from_whatsapp_number,
41
- body=body,
42
- to=to
43
- )
44
- print(f"Message sent with SID: {message.sid}")
45
- except Exception as e:
46
- print(f"Error sending message: {e}")
47
-
48
- # Function to send an initial message
49
- def send_initial_message(to_number):
50
- send_message(
51
- f'whatsapp:{to_number}',
52
- 'Welcome to the Agri AI Chatbot! How can I assist you today?'
53
- )
54
-
55
- if __name__ == '__main__':
56
- # Send an initial message when the script starts
57
- send_initial_message('916382792828') # Replace with the recipient's number
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)