#!/usr/bin/env python3 import os import json from flask import Flask, request, Response import logging import sys # Configure basic logging to print to console logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', stream=sys.stdout) app = Flask(__name__) listen_port = 1998 # The port specified @app.route('/sms_receive_test', methods=['POST']) # Define the path Telnyx will call def handle_incoming_sms(): """Listens for and logs incoming webhook POST requests.""" logging.info(f"Received request on /sms_receive_test from {request.remote_addr}") try: # Check if the content type is JSON, though Telnyx should always send JSON if request.is_json: payload = request.get_json() logging.info("Received JSON payload:") # Log the received payload nicely formatted logging.info(json.dumps(payload, indent=2)) # --- TODO: Add logic here if needed (e.g., save to DB) --- # For now, just logging is sufficient proof of receipt. # Acknowledge receipt to Telnyx with 204 No Content return Response(status=204) else: # Log if unexpected content type is received logging.warning(f"Received non-JSON request. Content-Type: {request.content_type}") # Still acknowledge, but maybe log the raw data if needed # raw_data = request.get_data(as_text=True) # logging.info(f"Raw data: {raw_data}") return Response(status=204) # Acknowledge anyway except Exception as e: logging.exception("Error processing incoming webhook") # Return 500 Internal Server Error to Telnyx if processing fails # Telnyx *might* retry the webhook later in case of 5xx errors. return Response(status=500) if __name__ == '__main__': logging.info(f"Starting simple webhook listener on port {listen_port}...") # Run on 0.0.0.0 to be accessible externally (within firewall limits) # DO NOT use debug=True in production environments app.run(host='0.0.0.0', port=listen_port, debug=False)