File size: 2,995 Bytes
359264c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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'