gendisjawi's picture
Upload folder using huggingface_hub
359264c verified
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__)
# Retrieve Twilio credentials using os.getenv()
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')
# Optional: Add a default value or error handling if environment variables are not set
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 initialization (optional, but useful if you want to send messages)
twilio_client = Client(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN)
# Fruit information dictionary
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
"""
# Get the message body and sender from the Twilio request
incoming_msg = request.form.get('Body', '').lower().strip()
# Create a Twilio messaging response
resp = MessagingResponse()
# Check if the fruit exists in our dictionary
if incoming_msg in FRUIT_INFO:
fruit_data = FRUIT_INFO[incoming_msg]
# Construct a detailed response
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:
# Handle unknown fruit
resp.message("Sorry, I don't have information about that fruit. Try 'apple' or 'orange'.")
return str(resp)
# For local testing
if __name__ == '__main__':
app.run(debug=True)
# Note: You'll need to set up these environment variables
# export TWILIO_ACCOUNT_SID='your_account_sid'
# export TWILIO_AUTH_TOKEN='your_auth_token'
# export TWILIO_WHATSAPP_NUMBER='whatsapp:+14155238886'