from flask import Flask, request, Response import xml.etree.ElementTree as ET import html # Initialize the Flask application app = Flask(__name__) # This is the endpoint TeleCloud will send requests to. # Make sure it's unique and secure. @app.route('/your_module_endpoint', methods=['POST']) def telecloud_module(): """ Handles incoming POST requests from TeleCloud. """ # --- 1. Read Incoming Data --- # Flask's request.form object contains the POST data. # Note: Arrays may be flattened by web servers for Python frameworks. The key might look like 'Customer[first_name]'. # Adjust the keys if your server configuration handles them differently. service = request.form.get('SERVICE', '') customer_first_name = request.form.get('Customer[first_name]', 'Guest') var1_value = request.form.get('VAR1', '') # Example: This could be an IMEI var2_value = request.form.get('VAR2', '') # Example: This could be a SIM carrier_product_id = request.form.get('CARRIER_PRODUCT_ID', '') # --- 2. Handle the Module Test Request --- # When you configure the module in TeleCloud, it sends a test request. # Your module MUST respond correctly to be saved. if service == 'TEST': xml_response = 'Y' return Response(xml_response, mimetype='text/xml') # --- 3. Your Business & Carrier Logic --- # This is where you'll add your custom code to interact with your carrier's API. # Use the variables from Step 1 to build your request to the carrier. # For this example, we'll simulate a carrier interaction. carrier_provisioned_successfully = True # Change to False to test error handling carrier_error_message = 'Carrier API Error: Invalid SIM and IMEI combination.' new_phone_number_from_carrier = '5559876543' # A value returned by the carrier # --- 4. Construct the XML Response for TeleCloud --- # Using a library like xml.etree.ElementTree is safer than building XML with strings. module_root = ET.Element('MODULE') if carrier_provisioned_successfully: # The operation was a success. success_element = ET.SubElement(module_root, 'SUCCESS') success_element.text = 'Y' # Example: The carrier assigned a new phone number during activation. # We want to store this in VAR3 on the TeleCloud order. # Your Product Group must have VAR3 configured for this to be saved. var3_element = ET.SubElement(module_root, 'VAR3') # Use html.escape to prevent XML injection, similar to htmlspecialchars in PHP. var3_element.text = html.escape(new_phone_number_from_carrier) # Optional: If provisioning takes a long time, you can tell TeleCloud to wait. # status_element = ET.SubElement(module_root, 'STATUS') # status_element.text = 'PENDING' else: # The operation failed. success_element = ET.SubElement(module_root, 'SUCCESS') success_element.text = 'N' error_element = ET.SubElement(module_root, 'ERROR') error_element.text = html.escape(carrier_error_message) # --- 5. Send the Response --- # Convert the XML ElementTree to a string and return it with the correct mimetype. xml_response_string = ET.tostring(module_root, encoding='unicode') return Response(xml_response_string, mimetype='text/xml') # --- How to Run This Script --- # To run for development: # 1. Open your terminal in the same directory as this file. # 2. Run the command: flask --app this_module_file_name.py run # # For production, use a proper WSGI server like Gunicorn or uWSGI. # Example with Gunicorn: gunicorn --workers 4 --bind 0.0.0.0:8000 app:app if __name__ == '__main__': # This allows running the script directly for simple testing. # The debug=True flag provides helpful error pages but should be # turned OFF in a production environment. app.run(host='0.0.0.0', port=5000, debug=True)