Step 1: Handle Incoming Messages (Opt-in)
Python (Flask) Webhook
This webhook:
• Listens for incoming messages.
• Checks if the user texted “APPT” (case insensitive).
• Sends a confirmation message.
from flask import Flask, request
from twilio.twiml.messaging_response import MessagingResponse
app = Flask(name)
@app.route(“/sms”, methods=[‘POST’])
def sms_reply():
incoming_msg = request.form.get(‘Body’).strip().upper() # Normalize input
from_number = request.form.get(‘From’)
resp = MessagingResponse()
if incoming_msg == "APPT":
# Store the opt-in (e.g., database or file)
resp.message("You're successfully opted in for appointment notifications!")
else:
resp.message("Reply with 'APPT' to opt-in for notifications.")
return str(resp)
if name == “main“:
app.run(debug=True)
Step 2: Configure Twilio to Use Webhook
1. In Twilio Console, go to Phone Numbers > Manage > Active Numbers.
2. Click on your Toll-Free Number.
3. Under Messaging, set:
• A Message Comes In → Webhook URL (e.g., https://yourserver.com/sms)
• Select HTTP POST.
Step 3: Test the Flow
1. Send an SMS from your phone to your Twilio Toll-Free Number with “APPT”.
2. Twilio should forward it to your webhook, and you should receive a response confirming the opt-in.
Step 4: Store Opt-ins in a Database
User opt-in will be stored in a simple json file.