[Python] struggling to make a twilio webhook with python

Discussão em 'Python' iniciado por Stack, Março 25, 2025.

  1. Stack

    Stack Membro Participativo

    so for a high school project i have to show something with code for an app, the idea is that you send a "yes" or "no" to a phone number and a screen in the computer would go green or red respectively

    first there's the twilio phone number, then there's the code, that i got from ai cuz even though i wanna be a programmer rn i have no idea what im doing(most of this is ai), then i connected it to ngrok which is connected to a python code, everything looks fine, or im not exactly finding any errors but i send a message to the phone number and i just get an error, neither twilio, the code, or ngrok sees it, i dont know whats wrong and i need to finish this for friday so im pretty stressed abt it here's the code:

    from flask import Flask, request

    app = Flask(__name__)

    # Default background color
    screen_color = "white"

    @app.route("/", methods=["GET"])
    def home():
    """Displays the webpage showing the current route status."""
    return f"""
    <html>
    <head>
    <title>Route Status</title>
    </head>
    <body style='background-color: {screen_color}; text-align: center; font-size: 24px;'>
    <h1>Hiking Route Status</h1>
    <p>Current Status: {"OPEN" if screen_color == "green" else "CLOSED"}</p>
    </body>
    </html>
    """

    @app.route("/sms", methods=["POST"])
    def receive_sms():
    """Handles incoming SMS from Twilio and updates the background color."""
    global screen_color

    # Read SMS body from Twilio
    message_body = request.form.get("Body", "").strip().lower()
    print("Received SMS body:", message_body)

    # Update the screen color based on message
    if message_body in ["yes", "true", "open", "y"]:
    screen_color = "green"
    elif message_body in ["no", "false", "closed", "n"]:
    screen_color = "red"

    return "Status Updated", 200

    if __name__ == "__main__":
    app.run(port=5000, debug=True)


    and here's the details of the twilio number: enter image description here

    if there is anything else you need to know just ask of course

    Continue reading...

Compartilhe esta Página