|
from flask import Flask, request |
|
from twilio.twiml.messaging_response import MessagingResponse |
|
from twilio.rest import Client |
|
import os |
|
from dotenv import load_dotenv |
|
|
|
load_dotenv() |
|
app = Flask(__name__) |
|
|
|
|
|
TWILIO_ACCOUNT_SID = os.getenv('TWILIO_ACCOUNT_SID') |
|
TWILIO_AUTH_TOKEN = os.getenv('TWILIO_AUTH_TOKEN') |
|
TWILIO_WHATSAPP_NUMBER = os.getenv('TWILIO_WHATSAPP_NUMBER', 'whatsapp:+14155238886') |
|
|
|
|
|
if not TWILIO_ACCOUNT_SID or not TWILIO_AUTH_TOKEN: |
|
raise ValueError("Missing Twilio credentials. Please set TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables.") |
|
|
|
|
|
twilio_client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) |
|
|
|
|
|
|
|
FRUIT_INFO = { |
|
'apple': { |
|
'description': 'A sweet, crisp fruit that grows on apple trees.', |
|
'nutrition': 'Rich in fiber, vitamin C, and antioxidants.', |
|
'varieties': 'Common varieties include Gala, Fuji, Granny Smith, and Red Delicious.', |
|
'fun_fact': 'Apples float because 25% of their volume is air.' |
|
}, |
|
'orange': { |
|
'description': 'A citrus fruit known for its bright orange color and tangy flavor.', |
|
'nutrition': 'Excellent source of vitamin C, folate, and thiamine.', |
|
'varieties': 'Popular types include Navel, Valencia, and Mandarin oranges.', |
|
'fun_fact': 'Oranges originated in Southeast Asia and were first cultivated in China.' |
|
} |
|
} |
|
|
|
@app.route('/webhook', methods=['POST']) |
|
def handle_whatsapp_message(): |
|
""" |
|
Handle incoming WhatsApp messages and return fruit information |
|
""" |
|
|
|
incoming_msg = request.form.get('Body', '').lower().strip() |
|
|
|
|
|
resp = MessagingResponse() |
|
|
|
|
|
if incoming_msg in FRUIT_INFO: |
|
fruit_data = FRUIT_INFO[incoming_msg] |
|
|
|
|
|
response_text = f"π {incoming_msg.capitalize()} Information π\n\n" |
|
response_text += f"Description: {fruit_data['description']}\n\n" |
|
response_text += f"Nutrition: {fruit_data['nutrition']}\n\n" |
|
response_text += f"Varieties: {fruit_data['varieties']}\n\n" |
|
response_text += f"Fun Fact: {fruit_data['fun_fact']}" |
|
|
|
resp.message(response_text) |
|
else: |
|
|
|
resp.message("Sorry, I don't have information about that fruit. Try 'apple' or 'orange'.") |
|
|
|
return str(resp) |
|
|
|
|
|
if __name__ == '__main__': |
|
app.run(debug=True) |
|
|
|
|
|
|
|
|
|
|